38

I'm looking for a good tutorial on zlib. I'm interested only in decompressing the archives. I also want to know how I can access a desired file inside an archive, preferably by filename alone, if that can be done in zlib at all.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • 1
    Preferably, yes, but not necessarily. – Paul Manta Mar 19 '11 at 13:08
  • 5
    Be aware that zlib is just for (de)compressing a data stream. It's not for dealing with archives or collections of files as zip or tar does, though such other formats might use zlib internally for some of its functioning. – nos Jul 16 '12 at 20:07
  • Look at [libarchive](https://www.libarchive.org) for working with archives of multiple files. – Mark Adler Feb 02 '23 at 22:52

5 Answers5

39

Well there are many zlib articles , tips and tutorials. Some of them are

1) Bobobobo's Blog

This article basically tells you how to use zlib, and there is a snippet of code that will get you going.

This project shows you how to use zlib. Its a console project, because there's no need to create a window to demonstrate use of zlib.

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

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.

Since its in C language, it will be most beneficial to your requirements.

Last, you can use this too available in zlib. From the manual, Utility Functions:

ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
                                   const Bytef *source, uLong sourceLen));
blackgreen
  • 34,072
  • 23
  • 111
  • 129
Muhammad Shahab
  • 4,187
  • 4
  • 34
  • 44
8

See http://zlib.net/zlib_how.html

Derick Schoonbee
  • 2,971
  • 1
  • 23
  • 39
2

http://www.boost.org/doc/libs/1_48_0/libs/iostreams/doc/classes/zlib.html

Another option worth mentioning here is boost. Note you must compile boost with special flags for zlib support.

Ben
  • 669
  • 8
  • 14
2

The simplest way to use zlib is in C++ with

https://github.com/rudi-cilibrasi/zlibcomplete

The zlibcomplete library makes it easy because you don't need to do any raw pointer manipulation whatsoever. It is based on RAII (Resource Allocation is Initialization) which means that all the dynamic allocation and deallocation stuff happens automatically in the constructors.

It is better than the Boost zlib wrapper because it supports flush (necessary for interactive network protocols) and is simpler to use. The zlibcomplete library uses only regular std::string to send and receive data so no advanced knowledge is required.

Rudi Cilibrasi
  • 885
  • 4
  • 12
2

The simplest approach is to read zpipe.c, which has a walk-through in zlib documentation.

It is tiny and comprehensive, implements and illustrates compression and decompression. You can just copy and paste and modify the data stream stuff, for example you may not read and write to files but to memory.

One thing to notice: if you're handling GZIP (instead of deflate), which you may find when making a HTTP request, you need to replace inflateInit(&strm) with inflateInit2(&strm, 16 + MAX_WBITS), since GZIP has a bigger header.

user26742873
  • 919
  • 6
  • 21