[WPF 学习] 5. 1 版本升级之压缩和解压

生成的单个文件的压缩率可达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);

你可能感兴趣的:([WPF 学习] 5. 1 版本升级之压缩和解压)