3DSoftware.com > Programming > Compression > ZLib
Lossless Compression
 
An effective method of lossless compression is implemented with the ZLib Library for C/C++.
 
ZLib Library
 
The ZLib programming library was designed for embedded systems computers (microcontrollers) which require small memory footprint. For general purpose computing you need to write a wrapper to handle the memory management in accumulation loop fashion, as covered in the ZLib Usage article.
 
ZLib is written in the C language and is very portable. To port to C++, simply change the filename extensions of the source code files as needed, and change any old style C function declarations (your compiler will report each one as an error) to the new C++ style function declarations. For example, this is an old style C function declaration:
 
ZLib
ZLib is a lossless compression programming library that is self contained (does not require other libraries) and is genuinely free (for commercial use without restriction). It is written in C, and is easily ported to C++. Download the source code from:
www.zlib.net
 
int ZEXPORT deflate ( strm, flush )
    z_streamp strm;
    int flush;
{
//...
 
If your compiler reports that as an error, change that to:
 
int ZEXPORT deflate ( z_streamp strm, int flush )
{
//...
 
Porting to C++ also requires that you change the name of one of the variables (name of a record). What happened is that the ZLib source code (written in C) names a record (struct) “this”. In C you can do that, but in C++ “this” is a reserved name. When you port ZLib to C++ your compiler will report an error wherever “this” is used as a variable name. All you have to do is change that variable name. You can change it to “This” since reserved names are only lower case. Here is an example from the ZLib source code ported to C++:
 
if (This.val < 16) {
    NEEDBITS(This.bits);
    DROPBITS(This.bits);
    state->lens[state->have++] = This.val;
}
 
In that source code, “this.val” was changed to “This.val” and “this.bits” was changed to “This.bits”. Rename the record name “this” to “This” (or something else) wherever your compiler reports that error. That is all you need to do to port ZLib to C++.
 
To perform in-memory processing instead of writing to disk, allocate a memory block each time data made available, and keep a list of pointers to the allocated memory blocks. After processing, use the memory blocks in order, and free each one when done.
 
 
Copyright © 2008 by 3D Software. All rights reserved.
3D Software, P.O. Box 221190, Sacramento CA 95822 USA
www.3DSoftware.com     Contact us
Thursday, 20-Nov-2008 12:38:20 GMT