Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress a buffer with zlib?

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 data stored in a buffer in memory. I don't want to save the compressed buffer to disk either.

Basically here is my buffer:

fIplImageHeader->imageData = (char*)imageIn->getFrame(); 

How can I compress it with zlib?

I would appreciate some code example of how to do that.

like image 578
Richard Knop Avatar asked Dec 27 '10 12:12

Richard Knop


People also ask

How does zlib compress work?

zlib compressed data are typically written with a gzip or a zlib wrapper. The wrapper encapsulates the raw DEFLATE data by adding a header and trailer. This provides stream identification and error detection that are not provided by the raw DEFLATE data.

Can zlib decompress gzip?

To decompress a gzip format file with zlib, call inflateInit2 with the windowBits parameter as 16+MAX_WBITS , like this: inflateInit2(&stream, 16+MAX_WBITS); If you don't do this, zlib will complain about a bad stream format.

Does gzip use zlib?

zlib was adapted from the gzip code. All of the mentioned patents have since expired. The zlib library supports Deflate compression and decompression, and three kinds of wrapping around the deflate streams.


1 Answers

zlib.h has all the functions you need: compress (or compress2) and uncompress. See the source code of zlib for an answer.

ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen, const Bytef *source, uLong sourceLen)); /*          Compresses the source buffer into the destination buffer.  sourceLen is      the byte length of the source buffer.  Upon entry, destLen is the total size      of the destination buffer, which must be at least the value returned by      compressBound(sourceLen).  Upon exit, destLen is the actual size of the      compressed buffer.           compress returns Z_OK if success, Z_MEM_ERROR if there was not      enough memory, Z_BUF_ERROR if there was not enough room in the output      buffer. */  ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen, const Bytef *source, uLong sourceLen)); /*          Decompresses the source buffer into the destination buffer.  sourceLen is      the byte length of the source buffer.  Upon entry, destLen is the total size      of the destination buffer, which must be large enough to hold the entire      uncompressed data.  (The size of the uncompressed data must have been saved      previously by the compressor and transmitted to the decompressor by some      mechanism outside the scope of this compression library.) Upon exit, destLen      is the actual size of the uncompressed buffer.           uncompress returns Z_OK if success, Z_MEM_ERROR if there was not      enough memory, Z_BUF_ERROR if there was not enough room in the output      buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.  In      the case where there is not enough room, uncompress() will fill the output      buffer with the uncompressed data up to that point. */ 
like image 92
Huiwei Avatar answered Oct 05 '22 22:10

Huiwei