Click here to Skip to main content
Click here to Skip to main content

Compression and decompression using the Crypto++ library

By , 16 Apr 2001
 

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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionpassword protected archive? PinmemberMatthias Blum15 Jan '09 - 13:23 
GeneralCompressing and Decompressing Data using C++ Pinmemberjassshan24 Nov '08 - 21:39 
GeneralGzip a set of files Pinmembersheyliger7 Jul '08 - 3:21 
GeneralWell I've added header but it doesn't work PinmemberNoagene15 Aug '07 - 21:36 
GeneralClose() is No Longer Available... PinmemberJeffrey Walton27 May '07 - 15:21 
GeneralCrypto++ Integration Article PinmemberJeffrey Walton8 Dec '06 - 18:28 
GeneralGzip work verry greatly! Pinmemberdungbkhn28 Jun '05 - 17:50 
GeneralGot a wrong result! PinmemberJerryCheng11 Nov '03 - 22:44 
Questionencryption of file..help?? Pinmemberxxhimanshu11 Nov '03 - 20:40 
Generalzipping PinsussAnonymous1 Jun '03 - 8:24 
General'Close' : is not a member of 'CryptoPP::Gzip' PinmemberÅke Johansson11 Apr '03 - 19:41 
QuestionCouldn't be simpler? What are you smoking? PinmemberLarryLeonard11 Apr '03 - 11:34 
GeneralDoesn't compile in Visual C++ 7.0 Pinmemberleandrobecker27 Mar '03 - 3:04 
GeneralHaving trouble using this library :( PinmemberOmee4u19 Nov '02 - 15:20 
Questionyour code does not work ? PinmemberAnonymous23 Oct '01 - 14:46 
Questionyour code does not work ? PinmemberAnonymous23 Oct '01 - 14:45 
Generalno constructor with just 1 parameter PinmemberAnonymous14 May '01 - 2:37 
GeneralNamespace problem PinmemberSteve Harris7 Dec '00 - 22:50 

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

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