我必须压缩responseStream,但流是不可写的。我已经编写了这段代码,但它不工作(C#中的GZip压缩)
试试这个,看看是否对你不起作用:我必须压缩responseStream,但流是不可写的。我已经编写了这段代码,但它不工作(C#中的GZip压缩),c#,gzip,C#,Gzip,试试这个,看看是否对你不起作用: try { TextReader reader = new StreamReader(responseStream); gzipBytes = ASCIIEncoding.ASCII.GetBytes(reader.ToString()); MemoryStream ms = new MemoryStream(); // Use the newly created memory stream for the compressed dat
try
{
TextReader reader = new StreamReader(responseStream);
gzipBytes = ASCIIEncoding.ASCII.GetBytes(reader.ToString());
MemoryStream ms = new MemoryStream();
// Use the newly created memory stream for the compressed data.
outStream = new GZipStream(ms, CompressionMode.Compress, true);
outStream.Write(gzipBytes, 0, gzipBytes.Length);
// Close the stream.
}
catch
{
}
您需要做的就是将第一行Stream inputStream=…
替换为您的输入流,例如:Stream inputStream=File.OpenRead(…)
或Stream inputStream=new StreamReader(…)
。。。无论什么应复制到zipStream
并执行压缩
另外,
使用
块也很好。它们释放非托管资源,而不是让它们四处浮动,直到垃圾收集器将其回收。没有迹象表明您试图写入responseStream。您也没有提供任何错误消息。我不认为有人能那样帮助你。请编辑您的帖子,使人们能够帮助您解决问题。ascienceoding.ASCII.GetBytes(reader.ToString())
基本上保证不起作用。改用reader.readToEnd()
当responseStream放入新的GZipStream时(responseStream,CompressionMode.Compress,true);然后它抛出异常“流不可写”以避免此异常我已在TextReader中读取responseStream,但它不起作用。也不抛出任何异常。responseStream
定义在哪里?在响应流中em获取响应responseStream=response.GetResponseStream();
using (Stream inputStream = ...)
using (MemoryStream outputStream = new MemoryStream())
using (GZipStream zipStream = new GZipStream(outputStream, CompressionMode.Compress, true))
{
inputStream.CopyTo(zipStream);
}