Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C++
Article

CGZip, a C++ wrapper for gzip methods

Rate me:
Please Sign up or sign in to vote.
4.36/5 (17 votes)
9 Dec 20021 min read 336K   9.7K   83   70
A minimal class for handling gzip methods included in the zlib library.

Description

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,

Examples

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:

Strings:

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

Buffers:

void* pBuffer; // a memory buffer
int len;       // size in bytes of the memory buffer
// writing buffer
gzip.WriteBuffer(pBuffer,len);

When done, you must close the file:

gzip.Close();

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
gzip.ReadBuffer(&pBuffer,&len);
// 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
gzip.ReadBufferSize(pBuffer,len);

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
//jloup@gzip.org          madler@alumni.caltech.edu


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

License

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


Written By
Engineer
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 (http://research.microsoft.com/pex).

Comments and Discussions

 
GeneralRe: buffer to buffer treatment Pin
Jonathan de Halleux15-Jan-03 22:46
Jonathan de Halleux15-Jan-03 22:46 
GeneralRe: buffer to buffer treatment Pin
Jonathan de Halleux27-Jun-03 1:38
Jonathan de Halleux27-Jun-03 1:38 
QuestionWhere is the code? Pin
peter.ward@chasseral.com13-Jan-03 1:45
peter.ward@chasseral.com13-Jan-03 1:45 
AnswerCode = gzip.h, gzip.cpp Pin
Jonathan de Halleux13-Jan-03 2:41
Jonathan de Halleux13-Jan-03 2:41 
GeneralRe: Code = gzip.h, gzip.cpp Pin
EvilTed14-Aug-03 8:13
EvilTed14-Aug-03 8:13 
QuestionHow can I unzip a file which tarred ? Pin
vanta8-Jan-03 21:09
vanta8-Jan-03 21:09 
AnswerTar files Pin
Jonathan de Halleux15-Jan-03 22:41
Jonathan de Halleux15-Jan-03 22:41 
GeneralRe: Tar files Pin
vanta15-Jan-03 23:06
vanta15-Jan-03 23:06 
Hello,
Thks for your reply.
I know, untar ispart of GNU. and I haved a look in their sites already.

BUT :
My application is to extract the files in TAR,GZ,GZIP formats.
My application is available on Windows OS, NOT UNIX.
So, I want to have an example ( of Visual C, Windows OS) to decompress the file which was compressed in TAR,GZ formats.

For example : I have a file aaa.tar.gz and I want to extract that file.
your demo can extract that file ???

Please give me any ideas.
I am waiting for your reply.
Best regards.

THIEU Anh-Van
E-Mail vthieuanh@yahoo.com

GeneralRe: Tar files Pin
Jonathan de Halleux15-Jan-03 23:40
Jonathan de Halleux15-Jan-03 23:40 
GeneralBetter CGzipFile Pin
Paul Andreassen2-Jan-03 17:30
sussPaul Andreassen2-Jan-03 17:30 
GeneralRe: Better CGzipFile Pin
Paul Selormey2-Jan-03 18:01
Paul Selormey2-Jan-03 18:01 
QuestionSo, GZIP class can gzip,ungzip the SINGLE file ? Pin
vanta29-Dec-02 19:55
vanta29-Dec-02 19:55 
AnswerUntar Pin
Jonathan de Halleux15-Jan-03 22:42
Jonathan de Halleux15-Jan-03 22:42 
GeneralGZip and GUnzip Pin
Paul Selormey23-Dec-02 3:21
Paul Selormey23-Dec-02 3:21 
Generala possible solution Pin
Jonathan de Halleux23-Dec-02 3:37
Jonathan de Halleux23-Dec-02 3:37 
GeneralRe: a possible solution Pin
Paul Selormey23-Dec-02 3:52
Paul Selormey23-Dec-02 3:52 
GeneralSanta si here Pin
Jonathan de Halleux23-Dec-02 5:17
Jonathan de Halleux23-Dec-02 5:17 
GeneralRe: Santa si here Pin
Paul Selormey23-Dec-02 13:14
Paul Selormey23-Dec-02 13:14 
GeneralMore details Pin
Jonathan de Halleux24-Dec-02 2:20
Jonathan de Halleux24-Dec-02 2:20 
GeneralRe: More details Pin
Paul Selormey24-Dec-02 13:46
Paul Selormey24-Dec-02 13:46 
GeneralStop party , start reading Pin
Jonathan de Halleux29-Dec-02 23:57
Jonathan de Halleux29-Dec-02 23:57 
GeneralIStream Pin
Jonathan de Halleux2-Jan-03 1:14
Jonathan de Halleux2-Jan-03 1:14 
GeneralRe: IStream Pin
Paul Selormey2-Jan-03 17:48
Paul Selormey2-Jan-03 17:48 
GeneralAllelulia Pin
Jonathan de Halleux3-Feb-03 0:27
Jonathan de Halleux3-Feb-03 0:27 
GeneralRe: Allelulia Pin
Paul Selormey3-Feb-03 0:47
Paul Selormey3-Feb-03 0:47 

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.