There is a usage example at the zlib website: http://www.zlib.net/zlib_how.html
However in the example they are compressing a file. I would like to compress a binary
You can easily adapt the example by replacing fread()
and fwrite()
calls with direct pointers to your data. For zlib compression (referred to as deflate as you "take out all the air of your data") you allocate z_stream
structure, call deflateInit()
and then:
next_in
with the next chunk of data you want to compressavail_in
to the number of bytes available in next_in
next_out
to where the compressed data should be written which should usually be a pointer inside your buffer that advances as you go alongavail_out
to the number of bytes available in next_out
deflate
avail_out
is non-zero (i.e. there's more room in the output buffer than zlib needs - no more data to write)Eventually you call deflateEnd()
and you're done.
You're basically feeding it chunks of input and output until you're out of input and it is out of output.