Use zlib library for Data Compression

Source: Internet
Author: User

Use zlib library for Data Compression

Zlib is designed to be a free, general-purpose, legally unencumbered -- that is, not covered by any patents -- lossless data-compression library for use on usually ally any computer hardware and operating system. the zlib data format is itself Portable platform SS platforms.

How can we use it for data compression?

First, download the latest release.pdf from http://www.zlib.net/and decompress it to a file directory.

If you work in Linux, you should first check the MAKEFILE file in the directory and open it in text (less in the command line) before compiling. You can see the following sentences:

# To compile and test, type:
#./Configure; Make Test
# The call of configure is optional if you don't have special requirements
# If you want to build zlib as a shared library, use:./configure-S

# To install/usr/local/lib/libz. * and/usr/local/include/zlib. H, type:
# Make install
# To install in $ home instead of/usr/local, use:
# Make install prefix = $ home

Compile and install the static library in Linux (. a) file, enter it in the command line. /configure; Make; make install; and you can compile and install the Shared Library (. so, similar to Windows. DLL), enter it in the command line. /configure-S; Make; make install; If yes appears, it will succeed.

Compiling in Windows is easier. Open zlib under projects/visualc6 in the directory. DSW, open it with vc6, select ations under the build menu, and select the type you want to generate. Vs2003/vs2005, similar to the following.

Use zlib to write a simple code for testing.

# I nclude <stdio. h>
# I nclude "zlib. H"

Int main ()
{
// Raw data
Const unsigned char strsrc [] = "Hello world! /N/
Aaaaa bbbbb CCCCC ddddd AAAAA bbbbb CCCCC ddddd Chinese test/
Aaaaa bbbbb CCCCC ddddd AAAAA bbbbb CCCCC ddddd Chinese test/
Aaaaa bbbbb CCCCC ddddd AAAAA bbbbb CCCCC ddddd Chinese test/
Aaaaa bbbbb CCCCC ddddd AAAAA bbbbb CCCCC ddddd Chinese test ";

Unsigned char Buf [1024] = {0}, strdst [1024] = {0 };
Unsigned long srclen = sizeof (strsrc), buflen = sizeof (BUF), dstlen = sizeof (strdst );

Printf ("src string: % S/nlength: % d/N", strsrc, srclen );
// Compression
Compress (BUF, & buflen, strsrc, srclen );
Printf ("/nafter compressed length: % d/N", buflen );
Printf ("compressed string: % s/n", Buf );
// Extract
Uncompress (strdst, & dstlen, Buf, buflen );
Printf ("/nafter uncompressed length: % d/N", dstlen );
Printf ("uncompressed string: % s/n", strdst );
Return 0;
}

Note when compiling this code. Assume that you are in windows and use the static library (. lib) connection, so under vc6, you need to set zlib. put the Lib file in your code directory and put zlib. h, zconf. H is added to the workspace, and the project attribute is set: Project-settings-link-category select input, and then enter a space and zlib at the end of the library modules. lib, and enter msvcrt in lgnore libraries, because this default library conflicts with zlib! Compile and run the program directly after setting it.

If you are running Linux. h, zconf. h. libz. a. Test. CPP is placed in the same directory, and then enter the following command in the command line, and then run. /test

G ++ *. cpp libz. a-g-o Test

 

 

 

 

 

 

 

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.