Compression and decompression using the Crypto++ library






4.57/5 (8 votes)
Using the Crypto++ library to compress and decompress data
Introduction
The Crypto++ library is a freeware library of cryptographic schemes, written by Wei Dai. However the library also contains other useful classes which are not immediately apparent when you use the library. Two of these are the Gzip and Gunzip classes which can be used to compress and decompress (zip and unzip) data.
Compression
Compressing your data could not be simpler. Say you had data pData that was of size dwLen that you wished to compress.
#include <gzip.h> Gzip zipper(1); // 1 is fast, 9 is slow zipper.Put(pData,dwLen); zipper.Close(); byte* pCompressed = new byte[zipper.MaxRetrieveable()]; zipper.Get(pCompressed,zipper.MaxRetrieveable());
pCompressed now contains the compressed data.
Decompression
You may not be surprised to know that decompression is just as easy. (dwLen is now the length of our compressed data)
Gunzip unzipper;
unzipper.Put(pCompressedData,dwLen);
unzipper.Close();
byte* pData = new byte[unzipper.MaxRetrieveable()];
unzipper.Get(pData,unzipper.MaxRetrieveable());
pData now contains the uncompressed data.
Remarks
Thanks to Wei Dai for his permission to write this article in what is hoped to be a series of articles on the use of his Crypto++ library.