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:
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)))
{
}
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");
gzip.WriteString(szString);
Buffers:
void* pBuffer;
int len;
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)))
{
}
Now, you can read a buffer with fixed length or the whole file:
The whole file:
void* pBuffer=NULL;
int len=0;
gzip.ReadBuffer(&pBuffer,&len);
...
if (pBuffer)
delete[] pBuffer;
A fixed length buffer:
int len;
char* pBuffer[len];
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: