Click here to Skip to main content
12,069,536 members (61,383 online)
Click here to Skip to main content
Add your own
alternative version

Stats

200K views
2.3K downloads
47 bookmarked
Posted

Compression and decompression using the Crypto++ library

, 16 Apr 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Shaun Wilde
Software Developer (Senior) MYOB
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.

You may also be interested in...

Comments and Discussions

 
Questionpassword protected archive? Pin
Matthias Blum15-Jan-09 14:23
memberMatthias Blum15-Jan-09 14:23 
AnswerRe: password protected archive? Pin
Shaun Wilde16-Jan-09 1:06
memberShaun Wilde16-Jan-09 1:06 
GeneralCompressing and Decompressing Data using C++ Pin
jassshan24-Nov-08 22:39
memberjassshan24-Nov-08 22:39 
GeneralGzip a set of files Pin
sheyliger7-Jul-08 4:21
membersheyliger7-Jul-08 4:21 
GeneralRe: Gzip a set of files Pin
Shaun Wilde7-Jul-08 9:35
memberShaun Wilde7-Jul-08 9:35 
GeneralWell I've added header but it doesn't work Pin
Noagene15-Aug-07 22:36
memberNoagene15-Aug-07 22:36 
GeneralRe: Well I've added header but it doesn't work Pin
Shaun Wilde19-Aug-07 0:28
memberShaun Wilde19-Aug-07 0:28 
GeneralRe: Well I've added header but it doesn't work Pin
Noagene19-Aug-07 14:38
memberNoagene19-Aug-07 14:38 
GeneralError code has been changed.. Pin
Noagene19-Aug-07 19:55
memberNoagene19-Aug-07 19:55 
AnswerRe: Error code has been changed.. Pin
butuzov_Ilya6-Sep-07 23:27
memberbutuzov_Ilya6-Sep-07 23:27 
GeneralClose() is No Longer Available... Pin
Jeffrey Walton27-May-07 16:21
memberJeffrey Walton27-May-07 16:21 
GeneralRe: Close() is No Longer Available... Pin
Shaun Wilde28-May-07 11:04
memberShaun Wilde28-May-07 11:04 
GeneralRe: Close() is No Longer Available... Pin
Jeffrey Walton28-May-07 12:03
memberJeffrey Walton28-May-07 12:03 
GeneralRe: Close() is No Longer Available... Pin
Shaun Wilde29-May-07 20:55
memberShaun Wilde29-May-07 20:55 
GeneralRe: Close() is No Longer Available... Pin
Jeffrey Walton28-May-07 12:08
memberJeffrey Walton28-May-07 12:08 
GeneralRe: Close() is No Longer Available... Pin
Shaun Wilde29-May-07 20:56
memberShaun Wilde29-May-07 20:56 
GeneralCrypto++ Integration Article Pin
Jeffrey Walton8-Dec-06 19:28
memberJeffrey Walton8-Dec-06 19:28 
GeneralGzip work verry greatly! Pin
dungbkhn28-Jun-05 18:50
memberdungbkhn28-Jun-05 18:50 
GeneralGot a wrong result! Pin
JerryCheng11-Nov-03 23:44
memberJerryCheng11-Nov-03 23:44 
GeneralRe: Got a wrong result! Pin
Shaun Wilde12-Nov-03 9:13
memberShaun Wilde12-Nov-03 9:13 
GeneralRe: Got a wrong result! Pin
JerryCheng13-Nov-03 17:59
memberJerryCheng13-Nov-03 17:59 
GeneralRe: Got a wrong result! Pin
Shaun Wilde14-Nov-03 5:43
memberShaun Wilde14-Nov-03 5:43 
Questionencryption of file..help?? Pin
xxhimanshu11-Nov-03 21:40
memberxxhimanshu11-Nov-03 21:40 
AnswerRe: encryption of file..help?? Pin
Shaun Wilde12-Nov-03 9:17
memberShaun Wilde12-Nov-03 9:17 
Generalzipping Pin
Anonymous1-Jun-03 9:24
sussAnonymous1-Jun-03 9:24 
GeneralRe: zipping Pin
Shaun Wilde1-Jun-03 11:08
memberShaun Wilde1-Jun-03 11:08 
General'Close' : is not a member of 'CryptoPP::Gzip' Pin
Åke Johansson11-Apr-03 20:41
memberÅke Johansson11-Apr-03 20:41 
GeneralRe: 'Close' : is not a member of 'CryptoPP::Gzip' Pin
Shaun Wilde13-Apr-03 21:32
memberShaun Wilde13-Apr-03 21:32 
GeneralRe: 'Close' : is not a member of 'CryptoPP::Gzip' Pin
Åke Johansson14-Apr-03 21:29
memberÅke Johansson14-Apr-03 21:29 
GeneralRe: 'Close' : is not a member of 'CryptoPP::Gzip' Pin
Shaun Wilde14-Apr-03 21:47
memberShaun Wilde14-Apr-03 21:47 
GeneralRe: 'Close' : is not a member of 'CryptoPP::Gzip' Pin
Åke Johansson15-Apr-03 2:41
memberÅke Johansson15-Apr-03 2:41 
GeneralRe: 'Close' : is not a member of 'CryptoPP::Gzip' Pin
Shaun Wilde15-Apr-03 3:25
memberShaun Wilde15-Apr-03 3:25 
QuestionCouldn't be simpler? What are you smoking? Pin
LarryLeonard11-Apr-03 12:34
memberLarryLeonard11-Apr-03 12:34 
AnswerRe: Couldn't be simpler? What are you smoking? Pin
Shaun Wilde13-Apr-03 21:41
memberShaun Wilde13-Apr-03 21:41 
GeneralRe: Couldn't be simpler? What are you smoking? Pin
LarryLeonard14-Apr-03 4:26
memberLarryLeonard14-Apr-03 4:26 
GeneralRe: Couldn't be simpler? What are you smoking? Pin
Shaun Wilde14-Apr-03 21:55
memberShaun Wilde14-Apr-03 21:55 
GeneralRe: Couldn't be simpler? What are you smoking? Pin
LarryLeonard15-Apr-03 6:06
memberLarryLeonard15-Apr-03 6:06 
GeneralDoesn't compile in Visual C++ 7.0 Pin
leandrobecker27-Mar-03 4:04
memberleandrobecker27-Mar-03 4:04 
GeneralRe: Doesn't compile in Visual C++ 7.0 Pin
Shaun Wilde27-Mar-03 22:05
memberShaun Wilde27-Mar-03 22:05 
GeneralHaving trouble using this library :( Pin
Omee4u19-Nov-02 16:20
memberOmee4u19-Nov-02 16:20 
GeneralRe: Having trouble using this library :( Pin
Shaun Wilde19-Nov-02 23:06
memberShaun Wilde19-Nov-02 23:06 
GeneralRe: Having trouble using this library :( Pin
Omee4u3-Dec-02 5:16
memberOmee4u3-Dec-02 5:16 
GeneralRe: Having trouble using this library :( Pin
Shaun Wilde3-Dec-02 12:30
memberShaun Wilde3-Dec-02 12:30 
Questionyour code does not work ? Pin
Anonymous23-Oct-01 15:46
memberAnonymous23-Oct-01 15:46 
AnswerRe: your code does not work ? Pin
SAWilde23-Oct-01 23:08
memberSAWilde23-Oct-01 23:08 
GeneralRe: your code does not work ? Pin
Anonymous25-Oct-01 1:34
memberAnonymous25-Oct-01 1:34 
GeneralRe: your code does not work ? Pin
Anonymous25-Oct-01 16:08
memberAnonymous25-Oct-01 16:08 
GeneralRe: your code does not work ? Pin
SAWilde29-Oct-01 12:57
memberSAWilde29-Oct-01 12:57 
Questionyour code does not work ? Pin
Anonymous23-Oct-01 15:45
memberAnonymous23-Oct-01 15:45 
Generalno constructor with just 1 parameter Pin
Anonymous14-May-01 3:37
memberAnonymous14-May-01 3:37 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160208.1 | Last Updated 17 Apr 2001
Article Copyright 2000 by Shaun Wilde
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid