开发者

C# GZipStream to String

I am in need of a way to write a GZipStream to a string.

I am using:

GZipStream Decompress = new GZipStream(inFile, CompressionMode.Decompress)

I have tried several methods, bu开发者_如何学Pythont can't figure it out. Does anyone have any ideas?

Many thanks, Brett


You have a decompressing GZipStream, so you need to read data from it. The easiest way is to wrap the GZipStream with a StreamReader which has a ReadToEnd method returning a string.

Something like:

string res;
using (var decompress = new GZipStream(inFile, CompressionMode.Decompress))
using (var sr = new StreamReader(decompress)) {
  res = sr.ReadToEnd();
}

(using statements ensure that inFile is closed and any other resources are freed.)

NB this does assume that inFile contains text encoded UTF-8 or UTF-16. Binary content or other text encoding could cause problems (you can override the encoding with a different StreamReader constructor).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜