How do I use GZipStream with System.IO.MemoryStream?

前端 未结 9 915
遥遥无期
遥遥无期 2020-12-04 16:36

I am having an issue with this test function where I take an in memory string, compress it, and decompress it. The compression works great, but I can\'t seem to get the dec

9条回答
  •  有刺的猬
    2020-12-04 17:01

    This is a known issue: http://blogs.msdn.com/b/bclteam/archive/2006/05/10/592551.aspx

    I have changed your code a bit so this one works:

    var mStream = new MemoryStream(new byte[100]);
    var outStream = new System.IO.MemoryStream();
    
    using (var tinyStream = new GZipStream(outStream, CompressionMode.Compress))
    {
        mStream.CopyTo(tinyStream);           
    }
    
    byte[] bb = outStream.ToArray();
    
    //Decompress                
    var bigStream = new GZipStream(new MemoryStream(bb), CompressionMode.Decompress);
    var bigStreamOut = new System.IO.MemoryStream();
    bigStream.CopyTo(bigStreamOut);
    

提交回复
热议问题