0
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
 {
 }
4

1 回答 1

0

试试这个,看看它是否不适合你:

using (Stream inputStream = ...)
using (MemoryStream outputStream = new MemoryStream())
using (GZipStream zipStream = new GZipStream(outputStream, CompressionMode.Compress, true))
{
    inputStream.CopyTo(zipStream);
}

您所要做的就是用Stream inputStream = ...您的输入流替换第一行,例如:Stream inputStream = File.OpenRead(...)Stream inputStream = new StreamReader(...)...随便什么。应该复制到zipStream并执行压缩。

另外,using积木也不错。它们释放非托管资源,而不是让它们四处飘荡,直到垃圾收集器捡起它们。

于 2013-05-29T05:47:27.860 回答