blob: d31af04b84bebb02ef756adf6c46479c409c75b4 [file] [log] [blame]
Jeremy Hylton45b0aed1999-04-05 21:55:21 +00001% XXX The module has been extended (by Jeremy and Andrew) but this
2% documentation is incorrect in some cases.
Guido van Rossumeb0f0661997-12-30 20:38:16 +00003
Fred Drake295da241998-08-10 19:42:37 +00004\section{\module{zlib} ---
Fred Drakeb11d1081999-04-21 18:44:41 +00005 Compression compatible with \program{gzip}}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Fred Drakebbac4321999-02-20 00:14:17 +00007\declaremodule{builtin}{zlib}
Fred Drake08caa961998-07-27 22:08:49 +00008\modulesynopsis{Low-level interface to compression and decompression
Fred Drakeb11d1081999-04-21 18:44:41 +00009 routines compatible with \program{gzip}.}
Fred Drakeb91e9341998-07-23 17:59:49 +000010
Guido van Rossum04bc9d61997-04-30 18:12:27 +000011
12For applications that require data compression, the functions in this
Fred Drake8a254b51998-04-09 15:41:44 +000013module allow compression and decompression, using the zlib library.
14The zlib library has its own home page at
Jeremy Hylton45b0aed1999-04-05 21:55:21 +000015\url{http://www.cdrom.com/pub/infozip/zlib/}. Version 1.1.3 is the
Fred Drakeb11d1081999-04-21 18:44:41 +000016most recent version as of April 1999; use a later version if one
Jeremy Hylton45b0aed1999-04-05 21:55:21 +000017is available. There are known incompatibilities between the Python
18module and earlier versions of the zlib library.
19
20The documentation for this module is woefully out of date. In some
21cases, the doc strings have been updated more recently. In other
22cases, they are both stale.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000023
Fred Drake74810d51998-04-03 06:49:26 +000024The available exception and functions in this module are:
Guido van Rossum04bc9d61997-04-30 18:12:27 +000025
Fred Drake74810d51998-04-03 06:49:26 +000026\begin{excdesc}{error}
27 Exception raised on compression and decompression errors.
28\end{excdesc}
29
30
Fred Drakecce10901998-03-17 06:33:25 +000031\begin{funcdesc}{adler32}{string\optional{, value}}
Guido van Rossum04bc9d61997-04-30 18:12:27 +000032 Computes a Adler-32 checksum of \var{string}. (An Adler-32
33 checksum is almost as reliable as a CRC32 but can be computed much
34 more quickly.) If \var{value} is present, it is used as the
35 starting value of the checksum; otherwise, a fixed default value is
36 used. This allows computing a running checksum over the
37 concatenation of several input strings. The algorithm is not
38 cryptographically strong, and should not be used for
39 authentication or digital signatures.
40\end{funcdesc}
41
Fred Drakecce10901998-03-17 06:33:25 +000042\begin{funcdesc}{compress}{string\optional{, level}}
Fred Drake59160701998-06-19 21:18:28 +000043 Compresses the data in \var{string}, returning a string contained
44 compressed data. \var{level} is an integer from \code{1} to
45 \code{9} controlling the level of compression; \code{1} is fastest
46 and produces the least compression, \code{9} is slowest and produces
47 the most. The default value is \code{6}. Raises the
48 \exception{error} exception if any error occurs.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000049\end{funcdesc}
50
51\begin{funcdesc}{compressobj}{\optional{level}}
Fred Drakeed797831998-01-22 16:11:18 +000052 Returns a compression object, to be used for compressing data streams
Guido van Rossum04bc9d61997-04-30 18:12:27 +000053 that won't fit into memory at once. \var{level} is an integer from
Fred Drakeed797831998-01-22 16:11:18 +000054 \code{1} to \code{9} controlling the level of compression; \code{1} is
55 fastest and produces the least compression, \code{9} is slowest and
56 produces the most. The default value is \code{6}.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000057\end{funcdesc}
58
Fred Drakecce10901998-03-17 06:33:25 +000059\begin{funcdesc}{crc32}{string\optional{, value}}
Fred Drake74810d51998-04-03 06:49:26 +000060 Computes a CRC (Cyclic Redundancy Check)%
61 \index{Cyclic Redundancy Check}
Fred Drakeb208f121998-04-04 06:28:54 +000062 \index{checksum!Cyclic Redundancy Check}
Fred Drake74810d51998-04-03 06:49:26 +000063 checksum of \var{string}. If
64 \var{value} is present, it is used as the starting value of the
65 checksum; otherwise, a fixed default value is used. This allows
66 computing a running checksum over the concatenation of several
67 input strings. The algorithm is not cryptographically strong, and
68 should not be used for authentication or digital signatures.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000069\end{funcdesc}
70
Fred Drake59160701998-06-19 21:18:28 +000071\begin{funcdesc}{decompress}{string\optional{, wbits\optional{, buffsize}}}
72 Decompresses the data in \var{string}, returning a string containing
73 the uncompressed data. The \var{wbits} parameter controls the size of
74 the window buffer. If \var{buffsize} is given, it is used as the
75 initial size of the output buffer. Raises the \exception{error}
76 exception if any error occurs.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000077\end{funcdesc}
78
79\begin{funcdesc}{decompressobj}{\optional{wbits}}
Fred Drake59160701998-06-19 21:18:28 +000080 Returns a compression object, to be used for decompressing data
81 streams that won't fit into memory at once. The \var{wbits}
82 parameter controls the size of the window buffer.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000083\end{funcdesc}
84
85Compression objects support the following methods:
86
Fred Drake74810d51998-04-03 06:49:26 +000087\begin{methoddesc}[Compress]{compress}{string}
Guido van Rossum04bc9d61997-04-30 18:12:27 +000088Compress \var{string}, returning a string containing compressed data
89for at least part of the data in \var{string}. This data should be
90concatenated to the output produced by any preceding calls to the
Fred Drakeed797831998-01-22 16:11:18 +000091\method{compress()} method. Some input may be kept in internal buffers
Guido van Rossum04bc9d61997-04-30 18:12:27 +000092for later processing.
Fred Drake74810d51998-04-03 06:49:26 +000093\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +000094
Andrew M. Kuchlingf07c3281998-12-31 21:14:23 +000095\begin{methoddesc}[Compress]{flush}{\optional{mode}}
96All pending input is processed, and a string containing the remaining
97compressed output is returned. \var{mode} can be selected from the
98constants \constant{Z_SYNC_FLUSH}, \constant{Z_FULL_FLUSH}, or
99\constant{Z_FINISH}, defaulting to \constant{Z_FINISH}. \constant{Z_SYNC_FLUSH} and
100\constant{Z_FULL_FLUSH} allow compressing further strings of data and
101are used to allow partial error recovery on decompression, while
102\constant{Z_FINISH} finishes the compressed stream and
103prevents compressing any more data. After calling
104\method{flush()} with \var{mode} set to \constant{Z_FINISH}, the
Fred Drakeed797831998-01-22 16:11:18 +0000105\method{compress()} method cannot be called again; the only realistic
Andrew M. Kuchlingf07c3281998-12-31 21:14:23 +0000106action is to delete the object.
Fred Drake74810d51998-04-03 06:49:26 +0000107\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000108
109Decompression objects support the following methods:
110
Fred Drake74810d51998-04-03 06:49:26 +0000111\begin{methoddesc}[Decompress]{decompress}{string}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000112Decompress \var{string}, returning a string containing the
113uncompressed data corresponding to at least part of the data in
114\var{string}. This data should be concatenated to the output produced
115by any preceding calls to the
Fred Drakeed797831998-01-22 16:11:18 +0000116\method{decompress()} method. Some of the input data may be preserved
Guido van Rossum412154f1997-04-30 19:39:21 +0000117in internal buffers for later processing.
Fred Drake74810d51998-04-03 06:49:26 +0000118\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000119
Fred Drake74810d51998-04-03 06:49:26 +0000120\begin{methoddesc}[Decompress]{flush}{}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000121All pending input is processed, and a string containing the remaining
Fred Drakeed797831998-01-22 16:11:18 +0000122uncompressed output is returned. After calling \method{flush()}, the
123\method{decompress()} method cannot be called again; the only realistic
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000124action is to delete the object.
Fred Drake74810d51998-04-03 06:49:26 +0000125\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000126
Guido van Rossume47da0a1997-07-17 16:34:52 +0000127\begin{seealso}
Fred Drakeb11d1081999-04-21 18:44:41 +0000128 \seemodule{gzip}{reading and writing \program{gzip}-format files}
129 \seetext{The zlib library home page is located at
130 \url{http://www.cdrom.com/pub/infozip/zlib/}.}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000131\end{seealso}