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?
zlib (/ˈziːlɪb/ or "zeta-lib", /ˈziːtəˌlɪb/) is a software library used for data compression. zlib was written by Jean-loup Gailly and Mark Adler and is an abstraction of the DEFLATE compression algorithm used in their gzip file compression program.
The main difference between zlib and gzip wrapping is that the zlib wrapping is more compact, six bytes vs. a minimum of 18 bytes for gzip, and the integrity check, Adler-32, runs faster than the CRC-32 that gzip uses. Raw deflate is used by programs that read and write the .
It is one of the three standard formats for HTTP compression as specified in RFC 2616. This RFC also specifies a zlib format (called "DEFLATE"), which is equal to the gzip format except that gzip adds eleven bytes of overhead in the form of headers and trailers.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With