blob: c78523b1ff02592bd103a44b772af6f39f40e05b [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{zlib} ---
Fred Drakeb11d1081999-04-21 18:44:41 +00002 Compression compatible with \program{gzip}}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebbac4321999-02-20 00:14:17 +00004\declaremodule{builtin}{zlib}
Fred Drake08caa961998-07-27 22:08:49 +00005\modulesynopsis{Low-level interface to compression and decompression
Fred Drakeb11d1081999-04-21 18:44:41 +00006 routines compatible with \program{gzip}.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum04bc9d61997-04-30 18:12:27 +00008
9For applications that require data compression, the functions in this
Fred Drake8a254b51998-04-09 15:41:44 +000010module allow compression and decompression, using the zlib library.
11The zlib library has its own home page at
Fred Drakeb037d332001-06-25 15:30:13 +000012\url{http://www.gzip.org/zlib/}. Version 1.1.3 is the
Fred Drake315b9e02000-09-16 06:18:26 +000013most recent version as of September 2000; use a later version if one
14is available. There are known incompatibilities between the Python
15module and earlier versions of the zlib library.
Jeremy Hylton45b0aed1999-04-05 21:55:21 +000016
Fred Drake74810d51998-04-03 06:49:26 +000017The available exception and functions in this module are:
Guido van Rossum04bc9d61997-04-30 18:12:27 +000018
Fred Drake74810d51998-04-03 06:49:26 +000019\begin{excdesc}{error}
20 Exception raised on compression and decompression errors.
21\end{excdesc}
22
23
Fred Drakecce10901998-03-17 06:33:25 +000024\begin{funcdesc}{adler32}{string\optional{, value}}
Guido van Rossum04bc9d61997-04-30 18:12:27 +000025 Computes a Adler-32 checksum of \var{string}. (An Adler-32
26 checksum is almost as reliable as a CRC32 but can be computed much
27 more quickly.) If \var{value} is present, it is used as the
28 starting value of the checksum; otherwise, a fixed default value is
29 used. This allows computing a running checksum over the
30 concatenation of several input strings. The algorithm is not
31 cryptographically strong, and should not be used for
32 authentication or digital signatures.
33\end{funcdesc}
34
Fred Drakecce10901998-03-17 06:33:25 +000035\begin{funcdesc}{compress}{string\optional{, level}}
Fred Drake59160701998-06-19 21:18:28 +000036 Compresses the data in \var{string}, returning a string contained
37 compressed data. \var{level} is an integer from \code{1} to
38 \code{9} controlling the level of compression; \code{1} is fastest
39 and produces the least compression, \code{9} is slowest and produces
40 the most. The default value is \code{6}. Raises the
41 \exception{error} exception if any error occurs.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000042\end{funcdesc}
43
44\begin{funcdesc}{compressobj}{\optional{level}}
Fred Drakeed797831998-01-22 16:11:18 +000045 Returns a compression object, to be used for compressing data streams
Guido van Rossum04bc9d61997-04-30 18:12:27 +000046 that won't fit into memory at once. \var{level} is an integer from
Fred Drakeed797831998-01-22 16:11:18 +000047 \code{1} to \code{9} controlling the level of compression; \code{1} is
48 fastest and produces the least compression, \code{9} is slowest and
49 produces the most. The default value is \code{6}.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000050\end{funcdesc}
51
Fred Drakecce10901998-03-17 06:33:25 +000052\begin{funcdesc}{crc32}{string\optional{, value}}
Fred Drake74810d51998-04-03 06:49:26 +000053 Computes a CRC (Cyclic Redundancy Check)%
54 \index{Cyclic Redundancy Check}
Fred Drakeb208f121998-04-04 06:28:54 +000055 \index{checksum!Cyclic Redundancy Check}
Fred Drake74810d51998-04-03 06:49:26 +000056 checksum of \var{string}. If
57 \var{value} is present, it is used as the starting value of the
58 checksum; otherwise, a fixed default value is used. This allows
59 computing a running checksum over the concatenation of several
60 input strings. The algorithm is not cryptographically strong, and
61 should not be used for authentication or digital signatures.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000062\end{funcdesc}
63
Fred Drake38e5d272000-04-03 20:13:55 +000064\begin{funcdesc}{decompress}{string\optional{, wbits\optional{, bufsize}}}
Fred Drake59160701998-06-19 21:18:28 +000065 Decompresses the data in \var{string}, returning a string containing
66 the uncompressed data. The \var{wbits} parameter controls the size of
Fred Drake38e5d272000-04-03 20:13:55 +000067 the window buffer. If \var{bufsize} is given, it is used as the
Fred Drake59160701998-06-19 21:18:28 +000068 initial size of the output buffer. Raises the \exception{error}
69 exception if any error occurs.
Fred Drake38e5d272000-04-03 20:13:55 +000070
71The absolute value of \var{wbits} is the base two logarithm of the
72size of the history buffer (the ``window size'') used when compressing
73data. Its absolute value should be between 8 and 15 for the most
74recent versions of the zlib library, larger values resulting in better
75compression at the expense of greater memory usage. The default value
76is 15. When \var{wbits} is negative, the standard
77\program{gzip} header is suppressed; this is an undocumented feature
78of the zlib library, used for compatibility with \program{unzip}'s
79compression file format.
80
81\var{bufsize} is the initial size of the buffer used to hold
82decompressed data. If more space is required, the buffer size will be
83increased as needed, so you don't have to get this value exactly
84right; tuning it will only save a few calls to \cfunction{malloc()}. The
85default size is 16384.
86
Guido van Rossum04bc9d61997-04-30 18:12:27 +000087\end{funcdesc}
88
89\begin{funcdesc}{decompressobj}{\optional{wbits}}
Fred Drakebc524c42001-04-18 20:16:51 +000090 Returns a decompression object, to be used for decompressing data
Fred Drake59160701998-06-19 21:18:28 +000091 streams that won't fit into memory at once. The \var{wbits}
92 parameter controls the size of the window buffer.
Guido van Rossum04bc9d61997-04-30 18:12:27 +000093\end{funcdesc}
94
95Compression objects support the following methods:
96
Fred Drake74810d51998-04-03 06:49:26 +000097\begin{methoddesc}[Compress]{compress}{string}
Guido van Rossum04bc9d61997-04-30 18:12:27 +000098Compress \var{string}, returning a string containing compressed data
99for at least part of the data in \var{string}. This data should be
100concatenated to the output produced by any preceding calls to the
Fred Drakeed797831998-01-22 16:11:18 +0000101\method{compress()} method. Some input may be kept in internal buffers
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000102for later processing.
Fred Drake74810d51998-04-03 06:49:26 +0000103\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000104
Andrew M. Kuchlingf07c3281998-12-31 21:14:23 +0000105\begin{methoddesc}[Compress]{flush}{\optional{mode}}
106All pending input is processed, and a string containing the remaining
107compressed output is returned. \var{mode} can be selected from the
108constants \constant{Z_SYNC_FLUSH}, \constant{Z_FULL_FLUSH}, or
109\constant{Z_FINISH}, defaulting to \constant{Z_FINISH}. \constant{Z_SYNC_FLUSH} and
110\constant{Z_FULL_FLUSH} allow compressing further strings of data and
111are used to allow partial error recovery on decompression, while
112\constant{Z_FINISH} finishes the compressed stream and
113prevents compressing any more data. After calling
114\method{flush()} with \var{mode} set to \constant{Z_FINISH}, the
Fred Drakeed797831998-01-22 16:11:18 +0000115\method{compress()} method cannot be called again; the only realistic
Andrew M. Kuchlingf07c3281998-12-31 21:14:23 +0000116action is to delete the object.
Fred Drake74810d51998-04-03 06:49:26 +0000117\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000118
Fred Drake38e5d272000-04-03 20:13:55 +0000119Decompression objects support the following methods, and a single attribute:
120
121\begin{memberdesc}{unused_data}
122A string which contains any unused data from the last string fed to
123this decompression object. If the whole string turned out to contain
124compressed data, this is \code{""}, the empty string.
125
126The only way to determine where a string of compressed data ends is by
127actually decompressing it. This means that when compressed data is
128contained part of a larger file, you can only find the end of it by
129reading data and feeding it into a decompression object's
130\method{decompress} method until the \member{unused_data} attribute is
131no longer the empty string.
132\end{memberdesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000133
Fred Drake74810d51998-04-03 06:49:26 +0000134\begin{methoddesc}[Decompress]{decompress}{string}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000135Decompress \var{string}, returning a string containing the
136uncompressed data corresponding to at least part of the data in
137\var{string}. This data should be concatenated to the output produced
138by any preceding calls to the
Fred Drakeed797831998-01-22 16:11:18 +0000139\method{decompress()} method. Some of the input data may be preserved
Guido van Rossum412154f1997-04-30 19:39:21 +0000140in internal buffers for later processing.
Fred Drake74810d51998-04-03 06:49:26 +0000141\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000142
Fred Drake74810d51998-04-03 06:49:26 +0000143\begin{methoddesc}[Decompress]{flush}{}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000144All pending input is processed, and a string containing the remaining
Fred Drakeed797831998-01-22 16:11:18 +0000145uncompressed output is returned. After calling \method{flush()}, the
146\method{decompress()} method cannot be called again; the only realistic
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000147action is to delete the object.
Fred Drake74810d51998-04-03 06:49:26 +0000148\end{methoddesc}
Guido van Rossum04bc9d61997-04-30 18:12:27 +0000149
Guido van Rossume47da0a1997-07-17 16:34:52 +0000150\begin{seealso}
Fred Drakeba0a9892000-10-18 17:43:06 +0000151 \seemodule{gzip}{Reading and writing \program{gzip}-format files.}
Fred Drakeb037d332001-06-25 15:30:13 +0000152 \seeurl{http://www.gzip.org/zlib/}{The zlib library home page.}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000153\end{seealso}