Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C++
Article

Compression and decompression using the Crypto++ library

Rate me:
Please Sign up or sign in to vote.
4.57/5 (8 votes)
16 Apr 2001CPOL 238.5K   2.5K   49   54
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Employed (other) Purplebricks
Australia Australia
All articles are supplied as-is, as a howto on a particular task that worked for me in the past. None of the articles are supposed to be out-of-the-box freeware controls and nor should they be treated as such. Caveat emptor.

Now living and working in Australia, trying to be involved in the local .NET and Agile communities when I can.

I spend a good chunk of my spare time building OpenCover and maintaining PartCover both of which are Code Coverage utilities for .NET.

Comments and Discussions

 
GeneralRe: 'Close' : is not a member of 'CryptoPP::Gzip' Pin
Shaun Wilde15-Apr-03 2:25
Shaun Wilde15-Apr-03 2:25 
QuestionCouldn't be simpler? What are you smoking? Pin
LarryLeonard11-Apr-03 11:34
LarryLeonard11-Apr-03 11:34 
AnswerRe: Couldn't be simpler? What are you smoking? Pin
Shaun Wilde13-Apr-03 20:41
Shaun Wilde13-Apr-03 20:41 
GeneralRe: Couldn't be simpler? What are you smoking? Pin
LarryLeonard14-Apr-03 3:26
LarryLeonard14-Apr-03 3:26 
GeneralRe: Couldn't be simpler? What are you smoking? Pin
Shaun Wilde14-Apr-03 20:55
Shaun Wilde14-Apr-03 20:55 
GeneralRe: Couldn't be simpler? What are you smoking? Pin
LarryLeonard15-Apr-03 5:06
LarryLeonard15-Apr-03 5:06 
GeneralDoesn't compile in Visual C++ 7.0 Pin
leandrobecker27-Mar-03 3:04
leandrobecker27-Mar-03 3:04 
GeneralRe: Doesn't compile in Visual C++ 7.0 Pin
Shaun Wilde27-Mar-03 21:05
Shaun Wilde27-Mar-03 21:05 
you may need to get the latest library from www.cryptocpp.com[^]

Technically speaking the dictionary would define Visual Basic users as programmers.
But here again, a very generalized, liberal definition is being employed and it's wrong
- just plain wrong - Tom Archer 5/12/02


GeneralHaving trouble using this library :( Pin
Omee4u19-Nov-02 15:20
Omee4u19-Nov-02 15:20 
GeneralRe: Having trouble using this library :( Pin
Shaun Wilde19-Nov-02 22:06
Shaun Wilde19-Nov-02 22:06 
GeneralRe: Having trouble using this library :( Pin
Omee4u3-Dec-02 4:16
Omee4u3-Dec-02 4:16 
GeneralRe: Having trouble using this library :( Pin
Shaun Wilde3-Dec-02 11:30
Shaun Wilde3-Dec-02 11:30 
Questionyour code does not work ? Pin
23-Oct-01 14:46
suss23-Oct-01 14:46 
AnswerRe: your code does not work ? Pin
SAWilde23-Oct-01 22:08
SAWilde23-Oct-01 22:08 
GeneralRe: your code does not work ? Pin
25-Oct-01 0:34
suss25-Oct-01 0:34 
GeneralRe: your code does not work ? Pin
25-Oct-01 15:08
suss25-Oct-01 15:08 
GeneralRe: your code does not work ? Pin
SAWilde29-Oct-01 11:57
SAWilde29-Oct-01 11:57 
Questionyour code does not work ? Pin
23-Oct-01 14:45
suss23-Oct-01 14:45 
Generalno constructor with just 1 parameter Pin
14-May-01 2:37
suss14-May-01 2:37 
GeneralRe: no constructor with just 1 parameter Pin
8-Jun-01 22:27
suss8-Jun-01 22:27 
GeneralRe: no constructor with just 1 parameter Pin
SAWilde23-Oct-01 22:07
SAWilde23-Oct-01 22:07 
GeneralNamespace problem Pin
7-Dec-00 22:50
suss7-Dec-00 22:50 
GeneralRe: Namespace problem Pin
SAWilde8-Dec-00 0:58
SAWilde8-Dec-00 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.