C#GZipStream转换为字符串 [英] C# GZipStream to String

查看:465
本文介绍了C#GZipStream转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种将GZipStream写入字符串的方法。

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

我正在使用:

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



<我已经尝试了几种方法,但是无法弄清楚。
有人有什么想法吗?

I have tried several methods, but can't figure it out. Does anyone have any ideas?

非常感谢,
Brett

Many thanks, Brett

推荐答案

您已经解压缩了 GZipStream ,因此您需要从中读取数据。最简单的方法是将 GZipStream 包装为 StreamReader 其中具有 ReadToEnd 方法返回字符串。

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.

类似的东西:

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

(<< c $ c>使用语句确保关闭 inFile 并释放任何其他资源。)

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

NB 确实假定 inFile 包含文本编码的UTF-8或UTF-16。二进制内容或其他文本编码可能会导致问题(您可以使用其他 StreamReader 构造函数来覆盖编码)。

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).

这篇关于C#GZipStream转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆