I am using zlib to compress a stream of text data. The text data comes in chunks, and for each chunk, deflate()
is called, with flush set to Z_NO_FLUSH
. Once all chunks have been retrieved, deflate()
is called with flush set to Z_FINISH
.
Naturally, deflate()
doesn't produce compressed output on each call. It internally accumulates data to achieve a high compression rate. And that's fine! Every time deflate()
produces compressed output, that output is appended to a database field - a slow process.
However, once deflate()
produces compressed data, that data may not fit into the provided output buffer, deflate_out
. Therefore several calls to deflate()
are required. And that is what I want to avoid:
Is there a way to make
deflate_out
always large enough so thatdeflate()
can store all the compressed data in it, every times it decides to produce output?
Notes:
The total size of the uncompressed data is not known beforehand. As mentioned above, the uncompressed data comes in chunks, and the compressed data is appended to a database field, also in chunks.
In the include file
zconf.h
I have found the following comment. Is that perhaps what I am looking for? I.e. is(1 << (windowBits+2)) + (1 << (memLevel+9))
the maximum size in bytes of compressed data thatdeflate()
may produce?/* The memory requirements for deflate are (in bytes): (1 << (windowBits+2)) + (1 << (memLevel+9)) that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) plus a few kilobytes for small objects. For example, if you want to reduce the default memory requirements from 256K to 128K, compile with make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" Of course this will generally degrade compression (there's no free lunch). The memory requirements for inflate are (in bytes) 1 << windowBits that is, 32K for windowBits=15 (default value) plus a few kilobytes for small objects. */