上面感觉有点复杂,直接读取多个文件的字符串再合并感觉简单很多。既然楼主告诉已知多个文件内容,我介绍一下压缩操作。
代码如下:
static void Main(string[] args) { //1.获取从文件中读取的文本(假如文字如下) const string input = "测试文字:你好"; //2.压缩 var compressResult = GZip.Compress(input); //2.1 显示压缩结果 Console.WriteLine(compressResult); //3.解压 var decompressResult = GZip.Decompress(compressResult); //3.1 显示解压结果 Console.WriteLine(decompressResult); Console.Read(); }
一个助手类封装 GZip.cs
public class GZip { /// <summary> /// 压缩 /// </summary> /// <param name="text">文本</param> public static string Compress(string text) { var buffer = Encoding.UTF8.GetBytes(text); return Convert.ToBase64String(Compress(buffer)); } /// <summary> /// 解压缩 /// </summary> /// <param name="text">文本</param> public static string Decompress(string text) { var buffer = Convert.FromBase64String(text); using (var ms = new MemoryStream(buffer)) { using (var zip = new GZipStream(ms, CompressionMode.Decompress)) { using (var reader = new StreamReader(zip)) { return reader.ReadToEnd(); } } } } /// <summary> /// 压缩 /// </summary> /// <param name="buffer">字节流</param> public static byte[] Compress(byte[] buffer) { if (buffer == null) return null; using (var ms = new MemoryStream()) { using (var zip = new GZipStream(ms, CompressionMode.Compress, true)) { zip.Write(buffer, 0, buffer.Length); } return ms.ToArray(); } } }
可以看到,Compress() 和 Decompress() 就使用了流进行操作。
结果如下:
/// <summary> /// 合并文件 /// </summary> /// <param name="strDirectory"></param> /// <param name="strMergeResultFile"></param> private void MergeFiles(string strInputDirectory, string strMergeResultFile) { string[] arrFileNames = Directory.GetFiles(strInputDirectory); int iSumFile = arrFileNames.Length; //progressBar1.Maximum = iSumFile; FileStream AddStream = new FileStream(strMergeResultFile + "_temp", FileMode.OpenOrCreate); BinaryWriter AddWriter = new BinaryWriter(AddStream); long firstFileLength = 0; FileStream TempStream = null; BinaryReader TempReader = null; //文件长度列表 string allFileLength = ""; //文件长度和文件内容叠加 { for (int i = 0; i < iSumFile; i++) { TempStream = new FileStream(arrFileNames[i], FileMode.Open); TempReader = new BinaryReader(TempStream); AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length)); if (firstFileLength == 0) { firstFileLength = TempReader.BaseStream.Length; allFileLength = firstFileLength.ToString().PadLeft(20, '0'); } //暂时只处理2个文件的情况,下面是多文件 //allFileLength += "," + TempReader.BaseStream.Length.ToString().PadLeft(20, '0'); TempReader.Close(); TempStream.Close(); } allFileLength = allFileLength.Trim(','); //释放资源 AddWriter.Flush(); AddWriter.Close(); AddStream.Close(); TempStream = null; TempReader = null; } //创建含有头信息的文件- 也达到了加密的效果 { System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding(); byte[] BytesMessage = UTF8.GetBytes(allFileLength); AddStream = new FileStream(strMergeResultFile + "_r", FileMode.OpenOrCreate); AddWriter = new BinaryWriter(AddStream); //文件头 AddWriter.Write(BytesMessage); //写入原来文件 TempStream = new FileStream(strMergeResultFile, FileMode.Open); TempReader = new BinaryReader(TempStream); AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length)); long cc = TempReader.BaseStream.Length; TempReader.Close(); AddWriter.Flush(); AddWriter.Close(); TempStream.Close(); //释放 TempReader = null; AddWriter = null; TempStream = null; } //删掉临时 File.Delete(strMergeResultFile + "_temp"); }
qdqwdqw