C#重用GZipStream进行多次解压缩

时间:2016-09-24 02:10:15

标签: c# gzipstream

我需要单独压缩和解压缩数百万个字符串。第一个循环工作。第二个没有。基本上我不知道如何使用流。我如何获得第二种方法,即重用流的方法?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            string s = "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah ";
            for (int i=0; i<10000; ++i) {
                var aCompressed = Zip(Encoding.ASCII.GetBytes(s));
                var aDecompressed = UnZip(aCompressed, 0, aCompressed.Length);
                string sDbg = System.Text.Encoding.ASCII.GetString(aDecompressed);
                Debug.Assert(sDbg == s);
            }
            // This loop is an utter failure.
            var UnZipper = new CUnZip(); // attempt to setup all the unzip framework once
            for (int i = 0; i < 10000; ++i) {
                var aCompressed = Zip(Encoding.ASCII.GetBytes(s));
                var aDecompressed = UnZipper.UnZip(aCompressed, 0, aCompressed.Length);
                string sDbg = System.Text.Encoding.ASCII.GetString(aDecompressed);
                Debug.Assert(sDbg == s);
            }
        }
        static byte[] Zip(byte[] aIn) {
            using (var outStream = new MemoryStream()) {
                using (var tinyStream = new GZipStream(outStream, CompressionMode.Compress))
                using (var mStream = new MemoryStream(aIn))
                    mStream.CopyTo(tinyStream);
                return outStream.ToArray();
            }
        }
        static byte[] UnZip(byte[] aIn, int i0, int cb) {
            using (var inStream = new MemoryStream(aIn, i0, cb))
            using (var bigStream = new GZipStream(inStream, CompressionMode.Decompress))
            using (var bigStreamOut = new MemoryStream()) {
                bigStream.CopyTo(bigStreamOut);
                return bigStreamOut.ToArray();
            }
        }
        // this class is an utter failure
        class CUnZip {
            GZipStream bigStream;
            MemoryStream inStream, bigStreamOut;
            BinaryWriter argh;
            public CUnZip() {
                inStream = new MemoryStream();
                argh = new BinaryWriter(inStream);
                bigStream = new GZipStream(inStream, CompressionMode.Decompress);
                bigStreamOut = new MemoryStream();
            }
            public byte[] UnZip(byte[] aIn, int i0, int cb) {
                argh.Write(aIn, i0, cb);
                argh.Flush();
                bigStream.CopyTo(bigStreamOut);
                return bigStreamOut.ToArray();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

以下测试您的代码将解决问题:

inStream.Position = 0;

argh.Flush() - class CUnZip

中将其设为UnZip Method

<强>详细信息:

当您重用MemoryStream类中的CUnZip时,其位置设置为写入的最后一个字节并执行任何需要读取的操作,您需要重置{ {1}}到开头(0),否则它没有任何东西可读,因此即使position包含数据也无法读取,因为它指向stream的结尾