Click here to Skip to main content
15,887,027 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 239.1K   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

 
Questionpassword protected archive? Pin
Matthias Blum15-Jan-09 13:23
Matthias Blum15-Jan-09 13:23 
AnswerRe: password protected archive? Pin
Shaun Wilde16-Jan-09 0:06
Shaun Wilde16-Jan-09 0:06 
GeneralCompressing and Decompressing Data using C++ Pin
jassshan24-Nov-08 21:39
jassshan24-Nov-08 21:39 
GeneralGzip a set of files Pin
sheyliger7-Jul-08 3:21
sheyliger7-Jul-08 3:21 
GeneralRe: Gzip a set of files Pin
Shaun Wilde7-Jul-08 8:35
Shaun Wilde7-Jul-08 8:35 
GeneralWell I've added header but it doesn't work Pin
Noagene15-Aug-07 21:36
Noagene15-Aug-07 21:36 
GeneralRe: Well I've added header but it doesn't work Pin
Shaun Wilde18-Aug-07 23:28
Shaun Wilde18-Aug-07 23:28 
GeneralRe: Well I've added header but it doesn't work Pin
Noagene19-Aug-07 13:38
Noagene19-Aug-07 13:38 
GeneralError code has been changed.. Pin
Noagene19-Aug-07 18:55
Noagene19-Aug-07 18:55 
AnswerRe: Error code has been changed.. Pin
butuzov_Ilya6-Sep-07 22:27
butuzov_Ilya6-Sep-07 22:27 
GeneralClose() is No Longer Available... Pin
Jeffrey Walton27-May-07 15:21
Jeffrey Walton27-May-07 15:21 
GeneralRe: Close() is No Longer Available... Pin
Shaun Wilde28-May-07 10:04
Shaun Wilde28-May-07 10:04 
GeneralRe: Close() is No Longer Available... Pin
Jeffrey Walton28-May-07 11:03
Jeffrey Walton28-May-07 11:03 
GeneralRe: Close() is No Longer Available... Pin
Shaun Wilde29-May-07 19:55
Shaun Wilde29-May-07 19:55 
GeneralRe: Close() is No Longer Available... Pin
Jeffrey Walton28-May-07 11:08
Jeffrey Walton28-May-07 11:08 
GeneralRe: Close() is No Longer Available... Pin
Shaun Wilde29-May-07 19:56
Shaun Wilde29-May-07 19:56 
GeneralCrypto++ Integration Article Pin
Jeffrey Walton8-Dec-06 18:28
Jeffrey Walton8-Dec-06 18:28 
GeneralGzip work verry greatly! Pin
dungbkhn28-Jun-05 17:50
dungbkhn28-Jun-05 17:50 
GeneralGot a wrong result! Pin
JerryCheng11-Nov-03 22:44
JerryCheng11-Nov-03 22:44 
GeneralRe: Got a wrong result! Pin
Shaun Wilde12-Nov-03 8:13
Shaun Wilde12-Nov-03 8:13 
GeneralRe: Got a wrong result! Pin
JerryCheng13-Nov-03 16:59
JerryCheng13-Nov-03 16:59 
GeneralRe: Got a wrong result! Pin
Shaun Wilde14-Nov-03 4:43
Shaun Wilde14-Nov-03 4:43 
Questionencryption of file..help?? Pin
xxhimanshu11-Nov-03 20:40
xxhimanshu11-Nov-03 20:40 
AnswerRe: encryption of file..help?? Pin
Shaun Wilde12-Nov-03 8:17
Shaun Wilde12-Nov-03 8:17 
Generalzipping Pin
Anonymous1-Jun-03 8:24
Anonymous1-Jun-03 8:24 

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.