Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zlib: Differences Between the `deflate` and `compress` Functions

Tags:

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?

like image 995
mma1480 Avatar asked Apr 15 '12 21:04

mma1480


People also ask

What is zlib deflate?

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.

Is zlib the same as gzip?

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 .

Is deflate same as gzip?

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.


1 Answers

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.

like image 122
Mark Adler Avatar answered Oct 26 '22 23:10

Mark Adler