- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个连接到后端 WAS 服务器的 .NET 2.0 WinForms 应用程序。我正在使用 GZipStream 来解码从对服务器发出的 HttpWebRequest 调用返回的数据。返回的数据是 Apache 正在压缩的压缩 CSV。整个服务器堆栈是 Hibernate-->EJB-->Spring-->Apache。
对于小响应,性能很好(<50 毫秒)。当我得到一个大于 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 的调用就很快了。
我发现使用 HttpWebRequest.AutomaticDecompression 会出现同样的性能问题,所以我要关闭这个问题。
最佳答案
尝试先将数据加载到 MemoryStream 中,然后解压缩 MemoryStream...
关于c# - GZipStream解压性能差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1522040/
知道我该怎么做吗?我能够压缩单个文件。 最佳答案 您不能直接 GZip 整个文件夹,因为 GZip 对单个数据流进行操作。您首先必须将文件夹转换为这样的流。 一种方法是创建一个 Tar archive
下面这段代码有什么问题。我总是得到 FALSE,意思是压缩后,解压后的数据与原始值不匹配。 public static bool Test() { string
我正在尝试在 C# 中使用 gzip 流,但压缩后的文件似乎比以前大。当我处理 .avi 和 .mkv 文件时会发生这种情况。但是,如果我使用比原始文件小的 .txt 和 .html 压缩文件。 us
我正在尝试使用 Gzip 压缩来压缩 JSON 文件以发送到另一个位置。它每天需要处理 5,000 - 10,000 个文件,我不需要本地机器上文件的压缩版本(它们实际上正在传输到 AWS S3 以进
我的 GZipStream 只会解压缩文件的第一行。通过 7-zip 提取内容按预期工作并为我提供了整个文件内容。它还在 cygwin 和 linux 上使用 gunzip 按预期提取,所以我希望这是
打开zip文件时出现问题。我正在使用以下代码来压缩文件: public static string Zip_File(string soruce , string target) { try
我正在 Windows Phone 7 上开发一个应用程序。我需要压缩/解压缩我的数据以便通过 tcp 套接字发送它。 但我一直没能找到任何 gzip 压缩器用于通过 tcp 套接字的仅转发流。 请帮
我有应该进行压缩的代码: FileStream fs = new FileStream("g:\\gj.txt", FileMode.Open); FileStream fd = new FileSt
我在 .NET 4.0 中遇到了一些奇怪的依赖于机器/操作系统的 GZipStream 行为。这是相关代码: public static string Compress(string input) {
我正在尝试压缩大量数据,有时在 100GB 左右,当我运行我编写的例程时,它看起来文件大小与以前的大小完全相同。还有其他人遇到过 GZipStream 的这个问题吗? 我的代码如下:
我遇到了一个针对 .Net 3.5 的 gzipstream 的讨厌问题。这是我第一次使用 gzipstream,但是我已经模仿了许多教程,包括 here我仍然卡住了。 我的应用程序将数据表序列化为
我正在尝试读取文件并使用 GZipStream 压缩它,如下所示: using (var outStream = new MemoryStream()) { using (var fileSt
我需要一种将 GZipStream 写入字符串的方法。 我正在使用: GZipStream Decompress = new GZipStream(inFile, CompressionMode.De
我正在尝试将大型 UInt16 数组保存到文件中。 positionCnt大约是50000,stationCnt大约是2500。直接保存,没有GZipStream,文件大约250MB,可以通过外部zi
我有一些代码可以下载 gzip 文件并解压缩它们。问题是,我无法让它解压缩整个文件,它只读取前 4096 个字节,然后再读取大约 500 个字节。 Byte[] buffer = new Byte[4
我正在使用以下代码来压缩文件并且它工作正常但是当我使用 WinRar 解压缩时我得到没有扩展名的原始文件名,任何线索为什么如果文件名是 myReport.xls 当我解压缩我只得到 myReport
我正在使用 .NET 框架中的 GZipStream 类压缩一些数据包。一切正常,压缩率也不错,但是当我使用十六进制编辑器查看压缩数据时,我注意到每个压缩数据包中有多达三分之一是尾随零。这正常吗? 大
我正在使用 .NET 框架中的 GZipStream 类压缩一些数据包。一切正常,压缩率也不错,但是当我使用十六进制编辑器查看压缩数据时,我注意到每个压缩数据包中有多达三分之一是尾随零。这正常吗? 大
我正在尝试将非常大的对象直接序列化为 zip 流。我设法通过在中间步骤中序列化到文件流、将其加载回来然后压缩它来做到这一点。 我还尝试过直接压缩到内存流并且它有效。但是当我使用 GZipStream
我可以获取磁盘上文件的 GZipStream 而不将整个压缩内容写入临时存储吗?我目前正在磁盘上使用临时文件,以避免在非常大的文件上使用 MemoryStream 可能出现的内存耗尽(这工作正常)。
我是一名优秀的程序员,十分优秀!