生成的單個文件的壓縮率可達50%,忍不住要壓縮一下,當然下載的時候也需要解壓,我用的是GZipStream 1.壓縮 compressionStream.Flush(); 這很重要,否則msTarget.ToArray()會丟掉部分內容。 2.解壓 ...
生成的單個文件的壓縮率可達50%,忍不住要壓縮一下,當然下載的時候也需要解壓,我用的是GZipStream
1.壓縮
string filePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
FileInfo fi = new FileInfo(filePath);
using FileStream msSource = fi.OpenRead();
using MemoryStream msTarget = new MemoryStream();
using GZipStream compressionStream = new GZipStream(msTarget, CompressionMode.Compress);
msSource.CopyTo(compressionStream);
compressionStream.Flush();
compressionStream.Flush();這很重要,否則msTarget.ToArray()會丟掉部分內容。
2.解壓
using MemoryStream mss = new MemoryStream(buffer);
using FileStream mst = File.Create(currentProcess.MainModule.FileName + "~");
using GZipStream decompressionStream = new GZipStream(mss, CompressionMode.Decompress);
decompressionStream.CopyTo(mst);