zlib usage simple instructions
Published: 2019-04-13

The purpose of this article is to simply explain how to add zlib to MFC program and provide memory compression function.

1. How to Obtain zlib

zlib's homepage is: http://www.zlib.net/

2. Open with VC++6.0

Open the downloaded source code. The VC6.0 project has been completed. Double-click zlib.dsw in /projects/visualc6. You can see three projects in VC++6.0: zlib is a library file (win32 lib debug/release is selected for compilation setting).Engineering example is an example of how to use zlib.lib, and engineering minigzip is an example of how to use functions provided by zlib to read and write. gz files (*.gz files are commonly used under Linux).

3. How to Join My Project

After compiling Zlib lib, you have all the files needed to call a Static library (Zlib zlib.lib, zlib.h, zconf.h h). I don't need to say how to call Static library.

4. What can I do with zlib

Let's take a look at those functions provided by zlib. They are all in Zlib. H. Don't be dizzy when you see a bunch of macros. In fact, they are all for compatibility with various compilers and some type definitions. If you cling to the prototype declarations of those major functions, you won't be affected by these things.

There are several key functions:

(1)int compress (Bytef *dest,   uLongf *destLen, const Bytef *source, uLong sourceLen);

Compress the source buffer into the destination buffer. It's as simple as that. A function is done

(2) int compress2 (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen,int level);

function is the same as the previous function, each parameter can specify the relationship between compression quality and compression degree (0-9). if you are not sure about this parameter, you need not pay too much attention to it. it is good to understand a reason: it takes more time to get a high compression ratio.

(3) uLong compressBound (uLong sourceLen);

Calculate the required buffer length. Assuming that you want to know how big your productivity is after data compression for sourcelen before compression, you can call this function to calculate. This function cannot get accurate results, but it can ensure that the actual output length is definitely smaller than the calculated length.

(4) int uncompress (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen);

Decompression (see name:)

(5) deflateInit() + deflate() + deflateEnd()

3 functions are combined to complete the compression function. See test_deflate () function of example.c for specific usage. In fact, the compress () function is implemented with these 3 functions (compress.c file of Engineering zlib)

(6) inflateInit() + inflate() + inflateEnd()

Similar to (5), decompression function is completed.

(7) gz start function. Used to operate *.gz files, and file stdio call way is similar. Want to know how to use the example.c test_gzio () function, very easy.

(8) Other functions such as obtaining version will not be mentioned.

Summary: In fact, as long as there are two functions, compress () and uncompress (), it is sufficient in most applications.