5,135,153 members and growing! (13,149 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

CGZip, a C++ wrapper for gzip methods

By Jonathan de Halleux

A minimal class for handling gzip methods included in the zlib library.
VC6, VC7, C++Windows, Win2K, WinXPVS6, VS, Dev

Posted: 17 Nov 2002
Updated: 9 Dec 2002
Views: 132,328
Announcements



Search    
Advanced Search
Sitemap
25 votes for this Article.
Popularity: 6.07 Rating: 4.34 out of 5
3 votes, 23.1%
1
0 votes, 0.0%
2
1 vote, 7.7%
3
2 votes, 15.4%
4
7 votes, 53.8%
5

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


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).

Occupation: Engineer
Location: United States United States

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 61 (Total in Forum: 61) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralUncompressed != plain textmemberMartin Beckett16:45 13 Nov '07  
GeneralLimited supportmemberkuroyume016123:36 20 Dec '06  
GeneralUsing CGZip on linuxmemberktbstudio8:22 24 Oct '06  
GeneralHave a fatal erro:LNK1104 cannot open file "zlib.lib"memberluoyi_iim2:53 2 Jan '06  
GeneralWhere is unzip-on-the-fly functionality?sussru4me19:48 21 Oct '05  
GeneralBad Compressed Filesussgrandamoca23:51 15 Sep '05  
GeneralLibrary to decompress ZIP filessussAnonymous6:58 14 Sep '05  
GeneralRe: Library to decompress ZIP filesmemberRavi Bhavnani7:37 14 Sep '05  
Generalfilesize getting biggermemberMervick0:27 6 Jul '05  
GeneralCGZip with zlib 1.2.2?membergrandamoca7:09 31 Mar '05  
GeneralA stupid question but..memberTommy2k6:22 8 Nov '04  
GeneralUnicode support for CGZipmemberJxm96c7:14 23 Apr '04  
GeneralRe: Unicode support for CGZipmembervmihalj0:55 8 Sep '05  
GeneralRe: Unicode support for CGZipmemberPeter Weyzen20:05 10 Aug '06  
GeneralCan not set compress modememberVickyLiu21:38 22 Sep '03  
GeneralConflicts with MFC programmemberHillrider12:52 31 Jul '03  
GeneralRe: Conflicts with MFC programmemberHillrider13:16 31 Jul '03  
GeneralRe: Conflicts with MFC programmemberJonathan de Halleux13:20 31 Jul '03  
GeneralRe: Conflicts with MFC programmemberrjahrman19:17 6 Aug '03  
GeneralRe: Conflicts with MFC programmemberJonathan de Halleux0:07 7 Aug '03  
GeneralArchive multiple filesmemberadityad10:18 12 Jun '03  
GeneralRe: Archive multiple filesmemberJonathan de Halleux10:30 12 Jun '03  
GeneralJust great!memberEugene Plokhov22:29 7 Jun '03  
GeneralRe: Just great!memberJonathan de Halleux13:23 31 Jul '03  
GeneralArchiving Multiple FilesmemberAnimesh Pandeya3:19 27 Jan '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Dec 2002
Editor: Chris Maunder
Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project