zlib:如何确定avail_out的尺寸

发布于 2024-10-16 07:57:23 字数 285 浏览 7 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(1

寂寞清仓 2024-10-23 07:57:23

zlib 有一个函数可以计算缓冲区缩小到的最大大小。您的假设是正确的 - 返回的值是输入缓冲区的大小 + 标头大小。紧缩后,您可以重新分配缓冲区以回收“浪费”的内存。
来自 zlib.h:

ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, uLong sourceLen));
/*
   deflateBound() returns an upper bound on the compressed size after
   deflation of sourceLen bytes.  It must be called after deflateInit() or
   deflateInit2(), and after deflateSetHeader(), if used.  This would be used
   to allocate an output buffer for deflation in a single pass, and so would be
   called before deflate().
*/

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:

ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, uLong sourceLen));
/*
   deflateBound() returns an upper bound on the compressed size after
   deflation of sourceLen bytes.  It must be called after deflateInit() or
   deflateInit2(), and after deflateSetHeader(), if used.  This would be used
   to allocate an output buffer for deflation in a single pass, and so would be
   called before deflate().
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文