我刚刚开始使用以下代码在 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
我尝试过使用多种缓冲区大小,但得到了相似的压缩时间和完全相同的压缩率。
最佳答案
编辑,或实际重写:看起来 BCL 编码人员决定 phone it in .
System.dll
2.0 版中的实现使用 statically defined, hardcoded Huffman trees针对纯 ASCII 文本进行了优化,而不是像其他实现那样自适应地生成霍夫曼树。它还不支持存储 block 优化(这是标准 GZip/Deflate 避免失控扩展的方式)。因此,通过其实现运行除纯文本以外的任何类型的文件都会产生比输入大得多的文件,Microsoft claims this is by design!
为自己省去一些痛苦,捕获一个third party implementation .
关于.net - IO.Compression 有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975182/