Compress uncompress Function

Source: Internet
Author: User
Tags uncompress

Address: http://lxflfr.blog.163.com/blog/static/129119579200983004539908/

Zlib is a common compression library that provides a set of In-memory compression and decompression functions, and can detect the integrity of extracted data (integrity ). Zlib also supports reading and writing gzip (.gz) files. The following describes the two most useful functions --CompressAndUncompress.

 

Int compress (bytef * DEST, ulogfn * destlen, const bytef * Source, ulong sourcelen );

The compress function compresses the content in the source buffer to the Dest buffer. Sourcelen indicates the size of the source buffer (in bytes ). Note that the second parameter destlen of the function is the address transfer call. When a function is called, destlen indicates the size of the Dest buffer. destlen> (sourcelen + 12) * 100.1%. After the function exits, destlen indicates the actual size of the buffer after compression. In this case, destlen/sourcelen is the compression ratio.

If compress succeeds, z_ OK is returned. If there is not enough memory, z_mem_error is returned. If the output buffer is not large enough, z_buf_error is returned.

 

Int uncompress (bytef * DEST, ulogfn * destlen, const bytef * Source, ulong sourcelen );

The uncompress function extracts the content of the source buffer to the Dest buffer. Sourcelen is the size of the source buffer (in bytes ). Note that the second parameter destlen of the function is the address transfer call. When a function is called, destlen indicates the size of the Dest buffer, and the Dest buffer must be sufficient to accommodate the extracted data. During decompression, you need to know in advance how large the compressed data will be. This requires that the size of the original data (that is, the size of the extracted data) be saved before compression ). This is not a function of the zlib function library and requires additional work. After the function exits, destlen indicates the actual size of the extracted data.

If uncompress succeeds, z_ OK is returned. If there is not enough memory, z_mem_error is returned. If the output buffer is not large enough, z_buf_error is returned. If the input data is incorrect, z_data_error is returned.

The example. c In zlib is a good learning example. We write a program to verify zlib's compression function. The written test program is saved as testzlib. cpp and placed under the zlib-1.1.4 directory. Program source code:

// Testzlib. cpp simple test zlib compression function

# Include <cstring>

# Include <cstdlib>

# Include <iostream>

# Include "zlib. H"

 

Using namespace STD;

 

Int main ()

{

Int err;

Byte compr [200], uncompr [200]; // big enough

Ulong comprlen, uncomprlen;

Const char * Hello = "12345678901234567890123456789012345678901234567890 ";

 

Ulong Len = strlen (Hello) + 1;

Comprlen = sizeof (compr)/sizeof (compr [0]);

 

Err = compress (compr, & comprlen, (const bytef *) Hello, Len );

 

If (Err! = Z_ OK ){

Cerr <"compess error:" <err <'\ n ';

Exit (1 );

}

Cout <"orignal size:" <Len

<", Compressed size:" <comprlen <'\ n ';

 

Strcpy (char *) uncompr, "garbage ");

 

Err = uncompress (uncompr, & uncomprlen, compr, comprlen );

 

If (Err! = Z_ OK ){

Cerr <"uncompess error:" <err <'\ n ';

Exit (1 );

}

Cout <"orignal size:" <Len

<", Uncompressed size:" <uncomprlen <'\ n ';

 

If (strcmp (char *) uncompr, hello )){

Cerr <"Bad uncompress !!! \ N ";

Exit (1 );

} Else {

Cout <"uncompress () succeed: \ n" <(char *) uncompr;

}

}

Compile and execute this program, and the output should be

D: \ libpng \ zlib-1.1.4>Bcc32 testzlib. cpp zlib. Lib

 

D: \ libpng \ zlib-1.1.4>Testzlib

Orignal size: 51, compressed size: 22

Orignal size: 51, uncompressed size: 51

Uncompress () succeed:

12345678901234567890123456789012345678901234567890

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.