Install zlib-under Linux for compression function

Source: Internet
Author: User
Tags uncompress

The zlib Library is a well-known compression engine, andzlib is also the compression engine used by the famous gzip (GNU Zip) .

How do I install the zlib library?

First, install zlib, download the latest source program from its official website, website: http://www.gzip.org/zlib/.

then extract the downloaded files to the directory. After unpacking the folder with the appropriate makefile:

When compiling with the Make command, you will be asked to run the Configure file in the same directory first.

Open the configure file We can find is shell script, so run sudo shconfigure to execute the script.


second , then, by looking at the makefiles file, we compile the library with the Make command, and after the successful compilation (with root permission), install the library by the Do install, that is, the header files and Lib files are automatically placed into the/usr/ Include and /usr/lib.





Third, after the installation is complete, you can write a simple compression program, for example, a simple example is as follows:

#include <zlib.h> #include <zconf.h> #include <stdio.h> #include <errno.h> #include < Stdlib.h>int Main (int Argc,char **argv[]) {    char msg[1000]= " 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901 23456789012345678901234567890123456789012345678901234567890\n ";    Char buf[1000];    Char result[1000];    unsigned long len, lenresult;    printf ("msg size:%d\n", sizeof (msg));    Len = +;    Compress (buf, &len, msg, sizeof (msg));    printf ("Buf size:%d\n", Len);    lenresult=1000;    Uncompress (Result,&lenresult, buf, Len);        printf ("Result size:%d\n", lenresult);    printf ("result:%s\n", result);    return 0;}
The result after the run is:



where the function


1, int compress (Bytef *dest, ULONGF *destlen, const BYTEF *source, ULong Sourcelen);

The Compress function compresses the contents of the source buffer into the dest buffer. Sourcelen represents the size , in bytes, of the source buffer. Note that the second parameter of the function Destlen is the address call. When the function is called, Destlen represents the size of the dest buffer, Destlen > (sourcelen+) *100.1%. When the function exits, Destlen represents the actual size of the buffer after compression. At this point the Destlen /Sourcelen is exactly the compression rate.

Compress returns Z_OK if it succeeds, returns Z_MEM_ERROR if there is not enough memory, or z_buf_error if the output buffer is not large enough.


2, int uncompress (Bytef *dest, ULONGF *destlen, const BYTEF *source, ULong Sourcelen);

The Uncompress function decompressed the contents of the source buffer to the dest buffer. Sourcelen is the size (in bytes) of the source buffer. Note that the second parameter of the function Destlen is the address call. When the function is called, Destlen represents the size of the dest buffer, and the dest buffer is sufficient to accommodate the extracted data. When extracting, it is necessary to know in advance how large the compressed data will be extracted. This requires that the size of the original data (that is, the size of the extracted data) be saved before compression is made. This is not the function of the Zlib function library, we need to do extra work. When the function exits, the destlen is the actual size of the extracted data.

Uncompress returns Z_OK if it succeeds, returns Z_MEM_ERROR if there is not enough memory, or z_buf_error if the output buffer is not large enough. If the input data is incorrect, the Z_data_error is returned.



Program compilation Remember to add-lz, the development of library files.


Install zlib-under Linux for compression function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.