如何在Stream(C#)中解压缩GZip?

时间:2011-07-04 08:24:33

标签: c# stream gzip gzipstream

此代码接收GZip编码的字符串。我怎么解码呢?

Stream stream = ret.GetResponseStream();

System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.Default);

string answer = reader.ReadToEnd();//answer is GZip encoded string !

byte[] bytes = Encoding.Default.GetBytes(answer);

//???

GZipStream compStream = new GZipStream(stream, CompressionMode.Decompress);

// ... what's next?

1 个答案:

答案 0 :(得分:18)

另一种方法是使用请求/响应的自动解压缩属性:

request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

在收到回复之前设置此属性。