Ionic.zip Gzipstream Decompress ,Bad state (invalid stored block lengths)

2k views Asked by At

i use ionic.zip gzipstream compress and decompress byte[], it can compress well,but when it decompressed from byte[] compressed it told

Bad state (invalid stored block lengths)

my codes bellow

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);
}

the decompress code is what the document said.here

and i found no other docs , what should i do when decompress?

1

There are 1 answers

0
goldii On

Ah~~~, it's so fool.

there are byte[] compress and decompress methods.

GZipStream.CompressBuffer() and GZipStream.UncompressBuffer()