GZipStream 和 DeflateStream 生成更大的文件

发布于 2024-09-26 07:49:14 字数 603 浏览 11 评论 0原文

我正在尝试在 C# 中使用 deflate/gzip 流,但压缩后的文件似乎比以前更大。

例如,我压缩了 900ko 的 docx 文件,但它生成了 1.4Mo 的文件!

它对我尝试过的每个文件都执行此操作。

也许我的做法是错的?这是我的代码:

  FileStream input = File.OpenRead(Environment.CurrentDirectory + "/file.docx");
  FileStream output = File.OpenWrite(Environment.CurrentDirectory + "/compressedfile.dat");

  GZipStream comp = new GZipStream(output, CompressionMode.Compress);

  while (input.Position != input.Length)
      comp.WriteByte((byte)input.ReadByte());

  input.Close();

  comp.Close(); // automatically call flush at closing
  output.Close();

I'm trying to use deflate/gzip streams in C# but it appears that the files after compression are bigger than before.

For example, I compress a docx file of 900ko, but it produce a 1.4Mo one !

And it does it for every file I tried.

May be I am wrong in the way I'm doing it? Here is my code :

  FileStream input = File.OpenRead(Environment.CurrentDirectory + "/file.docx");
  FileStream output = File.OpenWrite(Environment.CurrentDirectory + "/compressedfile.dat");

  GZipStream comp = new GZipStream(output, CompressionMode.Compress);

  while (input.Position != input.Length)
      comp.WriteByte((byte)input.ReadByte());

  input.Close();

  comp.Close(); // automatically call flush at closing
  output.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

可遇━不可求 2024-10-03 07:49:14

这么大的差异对我来说似乎很奇怪,但你应该记住,docx本身是在ZIP中压缩的,所以没有理由再次压缩它,结果通常会更大。

Such a big difference seems strange to me, but you should keep in mind that docx is itself compressed in ZIP, so there is no reason to compress it again, results usually are bigger.

过潦 2024-10-03 07:49:14

首先,与 zip、7z 等相比,deflate/gzip 流的压缩效果非常差。

其次,docx(以及所有末尾带有“x”的 MS 文档格式)无论如何都只是 .zip 文件。将 .docx 重命名为 .zip 以揭示烟雾和镜子。

因此,当您在 docx 上运行 deflate/gzip 时,它实际上会使文件变得更大。 (这就像对高压缩级别的压缩文件进行低压缩级别的 zip 操作一样。)

但是,如果您对 HTML、文本文件或未压缩的文件运行 deflate/gzip,那么它实际上会做一个漂亮的效果。好工作。

Firstly, deflate/gzip streams are remarkably bad at compression when compared to zip, 7z, etc.

Secondly, docx (and all of the MS document formats with an 'x' at the end) are just .zip files anyway. Rename a .docx to .zip to reveal the smoke and mirrors.

So when you run deflate/gzip over a docx, it will actually make the file bigger. (Its like doing a zip with a low level of compression over a zipped file with a high level of compression.)

However if you run deflate/gzip over HTML or a text file or something that is not compressed then it will actually do a pretty good job.

美煞众生 2024-10-03 07:49:14

尽管正如其他人指出的那样,您指定的示例文件确实已经被压缩 - 最大的问题是要了解,与大多数压缩实用程序不同,DeflateStreamGZipStream类只是尝试标记/压缩数据流,而不知道所有附加标记(开销)实际上会增加所需的数据量。 Zip、7z 等足够聪明,知道如果数据很大程度上是随机熵(实际上不可压缩),它们只是“按原样”存储数据(存储,不压缩),而不是尝试进一步压缩它。

Although it is true, as others have indicated, that the example files you specified are already compressed - the biggest issue is to understand that unlike most compression utilities, the DeflateStream and GZipStream classes simply try to tokenize/compress a data stream without the intelligence that all the additional tokens (overhead) are actually increasing the amount of data required. Zip, 7z, etc. are smart enough to know that if data is largely random entropy (virtually uncompressable), that they simply store the data "as-is" (store, not compressed), instead of attempting to compress it further.

等数载,海棠开 2024-10-03 07:49:14

我在压缩包含 jpg 数据的数据库时遇到了同样的问题。我尝试了 dotnetzip - 替代品下降并获得了不错的压缩(也支持紧凑框架!):

MS : 10MB -> 10.0MB
DNZ: 10MB ->  7.6MB

I had the same issue with compressing databases containing jpg data. I tried dotnetzip - a drop in replacement and got decent compression (Supports Compact Framework too!):

MS : 10MB -> 10.0MB
DNZ: 10MB ->  7.6MB
鹤舞 2024-10-03 07:49:14

我不认为 GzipStream 和 DeflateStream 旨在压缩文件。如果使用像 SharpZipLib 这样的文件压缩器,你的运气可能会更好。

I don't think GzipStream and DeflateStream are intended to compress files. You would probably have better luck with a file compressor like SharpZipLib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文