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

CGZip, a C++ wrapper for gzip methods

By , 9 Dec 2002
 

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

About the Author

Jonathan de Halleux
Engineer
United States United States
Member
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).

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   
GeneralUsing CGZip on linuxmemberktbstudio24 Oct '06 - 7:22 
Hello, my question is, how can i compile the cgzip on linux?
 
It needs atl what is in windows, but i don't found atl in linux.
GeneralHave a fatal erro:LNK1104 cannot open file "zlib.lib"memberluoyi_iim2 Jan '06 - 1:53 
when I have done follow the introduction and to link my project there have an erro "fatal erro:LNK1104 cannot open file "zlib.lib".I don't know how to solve it.
QuestionWhere is unzip-on-the-fly functionality?sussru4me21 Oct '05 - 18:48 
I have a 100mb gzip file. Unzipping the whole thing will take up 2GB memory. So unzipping on the fly is prefered. How would I go about doing that using this gzip wrapper?
 

GeneralBad Compressed Filesussgrandamoca15 Sep '05 - 22:51 
I have this problem, that occurs only sometimes:
 
-i compress a file (with no errors)
-then i decompress it (also with no errors)
 
the problem is that the decompressed file is diferent (not in size, but the content is different) from the original file.
Note that this problem only occurs rarely, but has very negative impact on the application that i'm building. Is there any way to prevent this? Can you help me? Sigh | :sigh:
 
Renato S. Gaio
GeneralLibrary to decompress ZIP filessussAnonymous14 Sep '05 - 5:58 
Hello,
 
Does anyone knows a library to decompress zip files.
I am trying to decompres files compressed with win zip .
Do you know if I can still use CGZip ?
GeneralRe: Library to decompress ZIP filesmemberRavi Bhavnani14 Sep '05 - 6:37 
Here's[^] one.
 
/ravi
 
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
Generalfilesize getting biggermemberMervick5 Jul '05 - 23:27 
is it only me that feels that the filesize is getting bigger each time i create a new file? was wondering if the header appended at the front and back of the file is being removed during the deflation or is there anything wrong with this code
 
if (!gzip.Open(_T("test"), CGZip::ArchiveModeRead))
{
printf(_T("Cannot read file\n"));
exit(-1);
}
 
void * pBuffer=NULL;
 
unsigned int size;
 
gzip.ReadBuffer( &pBuffer, size );
 
gzip.Close();
 
if (!gzip.Open("test2", CGZip::ArchiveModeWrite))
{
printf(_T("Cannot create file\n"));
exit(-1);
}
 
gzip.SetCompression( CGZip::CompressionModeNoCompression );
 
gzip.WriteBuffer( pBuffer, size );
 
gzip.Close();
QuestionCGZip with zlib 1.2.2?membergrandamoca31 Mar '05 - 6:09 
Can´t someone provide me CGZip (C++ wrapper) using the latest zlib? (1.2.2)
 
grandamoca
GeneralA stupid question but..memberTommy2k8 Nov '04 - 5:22 
How do i use this code to decompress a .gz file stored on the internet?
 
Thnx...
GeneralUnicode support for CGZipmemberJxm96c23 Apr '04 - 6:14 
Dear Jonathan,
 
Firstly let me thank you for making this easy for us newbies. Your wrapper was easy to use and does a great job. Smile | :)
 
One thing I am having problem is though is that I am building XML Dom trees for SVG docs using MSXML4, it all works fine if the XML data does not contain any wchars, and the XML is written to an .svgz document using CGZIP with no problem. What I have found however is that when there are occasional unicode characters involved in the XML data, the gzip produced will truncate some data towards the end of the document generated when I check the source (SVG viewer supports svgz which is a gzipped XML doc). I experimented with changing the line where WriteString calls the method WriteBuffer and giving it instead of sizeof(TCHAR), a sizeof(WCHAR). This way the XML data is passed on all well but I find possibly binary characters (or null) running over the end of the XML data so effectively it's gzipping blanks after the XML data source runs out.
 
Therefore I wonder if there is anything that I can do to rid this problem? How can I make XML data containing occasional Unicode work with CGZip? Or is there a way (algorithm) to calculate the real byte length of the XML data string containing bits of Unicode chars.
 
Thanks,
Julius

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 10 Dec 2002
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid