c# gzipstream decompression is more like depression
why can't I get this code here to work? I want to call this on a byte array tha开发者_如何转开发t was previously compressed....anyway, it just returns an empty string...
public static string FromGZipToString( this byte[] source )
{
using( MemoryStream stream = new MemoryStream( ) )
{
stream.Write( source, 0, source.Length );
using (var gzipstream = new GZipStream(stream, CompressionMode.Decompress))
using (var reader = new StreamReader(gzipstream))
{
return reader.ReadToEnd( );
}
}
}
here is the compress code by the way....
public static byte[] ToGZip( this string source )
{
using( var stream = new MemoryStream( ) )
using( var compressor = new GZipStream( stream, CompressionMode.Compress ) )
{
var bytes = System.Text.UTF8Encoding.UTF8.GetBytes( source );
compressor.Write( bytes, 0, bytes.Length );
return stream.ToArray( );
}
}
Your compression routine is faulty. It shouldn't be reading from stream until the compressor has been closed (or disposed), allowing the compressor to finish writing all bytes to the steam.
Check out my answer to this question: compressing and decompressing source data gives result different than source data
精彩评论