Click here to Skip to main content
13,296,374 members (54,166 online)
Click here to Skip to main content
Add your own
alternative version


82 bookmarked
Posted 17 Nov 2002

CGZip, a C++ wrapper for gzip methods

, 9 Dec 2002
Rate this:
Please Sign up or sign in to vote.
A minimal class for handling gzip methods included in the zlib library.


This documents present CGZip, C++ class wrapper for the gzip methods, which are included in the zlib library. The intention of this class is to have a simple class for zipping-unzipping buffers.

The main features of the class are:

  • Compress, decompress LPCTSTR to file
  • Compress, decompress memory buffer to file
  • Non-MFC
  • Hides zlib so you don't have to distribute the zlib headers
  • UNICODE compliant,


In the examples, we shall consider the following code snippet to be present on top:

// CGZip is in the zlib namespace
using namespace zlib;
CGZip gzip;

Zipping a memory buffer or a string

First of all, let's open a file for writing:

if(!gzip.Open(_T("myFile.gz"), CGZip::ArchiveModeWrite)))
        // the file could not be opened...

As you can see, Open returns true if success and false otherwise. All the methods (almost) have this behavior.

Now we can send data and strings to the file:


LPCTSTR szString = _T("This is a test string\n");
// writing string


void* pBuffer; // a memory buffer
int len;       // size in bytes of the memory buffer
// writing buffer

When done, you must close the file:


Note that if gzip goes out of scope, it is automatically closed.

Unzipping a buffer or a string

The reading behaves pretty much like the writing. First of all, open a file for reading:

if(!gzip.Open(_T("myFile.gz"), CGZip::ArchiveModeRead)))
        // the file could not be opened...

Now, you can read a buffer with fixed length or the whole file:

The whole file:

void* pBuffer=NULL;
int len=0;
// reads and unzip the hole file, len contains the resulting size of pBuffer
// pBuffer now contains len read bytes
// don't forget to delete pBuffer
if (pBuffer)
   delete[] pBuffer;

A fixed length buffer:

  int len;
char* pBuffer[len];
// reads and unzip the hole file, len contains the resulting size of pBuffer

Class reference

A detailed class reference is available as compressed HTML (generated by Doxygen).

GZip license

This piece of code is totally, utterly free for commercial and non-commercial use. However, it uses zlib so you should check the zlib license before using it. It is included in the source code but here it is:

//zlib.h -- interface of the 'zlib' general purpose compression library
//version 1.1.4, March 11th, 2002

//Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler

//This software is provided 'as-is', without any express or implied
//warranty.  In no event will the authors be held liable for any damages
//arising from the use of this software.

//Permission is granted to anyone to use this software for any purpose,
//including commercial applications, and to alter it and redistribute it
//freely, subject to the following restrictions:

//1. The origin of this software must not be misrepresented; you must not
//   claim that you wrote the original software. If you use this software
//   in a product, an acknowledgment in the product documentation would be
//   appreciated but is not required.
//2. Altered source versions must be plainly marked as such, and must not be
//   misrepresented as being the original software.
//3. This notice may not be removed or altered from any source distribution.

//Jean-loup Gailly        Mark Adler

//The data format used by the zlib library is described by RFCs (Request for
//Comments) 1950 to 1952 in the files
//(zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jonathan de Halleux
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (

You may also be interested in...


Comments and Discussions

Questionerror LNK2019: Pin
_R13_20-Feb-12 2:27
member_R13_20-Feb-12 2:27 
AnswerRe: error LNK2019: Pin
_R13_20-Feb-12 3:58
member_R13_20-Feb-12 3:58 
GeneralMy vote of 5 Pin
manoj kumar choubey16-Feb-12 0:16
membermanoj kumar choubey16-Feb-12 0:16 
GeneralMy vote of 5 Pin
chaau3-Nov-11 15:48
memberchaau3-Nov-11 15:48 
Questionunzip gzip data in memory Pin
rmusiy28-Jun-11 6:16
memberrmusiy28-Jun-11 6:16 
Generaldirectory Pin
JohnWallis4227-Aug-10 17:30
memberJohnWallis4227-Aug-10 17:30 
QuestionWhat is the point of this? Pin
ThisIsANameOK19-Nov-09 18:01
memberThisIsANameOK19-Nov-09 18:01 
QuestionUnZipping problem Pin
Member 264226931-Oct-08 4:38
memberMember 264226931-Oct-08 4:38 
GeneralUncompressed != plain text Pin
Martin Beckett13-Nov-07 16:45
memberMartin Beckett13-Nov-07 16:45 
GeneralLimited support Pin
kuroyume016120-Dec-06 23:36
memberkuroyume016120-Dec-06 23:36 
GeneralUsing CGZip on linux Pin
ktbstudio24-Oct-06 8:22
memberktbstudio24-Oct-06 8:22 
GeneralHave a fatal erro:LNK1104 cannot open file "zlib.lib" Pin
luoyi_iim2-Jan-06 2:53
memberluoyi_iim2-Jan-06 2:53 
QuestionWhere is unzip-on-the-fly functionality? Pin
ru4me21-Oct-05 19:48
sussru4me21-Oct-05 19:48 
GeneralBad Compressed File Pin
grandamoca15-Sep-05 23:51
sussgrandamoca15-Sep-05 23:51 
GeneralLibrary to decompress ZIP files Pin
Anonymous14-Sep-05 6:58
sussAnonymous14-Sep-05 6:58 
GeneralRe: Library to decompress ZIP files Pin
Ravi Bhavnani14-Sep-05 7:37
memberRavi Bhavnani14-Sep-05 7:37 
Generalfilesize getting bigger Pin
Mervick6-Jul-05 0:27
memberMervick6-Jul-05 0:27 
QuestionCGZip with zlib 1.2.2? Pin
grandamoca31-Mar-05 7:09
membergrandamoca31-Mar-05 7:09 
GeneralA stupid question but.. Pin
Tommy2k8-Nov-04 6:22
memberTommy2k8-Nov-04 6:22 
GeneralUnicode support for CGZip Pin
Jxm96c23-Apr-04 7:14
memberJxm96c23-Apr-04 7:14 
GeneralRe: Unicode support for CGZip Pin
vmihalj8-Sep-05 0:55
membervmihalj8-Sep-05 0:55 
Have you defined appropriate Unicode macros which will user wchar_t for TCHAR, instead of (default) plain char?

I think that all you need is "#define _UNICODE" or include "_UNICODE" in the compiler option list (exclude MBCS if it's defined there).

Take a look at MSDN docs on TCHAR or CString (which is of TCHAR type, so is treated as char or wchar_t).

"If you think that knowledge/education is expensive, would you try ignorance?"

Vatroslav 'Ziggy' Mihalj, B.Sc.Computer Science
Member of HUPRO, ACM, IEEE, IEEE Computer Soc. and Communications Soc.
GeneralRe: Unicode support for CGZip Pin
Peter Weyzen10-Aug-06 20:05
memberPeter Weyzen10-Aug-06 20:05 
GeneralRe: Unicode support for CGZip Pin
David Pritchard9-Mar-09 5:22
memberDavid Pritchard9-Mar-09 5:22 
QuestionCan not set compress mode Pin
VickyLiu22-Sep-03 21:38
memberVickyLiu22-Sep-03 21:38 
GeneralConflicts with MFC program Pin
Hillrider31-Jul-03 12:52
memberHillrider31-Jul-03 12:52 
GeneralRe: Conflicts with MFC program Pin
Hillrider31-Jul-03 13:16
memberHillrider31-Jul-03 13:16 
GeneralRe: Conflicts with MFC program Pin
Jonathan de Halleux31-Jul-03 13:20
memberJonathan de Halleux31-Jul-03 13:20 
GeneralRe: Conflicts with MFC program Pin
rjahrman6-Aug-03 19:17
memberrjahrman6-Aug-03 19:17 
GeneralRe: Conflicts with MFC program Pin
Jonathan de Halleux7-Aug-03 0:07
memberJonathan de Halleux7-Aug-03 0:07 
GeneralArchive multiple files Pin
adityad12-Jun-03 10:18
memberadityad12-Jun-03 10:18 
GeneralRe: Archive multiple files Pin
Jonathan de Halleux12-Jun-03 10:30
memberJonathan de Halleux12-Jun-03 10:30 
GeneralJust great! Pin
Eugene Plokhov7-Jun-03 22:29
memberEugene Plokhov7-Jun-03 22:29 
GeneralRe: Just great! Pin
Jonathan de Halleux31-Jul-03 13:23
memberJonathan de Halleux31-Jul-03 13:23 
GeneralArchiving Multiple Files Pin
Animesh Pandeya27-Jan-03 3:19
memberAnimesh Pandeya27-Jan-03 3:19 
GeneralTar Pin
Jonathan de Halleux27-Jan-03 3:53
memberJonathan de Halleux27-Jan-03 3:53 
Generalbuffer to buffer treatment Pin
rowby14-Jan-03 8:23
memberrowby14-Jan-03 8:23 
GeneralRe: buffer to buffer treatment Pin
Jonathan de Halleux15-Jan-03 23:46
memberJonathan de Halleux15-Jan-03 23:46 
GeneralRe: buffer to buffer treatment Pin
Jonathan de Halleux27-Jun-03 2:38
memberJonathan de Halleux27-Jun-03 2:38 
QuestionWhere is the code? Pin
peter.ward@chasseral.com13-Jan-03 2:45
memberpeter.ward@chasseral.com13-Jan-03 2:45 
AnswerCode = gzip.h, gzip.cpp Pin
Jonathan de Halleux13-Jan-03 3:41
memberJonathan de Halleux13-Jan-03 3:41 
GeneralRe: Code = gzip.h, gzip.cpp Pin
EvilTed14-Aug-03 9:13
memberEvilTed14-Aug-03 9:13 
QuestionHow can I unzip a file which tarred ? Pin
vanta8-Jan-03 22:09
membervanta8-Jan-03 22:09 
AnswerTar files Pin
Jonathan de Halleux15-Jan-03 23:41
memberJonathan de Halleux15-Jan-03 23:41 
GeneralRe: Tar files Pin
vanta16-Jan-03 0:06
membervanta16-Jan-03 0:06 
GeneralRe: Tar files Pin
Jonathan de Halleux16-Jan-03 0:40
memberJonathan de Halleux16-Jan-03 0:40 
GeneralBetter CGzipFile Pin
Paul Andreassen2-Jan-03 18:30
sussPaul Andreassen2-Jan-03 18:30 
GeneralRe: Better CGzipFile Pin
Paul Selormey2-Jan-03 19:01
memberPaul Selormey2-Jan-03 19:01 
QuestionSo, GZIP class can gzip,ungzip the SINGLE file ? Pin
vanta29-Dec-02 20:55
membervanta29-Dec-02 20:55 
AnswerUntar Pin
Jonathan de Halleux15-Jan-03 23:42
memberJonathan de Halleux15-Jan-03 23:42 
GeneralGZip and GUnzip Pin
Paul Selormey23-Dec-02 4:21
memberPaul Selormey23-Dec-02 4:21 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 10 Dec 2002
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid