Two Classes for Doing Gzip in Memory

Environment: OS independent

Now, many applications need API to do Gzip in memory. The Zlib form, http://www.gzip.org/zlib, provides Gzip and unGzip function, but they are for files. It’s very inconvenient to compress and decompress small data that could be compressed in memory. I ported the Gzip and unGzip functions to two template classes, CA2GZIP and CGZIP2A, to do Gzip in memory; they also allocate and deallocate memory automatically. It works with zlib from http://www.gzip.org/zlib. The following is some code for how to use them.


#include “GZipHelper.h”
void main()
{
char plainText[]=”Plain text here”;
CA2GZIP gzip(plainText,strlen(plainText));
// do compressing here;
LPGZIP pgzip=gzip.pgzip; // pgzip is zipped data pointer,
// you can use it directly

int len=gzip.Length; // Length is length of zipped
// data;

CGZIP2A plain(pgzip,len); // do uncompressing here

char *pplain=plain.psz; // psz is plain data pointer
int aLen=plain.Length; // Length is length of unzipped
// data.

}

Downloads


Download demo project – 131 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read