![]() |
Languages »
C / C++ Language »
General
Intermediate
CGZip, a C++ wrapper for gzip methodsBy Jonathan de HalleuxA minimal class for handling gzip methods included in the zlib library. |
VC6, VC7Win2K, WinXP, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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:
LPCTSTR to fileIn 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;
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 gzip.WriteString(szString);
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.
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:
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;
int len; char* pBuffer[len]; // reads and unzip the hole file, len contains the resulting size of pBuffer gzip.ReadBufferSize(pBuffer,len);
A detailed class reference is available as compressed HTML (generated by Doxygen).
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).
General
News
Question
Answer
Joke
Rant
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-2009 Web10 | Advertise on the Code Project |