Click here to Skip to main content
Licence CPOL
First Posted 20 Nov 2000
Views 176,665
Bookmarked 44 times

Compression and decompression using the Crypto++ library

By | 16 Apr 2001 | Article
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)

About the Author

Shaun Wilde

Architect

United Kingdom United Kingdom

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.
 
Been involved in programming from the early '80s. First on my Spectrum, then an Amstrad. Did lots of Fortran while at University before becoming involved in Forth, Prolog, Pascal, Occam, C and eventually C++. Eventually started programming on the Windows platform using Borland’s OWL framework, during my postgraduate years. When I started work in '94, I learnt MFC followed by COM and ATL and never looked back. Now working exclusively in .NET, working on WinForms, ASP.NET and the Compact Framework using C# and VB.NET. Using every bit of the .NET framework I can such as WebServices and writing my own controls for my .NET Portal, and for the Compact Framework. Still lots I don't know, but the end of the universe hasn't happened yet - I wonder if I'll have time?
 
I was a permanent employee for a number of companies until Dec 2000 before going independent after the DotCom I ws involved with went DotBomb. Now supplying consulting services to various institutions in London and surrounding regions i.e. UK Smile | :) .
 
Now living and working in Australia, trying to be involved in the local .NET and Agile communities. Also maintaining an open source code coverage tool called PartCover whilst thinking of developing a new one.

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionpassword protected archive? PinmemberMatthias Blum13:23 15 Jan '09  
AnswerRe: password protected archive? PinmemberShaun Wilde0:06 16 Jan '09  
GeneralCompressing and Decompressing Data using C++ Pinmemberjassshan21:39 24 Nov '08  
GeneralGzip a set of files Pinmembersheyliger3:21 7 Jul '08  
GeneralRe: Gzip a set of files PinmemberShaun Wilde8:35 7 Jul '08  
Try downloading the latest source code there may be sample source code there.
 
The API is quite rich and you could probably do what you want by chaining various classes together.
 
I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
www.many-monkeys.com

GeneralWell I've added header but it doesn't work PinmemberNoagene21:36 15 Aug '07  
GeneralRe: Well I've added header but it doesn't work PinmemberShaun Wilde23:28 18 Aug '07  
GeneralRe: Well I've added header but it doesn't work PinmemberNoagene13:38 19 Aug '07  
GeneralError code has been changed.. PinmemberNoagene18:55 19 Aug '07  
AnswerRe: Error code has been changed.. Pinmemberbutuzov_Ilya22:27 6 Sep '07  
GeneralClose() is No Longer Available... PinmemberJeffrey Walton15:21 27 May '07  
GeneralRe: Close() is No Longer Available... PinmemberShaun Wilde10:04 28 May '07  
GeneralRe: Close() is No Longer Available... PinmemberJeffrey Walton11:03 28 May '07  
GeneralRe: Close() is No Longer Available... PinmemberShaun Wilde19:55 29 May '07  
GeneralRe: Close() is No Longer Available... PinmemberJeffrey Walton11:08 28 May '07  
GeneralRe: Close() is No Longer Available... PinmemberShaun Wilde19:56 29 May '07  
GeneralCrypto++ Integration Article PinmemberJeffrey Walton18:28 8 Dec '06  
GeneralGzip work verry greatly! Pinmemberdungbkhn17:50 28 Jun '05  
GeneralGot a wrong result! PinmemberJerryCheng22:44 11 Nov '03  
GeneralRe: Got a wrong result! PinmemberShaun Wilde8:13 12 Nov '03  
GeneralRe: Got a wrong result! PinmemberJerryCheng16:59 13 Nov '03  
GeneralRe: Got a wrong result! PinmemberShaun Wilde4:43 14 Nov '03  
Questionencryption of file..help?? Pinmemberxxhimanshu20:40 11 Nov '03  
AnswerRe: encryption of file..help?? PinmemberShaun Wilde8:17 12 Nov '03  
Generalzipping PinsussAnonymous8:24 1 Jun '03  

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

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

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