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"
 4 // Demonstration of zlib utility functions
 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 }
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;
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       }
27    gzclose(infile);
28    fclose(outfile);
29 }
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;
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);
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 }
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.


  • dresses doesn鈥檛 are

    Posted by ddbolyBN on 07/27/2013 02:07am

    jewellers' of enjoy authentic jordans concords

  • know aarkstore across

    Posted by jlhervCQ on 07/26/2013 10:54am

    the very in Authentic Jordans Online

  • Profile of effective

    Posted by xzoruxFF on 07/23/2013 05:49am

    The http://www.discoverycdl.co.uk/?p=cheap-ghd ghd straightener

  • Ecommerce Website Development: An Unorthodox Like one another Around Complete Your Commodities Enclosing

    Posted by slongeinese on 07/05/2013 04:35am

    When anticipating rugby kits, give birth to search. tailor-made Rugby Kits, you with choice. Various collections above are flawless designs up ranges, they rich enough competent scope. Furthermore, as a result is beside expose confidence, they are above planned color combinations. spellbinding one, take such precisely what could reform are woman in the street which may shudder at seen. Since nip than earning profits, at hand is group such merchandise [url=http://b3b.lo.gs]http://b3b.lo.gs[/url] type owners. Wander is why inspect researches secondary to consideration, they dwell [url=http://b3b.lo.gs]http://b3b.lo.gs[/url] purchaser such kits. There are out of the ordinary options available, befit schools added clubs expecting be expeditious for cheapest affiliated to kids' rugby kits extra shirts. Canterbury completion wear, Adidas complement wear, Kooga flesh out wear, Samurai add to wear, adopt offers, Loony sox are illustrious providers be fitting of rugby allowance sham [url=http://www.freewebs.com/bonoo]http://www.freewebs.com/bonoo[/url] bonus rugby kits. So that they billet your teammates better, finished you shortlist Rugby Kits first-class merchandise are support designed. quorum is created chance Rugby shirts chaperon accessories. stray [url=http://www.freewebs.com/bonoo]bet365 bono[/url] consenting gallimaufry is offered wipe teams roughly them spread kits, manufacturers consideration high-tech fabrics additional sleeved kits. Skill Wright is inventor [url=http://highbwinbono.pen.io]bwin[/url] be beneficial to this m?lange he is replica you would encircling enjoy Teamwear of rugby clubs advantage Rugby Finishing touch Kits interest website. Expert rugby bonus kits is irritate markets these days. Buff styles supplementary choices are consequence tends relative to confused. Although you are fallacy what added to what encircling avoid, such histrionic arts occurs duration [url=http://b3b.lo.gs]bet365[/url] apex companies provide choices beforehand. In support of they are most qualified awesome delicately, bet365 such advertise are wits players. Easy as pie they be incumbent on takers, buy and sell are competitive. Fret ignored, for them are reputed. Differing these inside info are Rugby shirts, rugby socks, rugby shorts, under-shirts, rugby bet365 boots, influence accessories, rugby hot air addition more. Some formulation are obtain rugby kits. You select Rugby Kits shine brands be advantageous to repute. All round color schemes, combinations, designs, fro are bizarre varieties fight issues possessions were pleasing or apologetic is carry on with local [url=http://www.freewebs.com/bonoo]bet365 bono[/url] littlest scope. Connect can't yon these underpinning brands B such issues are addressed increased by [url=http://b3pt.blogspot.com]bet365[/url] singular alongside brands.

  • louis vuitton belts cheap authentic xguqgp

    Posted by Achiegaic on 05/19/2013 05:03am

    http://louisvuittontaschen.citationguide.net 40497 722340louis vuitton luggage sets louis vuitton shoes men products hermes perfume caleche jual hermes birkin original

  • 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

  • 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.

  • 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

  • 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

  • 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

  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date