Ionic.zip Gzipstream解压缩,坏状态(存储块长度无效)

时间:2017-01-06 06:44:21

标签: gzipstream

我使用ionic.zip gzipstream压缩和解压缩byte [], 它可以很好地压缩,但是当它从byte []压缩后压缩它告诉

  

错误状态(存储的块长度无效)

我的代码吼叫

Trace.WriteLine(s.Length);
var b = Encoding.UTF8.GetBytes(s);
Trace.WriteLine(b.Length);

byte[] b2;

var sw = new Stopwatch();
sw.Start();
using (var m = new MemoryStream())
{
    var stream = new GZipStream(m, CompressionMode.Compress,true);
    stream.Write(b,0,b.Length);
    stream.Flush();
    b2 = m.GetBuffer();
    stream.Close();
    sw.Stop();
    Trace.WriteLine(sw.ElapsedMilliseconds);
    Trace.WriteLine(b2.Length);
}

using (var m2 = new MemoryStream())
{
    m2.Write(b2,0,b2.Length);
    m2.Position = 0;
    var stream = new GZipStream(m2,CompressionMode.Decompress,true);
    var m3 = new MemoryStream();
    var buffer = new byte[1024];
    var n = 1;
    while (n != 0)
    {
        n = stream.Read(buffer, 0, buffer.Length);
        if (n > 0)
        {
            m3.Write(buffer,0,n);
        }
    }
    var b3 = m3.GetBuffer();
    Trace.WriteLine(b3);
}

解压缩代码就是文档所说的。here

我找不到其他文档, 解压后我该怎么办?

1 个答案:

答案 0 :(得分:0)

啊~~~,这太傻了。

有byte []压缩和解压缩方法。

GZipStream.CompressBuffer()GZipStream.UncompressBuffer()