Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Compression and decompression using the Crypto++ library

0.00/5 (No votes)
16 Apr 2001 1  
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.

Latest versions

The latest versions of the Crypto++ library can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here