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
{
}
答案 0 :(得分: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
块也不错。他们释放了非托管资源,而不是让它们漂浮,直到垃圾收集器拿起它们。