2011-07-04 40 views
9

Mã này nhận được chuỗi được mã hóa GZip. Làm thế nào tôi có thể giải mã nó?Làm thế nào để giải nén GZip trong luồng (C#)?

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? 

Trả lời

18

Một cách khác là sử dụng tài sản giải nén tự động của request/response:

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

Set thuộc tính này trước khi bạn nhận được câu trả lời.

+0

Xa lánh, điều đó giúp mọi việc trở nên dễ dàng hơn nhiều! – user1500403

Các vấn đề liên quan