zlib:如何确定avail_out的尺寸
我想使用 zlib 缩小一小块内存(<= 16 KiB)。输出也存储在内存块中。这里没有磁盘或数据库访问权限。
根据文档,我应该重复调用 deflate()
直到整个输入被放气。在这之间,我必须增加输出所在的内存块的大小。
然而,这似乎不必要地复杂,甚至可能效率低下。我知道输入的大小,难道我不能预先确定输出所需的最大大小,然后只需调用一次 deflate()
即可完成所有操作吗?
如果是这样,最大输出大小是多少?我假设类似:输入大小+一些字节开销
I would like to deflate a small block of memory (<= 16 KiB) using zlib. The output is stored in a block of memory as well. No disk or database access here.
According to the documentation, I should call deflate()
repeatedly until the whole input is deflated. In between, I have to increase the size of the memory block where the output goes.
However, that seems unnecessarily complicated and perhaps even inefficient. As I know the size of the input, can't I predetermine the maximum size needed for the output, and then do everything with just one call to deflate()
?
If so, what is the maximum output size? I assume something like: size of input + some bytes overhead
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
zlib 有一个函数可以计算缓冲区缩小到的最大大小。您的假设是正确的 - 返回的值是输入缓冲区的大小 + 标头大小。紧缩后,您可以重新分配缓冲区以回收“浪费”的内存。
来自 zlib.h:
zlib has a function to calculate the maximum size a buffer will deflate to. Your assumption is correct - the returned value is the size of the input buffer + header sizes. After deflation you can realloc the buffer to reclaim the 'wasted' memory.
From zlib.h: