Python Library -ZLIB

Python Library -ZLIB


Zlib was written by Jean-loup Gailly (compression) and Mark Adler (decompression).                          

The Python zlib library provides a Python interface to the zlib C library, which is a higher-level abstraction for the DEFLATE lossless  compression algorithm.

The main use of the zlib library is in applications that require compression and decompression of arbitrary data, whether it be a string, structured in-memory content, or files.

The 'zlib' compression library provides in-memory compression and decompression functions, including integrity checks of the uncompressed data. This version of the library supports only one compression method (deflation) but other algorithms will be added later and will have the same stream interface.

Compression is useful not only for back up and storage, but also for transferring files in networks.

Compression can be done in a single step if the buffers are large enough (for example if an input file is mmap'ed), or can be done byrepeated calls of the compression function. In the latter case, the application must provide more input and/or consume the output (providing more output space) before each call

The library also supports reading and writing files in gzip(.gz) format with an interface similar to that of stdio. The library does not install any signal handler. The decoder checks the consistency of the compressed data, so the library should never crash even in case of corrupted input.


In Ubuntu, the zlib package is zlib1g

You may install them by using this command  --   $ sudo apt install zlib1g

If you would install the development package use this --   $ sudo apt install zlib1g-dev

Below the steps are given for development package .

1.            $ sudo apt-get install zlib1g-dev

2.            $ wget

3.            $ tar xfz Python-2.7.10.tgz

4.            $ cd Python-2.7.9/

5.            $ ./configure --prefix /path/to/python/ --enable-ipv6

               (in my case path was /user/local )

6.            $ make

7.            $ sudo make install

               check: $ python2.7 -c "import zlib; print(zlib.version)"


1.      1. Utility functions

2.      2. Basic functions

3.      3. Advance functions

I have given all the list of utility functions only , because  advance functions are needed  only in some special applications.

Utility functions

The following utility functions are implemented on top of the basic stream-oriented functions. To simplify the interface, some default options are assumed (compression level and memory usage, standard memory allocation functions). The source code of these utility functions can easily be modified if you need special options.

Funciton list

->   int compress (Bytef *dest, uLongf *destLen, const Bytef*source, uLong sourceLen);

->   int compress2(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen,int level);

->   int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);

->   typedef voidp gzFile;

->   gzFile gzopen (const char *path, const char *mode);

->   gzFile gzdopen (int fd, const char*mode);

->   int gzsetparams (gzFile file, int level, int strategy);

->   int gzread (gzFile file, voidp buf, unsigned len);

->   int gzwrite (gzFile file, const voidp buf, unsigned len);

->   int VA gzprintf (gzFile file, const char *format, ...);

->   int gzputs (gzFile file, const char*s);

->   char * gzgets (gzFile file, char *buf, int len);

->   int gzputc (gzFile file, int c);

->   int gzgetc (gzFile file);

->   int gzflush (gzFile file, int flush);

->   z_off_t gzseek (gzFile file, z_off_t offset, int whence);

->   z_off_t gztell (gzFile file);

->   int gzrewind (gzFile file);

->   int gzeof (gzFile file);

->   int gzclose (gzFile file);

->   const char * gzerror (gzFile file, int*errnum);