GZipStream的减压性能很差

我有一个连接到后端WAS服务器的.NET 2.0 WinForms应用程序。 我正在使用GZipStream来解码从对服务器发出的HttpWebRequest调用返回的数据。 返回的数据是压缩的CSV,Apache正在压缩。 整个服务器堆栈是Hibernate – > EJB – > Spring – > Apache。

对于小响应,性能很好( 150KB的响应时,解压缩需要60多秒。 大部分时间似乎花在GZipStream构造函数中。

这是显示我从HttpWebResponse调用获取响应流的位置的代码:

using (Stream stream = this.Response.GetResponseStream()) { if (this.CompressData && this.Response.ContentEncoding == "gzip") { // Decompress the response byte[] b = Decompress(stream); this.ResponseBody = encoding.GetString(b); } else { // Just read the stream as a string using (StreamReader sr = new StreamReader(stream)) { this.ResponseBody = sr.ReadToEnd(); } } } 

编辑1

根据Lucero的评论,我将Decompress方法修改为以下内容,但在实例化GZipStream之前,我没有看到将ResponseStream加载到MemoryStream中的任何性能优势。

 private static byte[] Decompress(Stream stream) { using (MemoryStream ms = new MemoryStream()) { byte[] buffer = new byte[4096]; int read = 0; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } ms.Seek(0, SeekOrigin.Begin); using (GZipStream gzipStream = new GZipStream(ms, CompressionMode.Decompress, false)) { read = 0; buffer = new byte[4096]; using (MemoryStream output = new MemoryStream()) { while ((read = gzipStream.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } return output.ToArray(); } } } } 

根据上面的代码,任何人都可以看到任何问题? 这对我来说似乎很基础,但它让我疯狂。

编辑2

我使用ANTS Profiler对应用程序进行了分析,在60年代的解压缩过程中,CPU接近零,内存使用率没有变化。

编辑3

实际的减速似乎是在阅读期间

  this.Response.GetResponseStream 

整个60秒用于将响应流加载到MemoryStream中。 一旦它在那里,对GZipStream的调用很快。
编辑4

我发现使用HttpWebRequest.AutomaticDecompression表现出相同的性能问题,所以我正在关闭这个问题。

首先尝试将数据加载到MemoryStream中,然后解压缩MemoryStream …

DotNetZip有一个GZipStream类,可以用作System.IO.Compression.GZipStream的替代品。

DotNetZip是免费的。

注意:如果您只是在做GZipStream,那么您需要Ionic.Zlib.dll,而不是Ionic.Zip.dll。

很抱歉没有直接回答你的问题,但你看过SharpZip了吗? 我发现它比Gzip更容易使用。 如果您在解决当前问题时遇到问题,也许它可以更好地执行任务。

http://www.icsharpcode.net/OpenSource/SharpZipLib/

我将三美分投入到主题中,只是为了通知C#-users 7Zip似乎在普通C#中暴露其API。 我认为你们都非常了解7Zip工具,至少对我而言,不管它的API设计得有多好或多么糟糕—知道这对于处理ZIP文件/流的更好性能而言是一个很大的帮助。

参考: http : //www.splinter.com.au/compressing-using-the-7zip-lzma-algorithm-in/