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
- Hides zlib so you don't have to distribute the zlib headers
- UNICODE compliant,
In the examples, we shall consider the following code snippet to be present
using namespace zlib;
Zipping a memory buffer or a string
First of all, let's open a file for writing:
As you can see,
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");
void* pBuffer; int len; gzip.WriteBuffer(pBuffer,len);
When done, you must close the file:
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:
Now, you can read a buffer with fixed length or the whole file:
The whole file:
A fixed length buffer:
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:
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).