What are the differences between the deflate()
and compress()
functions in zlib?
I have looked through online examples and some used deflate while others used compress.
How should I decide which situation I would use one over the other?
compress()
is used to compress the data in a single call, and always compresses to the zlib format, which is deflate data with a two-byte header and a four-byte check value trailer. compress()
is used by itself.
deflate()
is used to compress data a chunk at a time, and/or to compress to other formats such as gzip-wrapped or raw, and with other options, such as memory levels and compression strategies.
You would use compress()
if you have all the data available at once and enough memory to hold the result, and you want the default compression format, memory usage, and strategy. Otherwise, you would use deflate()
.
deflate()
is not used by itself. You need to use deflateInit()
or deflateInit2()
to initialize the z_stream
structure used by deflate()
. Then you call deflate()
one or more times to take in data to compress and to make available the result. At the end, deflateEnd()
is called to free the memory resources used in the structure. You can read the documentation in zlib.h and at http://zlib.net/zlib_how.html for more information.