zlib: Add Industrial Strength Compression to Your C/C++ Apps

Zlib has been the gold standard for lossless compression almost since its introduction in 1995. Much of this success is probably due its open source status and wide portability—from Palm Pilots to IBM mainframes, you'll find zlib version for every platform. Furthermore, unlike the patented LZW algorithm, zlib compression is guaranteed to never require more space than the original data.

You may wonder whether you still need compression nowadays with 300 Gb hard drives everywhere. Consider that compression is more than just saving some space on the hard drive. Applications that use the Internet, such as instant messaging and real-time gaming, benefit from compression immediately because you can never have too much bandwidth. You also can leave some types of data that are accessed sequentially in-memory as compressed items, thereby saving another precious resource. Lastly, any decent compression system includes Cyclic Redundancy Check (CRC32), which provides yet another layer of defense against data communication or I/O errors.

How Shall I Compress Thee? Let Me Count the Ways

Although at the bottom-most layer zlib is implemented as a straightforward C-style interface, many people have improved zlib's accessibility over the years by offering alternative interfaces. If you use Visual Basic, you might find it easier to use the zlib ActiveX (OCX) interface developed by Mark Nelson. C++ purists may want to try the Gzstream Library that mimics the familiar iostream interfaces. Going further afield, further ports towards Perl, Python, Tcl, and several implementations in Java are available, all of which are linked from the zlib home page.

For simplicity's sake, this tutorial covers only the basic C-style interface. All the concepts inherent there will be relevant to most other bindings.

Zlib Utility Functions

You can write three different styles of applications using zlib: utility I/O functions, in-memory compression, and basic functions. This article examines only the utility I/O functions in depth because of their inherent simplicity. The utility functions are the highest level of API. Their purpose is to behave the same as regular functions except that they do compression behind the scenes. They mimic the C-style FILE objects by providing a replacement "gz" function for each traditional stdio.h function. For example, gzopen() replaces open(), gzwrite() replaces write(), and so on. As such, you must expect gzwrite() to return the number of uncompressed bytes written as opposed to the number of actual compressed bytes written.

The basic functions allow fine-grained control over memory allocation, as well as enabling you to examine Adler32 and CRC32 checksums on a block-by-block basis, set a compression level on a scale from 1 to 10, and control the compression dictionaries. As mentioned previously, these options are outside the scope of this article.

The remainder of this article presents the shortest possible program that compresses a file into a second file, and then it demonstrates uncompressing that second file into a third output file (which hopefully matches the original).

To get started, you'll need a copy of zlib 1.2.3 from the zlib home page. The sample program to follow uses the Win32 precompiled binaries for simplicity. If you're working a decently configured Linux system, you probably have headers in /usr/include/zlib.h.

 1 #include <stdio.h>
 2 #include "/zlib/include/zlib.h"
 3
 4 // Demonstration of zlib utility functions
 5
 6 unsigned long file_size(char *filename)
 7 {
 8    FILE *pFile = fopen(filename, "rb");
 9    fseek (pFile, 0, SEEK_END);
10    unsigned long size = ftell(pFile);
11    fclose (pFile);
12    return size;
13 }
14
15 int decompress_one_file(char *infilename, char *outfilename)
16 {
17    gzFile infile = gzopen(infilename, "rb");
18    FILE *outfile = fopen(outfilename, "wb");
19    if (!infile || !outfile) return -1;
20
21    char buffer[128];
22    int num_read = 0;
23    while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
24       fwrite(buffer, 1, num_read, outfile);
25       }
26
27    gzclose(infile);
28    fclose(outfile);
29 }
32
33 int compress_one_file(char *infilename, char *outfilename)
34 {
35    FILE *infile = fopen(infilename, "rb");
36    gzFile outfile = gzopen(outfilename, "wb");
37    if (!infile || !outfile) return -1;
38
39    char inbuffer[128];
40    int num_read = 0;
41    unsigned long total_read = 0, total_wrote = 0;
42    while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
43       total_read += num_read;
44       gzwrite(outfile, inbuffer, num_read);
45    }
46    fclose(infile);
47    gzclose(outfile);
48
49    printf("Read %ld bytes, Wrote %ld bytes,
             Compression factor %4.2f%%\n",
50       total_read, file_size(outfilename),
51       (1.0-file_size(outfilename)*1.0/total_read)*100.0);
52 }
53
54
55 int main(int argc, char **argv)
56 {
57    compress_one_file(argv[1],argv[2]);
58    decompress_one_file(argv[2],argv[3]);
59 }

The sample program takes three arguments. The first argument is the original uncompressed file; the second is the new compressed file; and the third (just for demonstration purposes) is the uncompressed version of the file you just compressed. For example, you might run it as follows:

zlibtest verybigfile.dat verybigfile.z verybigfile2.dat

And then you can do a comparison to convince yourself that the compress/uncompress step has not hurt your data. The compress_one_file (lines 33-52) portion uses a rather small buffer (128 bytes). In practice, you could have easily fit the whole thing in memory, but the point is simply to show how it would work piecewise.

To compile this program, you type in the following:

cl /TP zlibtest.c /link /DEBUG d:\zlib\lib\zdll.lib

Note that zlib1.dll will need to be accessible in your path or the current directory.

If you give the program a whirl on the Constitution of the United States, the results are pretty good:

zlibtest constitution.txt constitution.z constitution2.txt
Read 28365 bytes, Wrote 9196 bytes, Compression factor 67.58%

If you compare them by using InfoZIP with "-9" maximal compression, you see that in fact 68 percent is the best you can hope for on this text file.

Is It Safe Now?

You may have heard of zlib security vulnerabilities in the past. Because an application is only as secure as its weakest linked-in library, should you worry? No. All known security issues have been cured as of the latest zlib version 1.2.3. In July 2005, zlib 1.2.2 had a decompression buffer overflow vulnerability. Going back further to March 2005, zlib 1.2.0 had a denial of service (DoS) vulnerability. The previous year, a double free bug cropped up. Given that there are more than a thousand diverse applications using zlib (a few examples off the top of my head are the OpenSSH project, libPNG, and infoZIP, a popular free pkZIP replacement), even by conservative estimates you could argue it is probably one of the most severely tested libraries ever. Certainly, none of the developers of these widely used products, each of which has been distributed for more than a decade, has given up on zlib.

Compression Is Still Relevant

Until every filesystem and network pipe has transparent and seamless compression built in below the transport layers, you have plenty of good reasons to use compression in your apps, such as time, space, and therefore money. Zlib has stood the test of time, yet still remains one of the easiest third-party tools to integrate into your application suite. If you haven't thought about compression lately, maybe it's time to give zlib a shot.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.

About the Author

Victor Volkman

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.

IT Offers

Comments

  • louis vuitton speedy yugjik

    Posted by paswppi on 04/01/2013 02:59am

    Louis Vuitton soldes Louis Vuitton sac sac Louis Vuitton pas cher Sacs Louis Vuitton

    Reply
  • http://www.oakleysunglassesoutc.com/ wirkjh

    Posted by http://www.oakleysunglassesoutc.com/ Mandycof on 03/29/2013 03:40am

    ghd australia Now that you have the start will not stop, at least before the ghd hair straightener achieve their goals will not be stopped ...... Customs negotiating complex and difficult to understand, I've done a full mental preparation, but also for you to create better chance to fight for the advantage of the negotiations ... negotiations is not a day can be accumulated decades to solve. That's right, the Customs negotiations even longer difficult. Quiet said with a smile: also an opportunity! So-called communication, but a threat to it, but this is the manner commonly used language among countries, the Russians want to tell ghd naval forces in the Far East to China than Russia strong on that. But let ghd straightener do not forget it is a powerful multi Baltic Fleet in Europe than its Pacific Fleet .ghd sale,..... Tan Yan said with disdain: Does he think I was scared big what? Kou Green already complained to my relations with Russia has been a huge diplomatic pressure to bring the Ministry of Foreign Affairs, I also think that at the moment in time to open the Customs negotiations unfit and Russia against each other ... although the relationship between Britain and Russia was wrong Customs employment of foreign staff, none of the Russians can be seen to this point.

    Reply
  • fitted hats wholesale

    Posted by xxds3kg on 03/29/2013 12:11am

    [url=http://bestbaseballcap.webs.com]wholesale snapback caps[/url] wholesale snapback caps t srjs [url=http://wholesalefittedhat.webs.com]snapback wholesale[/url] snapback wholesale h pqbo[url=http://cheapsnapbacksforsalezone.webs.com]snapback hats cheap[/url] snapback hats cheap u iiyg[url=http://cheaphatsmall.webs.com]snapbacks for cheap[/url] snapbacks for cheap i qccs[url=http://wholesalefittedhat.webs.com]snapback wholesale[/url] snapback wholesale q duwy[url=http://cheapsnapbacksforsalezone.webs.com]cheap snapbacks free shipping[/url] cheap snapbacks free shipping g smdu [url=http://snapbackswholesalezone.webs.com]snapbacks wholesale[/url] snapbacks wholesale b pzil [url=http://goodsnapbackhatscheap.webs.com]cheap snapback hats[/url] cheap snapback hats d kajs[url=http://cheaphatsmall.webs.com]snapbacks for cheap[/url] snapbacks for cheap z txjj[url=http://cheaphatsmall.webs.com]snapbacks for cheap[/url] snapbacks for cheap a sbqq[url=http://wholesalefittedhat.webs.com]fitted hats wholesale[/url] fitted hats wholesale i jorj[url=http://snapbackhatwholesale.webs.com]snapback hats wholesale[/url] snapback hats wholesale g urmz [url=http://wholesalefittedhat.webs.com]snapbacks wholesale[/url] snapbacks wholesale g klrw [url=http://wholesalefittedhat.webs.com]fitted hats wholesale[/url] fitted hats wholesale f grdc[url=http://snapbackhatwholesale.webs.com]wholesale beanies[/url] wholesale beanies d vlkq[url=http://cheapsnapbacksforsalezone.webs.com]cheap snapbacks online[/url] cheap snapbacks online b uzhw[url=http://cheapsnapbackshat.webs.com]cheap snapbacks online[/url] cheap snapbacks online f zwbx[url=http://cheaphatsmall.webs.com]cheap hats[/url] cheap hats a jhnd

    Reply
  • fake oakley sunglasses

    Posted by ggliliImpumpvrq on 03/28/2013 11:34pm

    sunglasses wholesale [url=http://olesalesunglassesgood.webs.com]sunglasses wholesale[/url] fake ray ban sunglasses cheap ray ban [url=http://qualityguccisunglass.webs.com]cheap ray ban[/url] oakley sunglasses cheap cheap oakley [url=http://guccicheapsunglass.webs.com]cheap oakley[/url] cheap oakley sunglasses cheap [url=http://onlineguciisunglass.webs.com]sunglasses cheap[/url] oakley sunglasses discount sunglasses cheap [url=http://onlineguciisunglass.webs.com]sunglasses cheap[/url] discount oakley sunglasses,,,,,o discount oakleys [url=http://discountsunglassesfinewebs.com]discount oakleys[/url] ray ban sunglasses cheap cheap ray ban [url=http://qualityguccisunglass.webs.com]cheap ray ban[/url] cheap sun glasses cheap ray ban sunglasses [url=http://sunglasspomoteauthentic.webs.com]cheap ray ban sunglasses[/url] oakleys cheap

    Reply
  • oakley sunglasses cheap

    Posted by pgliliImpumptot on 03/28/2013 10:48pm

    cheap oakleys for sale [url=http://sunglasswholesaleofgucci.webs.com]cheap oakleys for sale[/url] designer sunglasses cheap oakley discount [url=http://discountsunglassesfinewebs.com]oakley discount[/url] replica oakleys cheap sunglasses [url=http://qualityguccisunglass.webs.com]cheap sunglasses[/url] wholesale designer sunglasses fake ray ban [url=http://fakeguccisunglasses.webs.com]fake ray ban[/url] cheap ray ban fake ray ban sunglasses [url=http://fakeguccisunglasses.webs.com]fake ray ban sunglasses[/url] ray ban sunglasses cheap fake ray ban wayfarer [url=http://fakeGucciwayfarer.webs.com]fake ray ban wayfarer[/url] sunglasses cheap cheap sunglasses [url=http://qualityguccisunglass.webs.com]cheap sunglasses[/url] discount sunglasses discount ray ban [url=http://discountsunglassessale.webs.com]discount ray ban[/url] cheap sun glasses

    Reply
  • women luxury watch ztjy

    Posted by Sacsikwr on 03/11/2013 01:52pm

    lunette louis vuitton pas cher louis vuitton pas cher nodkcr lunette louis vuitton pas cher ceinture louis vuitton pas cher draksc

    Reply
  • cheap ugg boots bMnr uJkc

    Posted by Suttonshe on 03/09/2013 10:05am

    cheap toms shoes rpwzlbba cheap toms fwgufobt toms coupon code kkbaknez toms on sale flyuscgz toms outlet hatdogty toms sale wnerakkr toms shoes on sale gmnbazai toms shoes sale zamcixhp toms shoes lbcxwrrb

    Reply
  • ghd australia vbfagj

    Posted by Mandymel on 02/03/2013 09:19pm

    5iFjj ugg vFyp mUvf nike shox sko 2nDcq toms outlet 0rDgs hollister 1sRpe ugg 8wHza longchamps 4pLld louis vuitton outlet 0xIas michael kors outlet 0jWjj christian louboutin 2fPrt Larry Grant Jersey 5gMzn 9eIty 0bMqj ghd 2pCcd ugg boots sale

    Reply
  • ugg boots qheyyd http://www.cheapfashionshoesas.com/

    Posted by Suttonxtl on 01/27/2013 01:40am

    4vYhk nike outlet yNir Michael Kors outlet bNpf ugg boots 9aYvz monster beats 7cEmz Cheap nfl jerseys 2qNhx ugg norge 7pUff burberry outlet 1qHkn longchamp 0mNjx nike shoes online 8dCbr cheap ugg boots 8cVyt monster beats 1nIym ugg 8iNyb GHD Hair Straightener 9rNkx 4fCng

    Reply
  • プラダレディースハンドバッグ、イギリスライフスタイル

    クィンテセンシャリーの象徴

    Posted by on 01/21/2013 11:00pm

    価格がより高い25ドル、500 Uにアクセスすることができます。色相、模造革の種類、仕様、およびいくつかの追加の事実に依存されるようなことが起こるのファンドhttp://www.monclervjapan.net

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Go Deeper

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds