IO.压缩有问题吗?

发布于 2024-10-17 13:48:41 字数 1002 浏览 4 评论 0原文

我刚刚开始使用以下代码在 VB.Net 中压缩文件。由于我的目标是 Fx 2.0,因此无法使用 Stream.CopyTo 方法。

然而,与 7-zip 中的 gzip Normal 压缩配置文件相比,我的代码给出的结果非常差。例如,我的代码将 630MB 的 Outlook 存档压缩为 740MB,7-zip 将其压缩为 490MB。

这是代码。是否有一个明显的错误(或很多?)

Using Input As New IO.FileStream(SourceFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    Using outFile As IO.FileStream = IO.File.Create(DestFile)
        Using Compress As IO.Compression.GZipStream = New IO.Compression.GZipStream(outFile, IO.Compression.CompressionMode.Compress)
            'TODO: Figure out the right buffer size.'
            Dim Buffer(524228) As Byte
            Dim ReadBytes As Integer = 0

            While True
                ReadBytes = Input.Read(Buffer, 0, Buffer.Length)
                If ReadBytes <= 0 Then Exit While
                Compress.Write(Buffer, 0, ReadBytes)
            End While
        End Using
    End Using
End Using

我尝试过使用多个缓冲区大小,但我得到了相似的压缩时间和完全相同的压缩比。

I've just started compressing file in VB.Net, using the following code. Since I'm targeting Fx 2.0, I can't use the Stream.CopyTo method.

My code, however, gives extremely poor results compared to the gzip Normal compression profile in 7-zip. For example, my code compressed a 630MB outlook archive to 740MB, and 7-zip makes it 490MB.

Here is the code. Is there a blatant mistake (or many?)

Using Input As New IO.FileStream(SourceFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    Using outFile As IO.FileStream = IO.File.Create(DestFile)
        Using Compress As IO.Compression.GZipStream = New IO.Compression.GZipStream(outFile, IO.Compression.CompressionMode.Compress)
            'TODO: Figure out the right buffer size.'
            Dim Buffer(524228) As Byte
            Dim ReadBytes As Integer = 0

            While True
                ReadBytes = Input.Read(Buffer, 0, Buffer.Length)
                If ReadBytes <= 0 Then Exit While
                Compress.Write(Buffer, 0, ReadBytes)
            End While
        End Using
    End Using
End Using

I've tried with multiple buffer sizes, but I get similar compression times, and exactly the same compression ratio.

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

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

发布评论

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

评论(3

﹏半生如梦愿梦如真 2024-10-24 13:48:41

编辑,或者实际重写:看起来 BCL 编码人员决定 打电话

System.dll 版本 2.0 中的实现静态地使用 定义的、硬编码的霍夫曼树针对纯 ASCII 文本进行了优化,而不是像其他实现那样自适应地生成霍夫曼树。它也不支持存储块优化(这是标准 GZip/Deflate 避免失控扩展的方式)。因此,通过其实现运行除纯文本之外的任何类型的文件都会产生比输入大得多的文件,并且 Microsoft 声称这是设计使然!

为了避免一些痛苦,请使用 Sharpdevelop.net/OpenSource/SharpZipLib/" rel="noreferrer">第三方实现

EDIT, or actually rewrite: It looks like the BCL coders decided to phone it in.

The implementation in System.dll version 2.0 uses statically defined, hardcoded Huffman trees optimized for plain ASCII text, rather than adaptively generating the Huffman trees as other implementations do. It also doesn't support stored-block optimization (which is how standard GZip/Deflate avoid runaway expansion). As a result, running any sort of file through their implementation other than plain text will result in a much larger file than the input, and Microsoft claims this is by design!

Save yourself some pain, grab a third party implementation.

二货你真萌 2024-10-24 13:48:41

IO.Compression 并不是真正为我们设计的。它的创建是为了支持 XPS 或 XML 纸张规范。目前,如果您想要适当的文件压缩,则必须使用第三方库。

IO.Compression wasn't really made for us. It was created the support the XPS or XML Paper Specificatin. Currently you have to use a third party library if you want decent file compression.

爱你是孤单的心事 2024-10-24 13:48:41

一些可能有用的附加信息。
我正在压缩一些静态文件(二进制)以包含在项目版本中,并且遇到了同样的问题,即文件大小随着 IO. Compression.GZipStream 的增加而增加。

我决定使用 Ionic.Zip 来代替可以使用最佳压缩的地方。

我立即注意到的一件事是,尽管 Ionic.Zip 将我的文件减少到原始大小的 25%,但压缩操作速度慢了大约 3-4 倍(完全符合预期),但解压缩过程也慢了 3 倍,这使得解压缩速度慢了 3 倍。 1.6 秒与 0.5 秒相比。

由于 GZipStream 是一个标准,尽管 .NET 中内置的 IO.Compression.GZipStream 的压缩空间效率要低得多,但解压缩速度要快得多。

因此,我使用两者 Ionic.Zip 库“ZLib.GZipStream”来压缩文件和“IO. Compression.GZipStream” strong>”以在生产中更快地解压缩文件。

Some additional information that may be useful.
I was compressing some static files (binary) to include in a project release and had the same issue where the file size increased with IO.Compression.GZipStream.

I decided to use Ionic.Zip instead where the best compression could be used.

One thing I noticed immediately is that even though Ionic.Zip reduced my files to 25% of there original size the Compressing Action was about 3-4 times slower (totally expected) but the unzip process was also 3 times slower which made the decompress take 1.6 seconds compared to 0.5 seconds.

Since the GZipStream is a standard, even though the built in IO.Compression.GZipStream in .NET was far less space efficient compressing, it was far faster decompressing.

So I use both Ionic.Zip Librarys "ZLib.GZipStream" to Compress the files and "IO.Compression.GZipStream" to Decompress the files much faster in production.

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