gpt4 book ai didi

.net - 什么会导致 GZipStream 压缩数据过多地填充零?

转载 作者:行者123 更新时间:2023-12-02 01:07:05 25 4
gpt4 key购买 nike

我正在使用 .NET 框架中的 GZipStream 类压缩一些数据包。一切正常,压缩率也不错,但是当我使用十六进制编辑器查看压缩数据时,我注意到每个压缩数据包中有多达三分之一是尾随零。这正常吗?

大概 GZipStream 是一个基于 block 的压缩器并且输出被填充以与一些 block 大小对齐?是否有一些同样稳定、得到支持但没有这个问题的替代品? (我认为通过使用不会过度填充的类似压缩算法,我可以再获得 10-30% 的压缩率)。

最佳答案

使用 GZipStream 不应添加过多的尾随零。

但是如果你使用 MemoryStream不正确,它会导致这种效果。它在内部使用 byte[] 来存储数据。这个内部缓冲区可以比目前写入的数据大,以减少分配次数。如果你使用 GetBuffer()你得到了完整的数组,你自己有责任只使用它的第一个 Length 字节。

或者您可以调用 ToArray()它返回一个新的字节数组,正好包含 Length 个字节。

引用 GetBuffer() 的文档:

Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.

关于.net - 什么会导致 GZipStream 压缩数据过多地填充零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21291263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com