blob: bd08f7f6a09f41c346579da2e28a1cd01183e4fa [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`zlib` --- Compression compatible with :program:`gzip`
3===========================================================
4
5.. module:: zlib
6 :synopsis: Low-level interface to compression and decompression routines compatible with
7 gzip.
8
9
10For applications that require data compression, the functions in this module
11allow compression and decompression, using the zlib library. The zlib library
12has its own home page at http://www.zlib.net. There are known
13incompatibilities between the Python module and versions of the zlib library
14earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using
151.1.4 or later.
16
17zlib's functions have many options and often need to be used in a particular
18order. This documentation doesn't attempt to cover all of the permutations;
19consult the zlib manual at http://www.zlib.net/manual.html for authoritative
20information.
21
Éric Araujoc3cc2ac2012-02-26 01:10:14 +010022For reading and writing ``.gz`` files see the :mod:`gzip` module.
Mark Summerfieldaea6e592007-11-05 09:22:48 +000023
Georg Brandl8ec7f652007-08-15 14:28:01 +000024The available exception and functions in this module are:
25
26
27.. exception:: error
28
29 Exception raised on compression and decompression errors.
30
31
Gregory P. Smith987735c2009-01-11 17:57:54 +000032.. function:: adler32(data[, value])
Georg Brandl8ec7f652007-08-15 14:28:01 +000033
Gregory P. Smith987735c2009-01-11 17:57:54 +000034 Computes a Adler-32 checksum of *data*. (An Adler-32 checksum is almost as
Georg Brandl8ec7f652007-08-15 14:28:01 +000035 reliable as a CRC32 but can be computed much more quickly.) If *value* is
36 present, it is used as the starting value of the checksum; otherwise, a fixed
37 default value is used. This allows computing a running checksum over the
Gregory P. Smith987735c2009-01-11 17:57:54 +000038 concatenation of several inputs. The algorithm is not cryptographically
Georg Brandl8ec7f652007-08-15 14:28:01 +000039 strong, and should not be used for authentication or digital signatures. Since
40 the algorithm is designed for use as a checksum algorithm, it is not suitable
41 for use as a general hash algorithm.
42
Gregory P. Smithf48f9d32008-03-17 18:48:05 +000043 This function always returns an integer object.
44
Gregory P. Smith987735c2009-01-11 17:57:54 +000045.. note::
46 To generate the same numeric value across all Python versions and
47 platforms use adler32(data) & 0xffffffff. If you are only using
48 the checksum in packed binary format this is not necessary as the
Gregory P. Smith86cc5022009-02-01 00:24:21 +000049 return value is the correct 32bit binary representation
Gregory P. Smith987735c2009-01-11 17:57:54 +000050 regardless of sign.
51
52.. versionchanged:: 2.6
Gregory P. Smith86cc5022009-02-01 00:24:21 +000053 The return value is in the range [-2**31, 2**31-1]
54 regardless of platform. In older versions the value is
Gregory P. Smith987735c2009-01-11 17:57:54 +000055 signed on some platforms and unsigned on others.
56
57.. versionchanged:: 3.0
Gregory P. Smith86cc5022009-02-01 00:24:21 +000058 The return value is unsigned and in the range [0, 2**32-1]
Gregory P. Smith987735c2009-01-11 17:57:54 +000059 regardless of platform.
Gregory P. Smithf48f9d32008-03-17 18:48:05 +000060
Georg Brandl8ec7f652007-08-15 14:28:01 +000061
62.. function:: compress(string[, level])
63
64 Compresses the data in *string*, returning a string contained compressed data.
65 *level* is an integer from ``1`` to ``9`` controlling the level of compression;
66 ``1`` is fastest and produces the least compression, ``9`` is slowest and
67 produces the most. The default value is ``6``. Raises the :exc:`error`
68 exception if any error occurs.
69
70
71.. function:: compressobj([level])
72
73 Returns a compression object, to be used for compressing data streams that won't
74 fit into memory at once. *level* is an integer from ``1`` to ``9`` controlling
75 the level of compression; ``1`` is fastest and produces the least compression,
76 ``9`` is slowest and produces the most. The default value is ``6``.
77
78
Gregory P. Smith987735c2009-01-11 17:57:54 +000079.. function:: crc32(data[, value])
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
81 .. index::
82 single: Cyclic Redundancy Check
83 single: checksum; Cyclic Redundancy Check
84
Gregory P. Smith987735c2009-01-11 17:57:54 +000085 Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is
Georg Brandl8ec7f652007-08-15 14:28:01 +000086 present, it is used as the starting value of the checksum; otherwise, a fixed
87 default value is used. This allows computing a running checksum over the
Gregory P. Smith987735c2009-01-11 17:57:54 +000088 concatenation of several inputs. The algorithm is not cryptographically
Georg Brandl8ec7f652007-08-15 14:28:01 +000089 strong, and should not be used for authentication or digital signatures. Since
90 the algorithm is designed for use as a checksum algorithm, it is not suitable
91 for use as a general hash algorithm.
92
Gregory P. Smithf48f9d32008-03-17 18:48:05 +000093 This function always returns an integer object.
94
Gregory P. Smith987735c2009-01-11 17:57:54 +000095.. note::
96 To generate the same numeric value across all Python versions and
97 platforms use crc32(data) & 0xffffffff. If you are only using
98 the checksum in packed binary format this is not necessary as the
Gregory P. Smith86cc5022009-02-01 00:24:21 +000099 return value is the correct 32bit binary representation
Gregory P. Smith987735c2009-01-11 17:57:54 +0000100 regardless of sign.
101
102.. versionchanged:: 2.6
Gregory P. Smith86cc5022009-02-01 00:24:21 +0000103 The return value is in the range [-2**31, 2**31-1]
Gregory P. Smith987735c2009-01-11 17:57:54 +0000104 regardless of platform. In older versions the value would be
105 signed on some platforms and unsigned on others.
106
107.. versionchanged:: 3.0
Gregory P. Smith86cc5022009-02-01 00:24:21 +0000108 The return value is unsigned and in the range [0, 2**32-1]
Gregory P. Smith987735c2009-01-11 17:57:54 +0000109 regardless of platform.
Gregory P. Smithf48f9d32008-03-17 18:48:05 +0000110
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
112.. function:: decompress(string[, wbits[, bufsize]])
113
114 Decompresses the data in *string*, returning a string containing the
115 uncompressed data. The *wbits* parameter controls the size of the window
Andrew M. Kuchling66dab172010-03-01 19:51:43 +0000116 buffer, and is discussed further below.
117 If *bufsize* is given, it is used as the initial size of the output
Georg Brandl8ec7f652007-08-15 14:28:01 +0000118 buffer. Raises the :exc:`error` exception if any error occurs.
119
120 The absolute value of *wbits* is the base two logarithm of the size of the
121 history buffer (the "window size") used when compressing data. Its absolute
122 value should be between 8 and 15 for the most recent versions of the zlib
123 library, larger values resulting in better compression at the expense of greater
Andrew M. Kuchling66dab172010-03-01 19:51:43 +0000124 memory usage. When decompressing a stream, *wbits* must not be smaller
125 than the size originally used to compress the stream; using a too-small
126 value will result in an exception. The default value is therefore the
127 highest value, 15. When *wbits* is negative, the standard
Jesus Ceac3ce9e32010-05-03 16:09:21 +0000128 :program:`gzip` header is suppressed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000129
130 *bufsize* is the initial size of the buffer used to hold decompressed data. If
131 more space is required, the buffer size will be increased as needed, so you
132 don't have to get this value exactly right; tuning it will only save a few calls
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100133 to :c:func:`malloc`. The default size is 16384.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134
135
136.. function:: decompressobj([wbits])
137
138 Returns a decompression object, to be used for decompressing data streams that
139 won't fit into memory at once. The *wbits* parameter controls the size of the
140 window buffer.
141
142Compression objects support the following methods:
143
144
145.. method:: Compress.compress(string)
146
147 Compress *string*, returning a string containing compressed data for at least
148 part of the data in *string*. This data should be concatenated to the output
149 produced by any preceding calls to the :meth:`compress` method. Some input may
150 be kept in internal buffers for later processing.
151
152
153.. method:: Compress.flush([mode])
154
155 All pending input is processed, and a string containing the remaining compressed
156 output is returned. *mode* can be selected from the constants
157 :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
158 defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
159 :const:`Z_FULL_FLUSH` allow compressing further strings of data, while
160 :const:`Z_FINISH` finishes the compressed stream and prevents compressing any
161 more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
162 the :meth:`compress` method cannot be called again; the only realistic action is
163 to delete the object.
164
165
166.. method:: Compress.copy()
167
168 Returns a copy of the compression object. This can be used to efficiently
169 compress a set of data that share a common initial prefix.
170
171 .. versionadded:: 2.5
172
173Decompression objects support the following methods, and two attributes:
174
175
176.. attribute:: Decompress.unused_data
177
178 A string which contains any bytes past the end of the compressed data. That is,
179 this remains ``""`` until the last byte that contains compression data is
180 available. If the whole string turned out to contain compressed data, this is
181 ``""``, the empty string.
182
183 The only way to determine where a string of compressed data ends is by actually
184 decompressing it. This means that when compressed data is contained part of a
185 larger file, you can only find the end of it by reading data and feeding it
186 followed by some non-empty string into a decompression object's
187 :meth:`decompress` method until the :attr:`unused_data` attribute is no longer
188 the empty string.
189
190
191.. attribute:: Decompress.unconsumed_tail
192
193 A string that contains any data that was not consumed by the last
194 :meth:`decompress` call because it exceeded the limit for the uncompressed data
195 buffer. This data has not yet been seen by the zlib machinery, so you must feed
196 it (possibly with further data concatenated to it) back to a subsequent
197 :meth:`decompress` method call in order to get correct output.
198
199
200.. method:: Decompress.decompress(string[, max_length])
201
202 Decompress *string*, returning a string containing the uncompressed data
203 corresponding to at least part of the data in *string*. This data should be
204 concatenated to the output produced by any preceding calls to the
205 :meth:`decompress` method. Some of the input data may be preserved in internal
206 buffers for later processing.
207
208 If the optional parameter *max_length* is supplied then the return value will be
209 no longer than *max_length*. This may mean that not all of the compressed input
210 can be processed; and unconsumed data will be stored in the attribute
211 :attr:`unconsumed_tail`. This string must be passed to a subsequent call to
212 :meth:`decompress` if decompression is to continue. If *max_length* is not
213 supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is an
214 empty string.
215
216
217.. method:: Decompress.flush([length])
218
219 All pending input is processed, and a string containing the remaining
220 uncompressed output is returned. After calling :meth:`flush`, the
221 :meth:`decompress` method cannot be called again; the only realistic action is
222 to delete the object.
223
224 The optional parameter *length* sets the initial size of the output buffer.
225
226
227.. method:: Decompress.copy()
228
229 Returns a copy of the decompression object. This can be used to save the state
230 of the decompressor midway through the data stream in order to speed up random
231 seeks into the stream at a future point.
232
233 .. versionadded:: 2.5
234
235
236.. seealso::
237
238 Module :mod:`gzip`
239 Reading and writing :program:`gzip`\ -format files.
240
241 http://www.zlib.net
242 The zlib library home page.
243
244 http://www.zlib.net/manual.html
245 The zlib manual explains the semantics and usage of the library's many
246 functions.
247