blob: 731023e139ddadc94dec8d06fbb639f68dee12fe [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`zlib` --- Compression compatible with :program:`gzip`
2===========================================================
3
4.. module:: zlib
Georg Brandl7f01a132009-09-16 15:58:14 +00005 :synopsis: Low-level interface to compression and decompression routines
6 compatible with gzip.
Georg Brandl116aa622007-08-15 14:28:22 +00007
8
9For applications that require data compression, the functions in this module
10allow compression and decompression, using the zlib library. The zlib library
11has its own home page at http://www.zlib.net. There are known
12incompatibilities between the Python module and versions of the zlib library
13earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using
141.1.4 or later.
15
16zlib's functions have many options and often need to be used in a particular
17order. This documentation doesn't attempt to cover all of the permutations;
18consult the zlib manual at http://www.zlib.net/manual.html for authoritative
19information.
20
Éric Araujof2fbb9c2012-01-16 16:55:55 +010021For reading and writing ``.gz`` files see the :mod:`gzip` module.
Guido van Rossum77677112007-11-05 19:43:04 +000022
Georg Brandl116aa622007-08-15 14:28:22 +000023The available exception and functions in this module are:
24
25
26.. exception:: error
27
28 Exception raised on compression and decompression errors.
29
30
Benjamin Peterson058e31e2009-01-16 03:54:08 +000031.. function:: adler32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +000032
Benjamin Peterson058e31e2009-01-16 03:54:08 +000033 Computes a Adler-32 checksum of *data*. (An Adler-32 checksum is almost as
Georg Brandl116aa622007-08-15 14:28:22 +000034 reliable as a CRC32 but can be computed much more quickly.) If *value* is
35 present, it is used as the starting value of the checksum; otherwise, a fixed
36 default value is used. This allows computing a running checksum over the
Benjamin Peterson058e31e2009-01-16 03:54:08 +000037 concatenation of several inputs. The algorithm is not cryptographically
Georg Brandl116aa622007-08-15 14:28:22 +000038 strong, and should not be used for authentication or digital signatures. Since
39 the algorithm is designed for use as a checksum algorithm, it is not suitable
40 for use as a general hash algorithm.
41
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000042 Always returns an unsigned 32-bit integer.
43
Benjamin Peterson058e31e2009-01-16 03:54:08 +000044.. note::
45 To generate the same numeric value across all Python versions and
46 platforms use adler32(data) & 0xffffffff. If you are only using
47 the checksum in packed binary format this is not necessary as the
Gregory P. Smithfa6cf392009-02-01 00:30:50 +000048 return value is the correct 32bit binary representation
Benjamin Peterson058e31e2009-01-16 03:54:08 +000049 regardless of sign.
50
Georg Brandl116aa622007-08-15 14:28:22 +000051
Georg Brandl4ad934f2011-01-08 21:04:25 +000052.. function:: compress(data[, level])
Georg Brandl116aa622007-08-15 14:28:22 +000053
Georg Brandl4ad934f2011-01-08 21:04:25 +000054 Compresses the bytes in *data*, returning a bytes object containing compressed data.
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010055 *level* is an integer from ``0`` to ``9`` controlling the level of compression;
Georg Brandl116aa622007-08-15 14:28:22 +000056 ``1`` is fastest and produces the least compression, ``9`` is slowest and
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010057 produces the most. ``0`` is no compression. The default value is ``6``.
58 Raises the :exc:`error` exception if any error occurs.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60
Georg Brandl9aae9e52012-06-26 08:51:17 +020061.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
Georg Brandl116aa622007-08-15 14:28:22 +000062
63 Returns a compression object, to be used for compressing data streams that won't
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020064 fit into memory at once.
65
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010066 *level* is the compression level -- an integer from ``0`` to ``9``. A value
Nadeem Vawda2180c972012-06-22 01:40:49 +020067 of ``1`` is fastest and produces the least compression, while a value of
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010068 ``9`` is slowest and produces the most. ``0`` is no compression. The default
Nadeem Vawda19e568d2012-11-11 14:04:14 +010069 value is ``6``.
Nadeem Vawda2180c972012-06-22 01:40:49 +020070
71 *method* is the compression algorithm. Currently, the only supported value is
72 ``DEFLATED``.
73
74 *wbits* is the base two logarithm of the size of the window buffer. This
75 should be an integer from ``8`` to ``15``. Higher values give better
76 compression, but use more memory.
77
78 *memlevel* controls the amount of memory used for internal compression state.
79 Valid values range from ``1`` to ``9``. Higher values using more memory,
80 but are faster and produce smaller output.
81
82 *strategy* is used to tune the compression algorithm. Possible values are
83 ``Z_DEFAULT_STRATEGY``, ``Z_FILTERED``, and ``Z_HUFFMAN_ONLY``.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020084
85 *zdict* is a predefined compression dictionary. This is a sequence of bytes
86 (such as a :class:`bytes` object) containing subsequences that are expected
87 to occur frequently in the data that is to be compressed. Those subsequences
88 that are expected to be most common should come at the end of the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +000089
Georg Brandl9aae9e52012-06-26 08:51:17 +020090 .. versionchanged:: 3.3
91 Added the *method*, *wbits*, *memlevel*, *strategy* and *zdict*
92 parameters.
93
Georg Brandl116aa622007-08-15 14:28:22 +000094
Benjamin Peterson058e31e2009-01-16 03:54:08 +000095.. function:: crc32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +000096
97 .. index::
98 single: Cyclic Redundancy Check
99 single: checksum; Cyclic Redundancy Check
100
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000101 Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is
Georg Brandl116aa622007-08-15 14:28:22 +0000102 present, it is used as the starting value of the checksum; otherwise, a fixed
103 default value is used. This allows computing a running checksum over the
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000104 concatenation of several inputs. The algorithm is not cryptographically
Georg Brandl116aa622007-08-15 14:28:22 +0000105 strong, and should not be used for authentication or digital signatures. Since
106 the algorithm is designed for use as a checksum algorithm, it is not suitable
107 for use as a general hash algorithm.
108
Gregory P. Smithab0d8a12008-03-17 20:24:09 +0000109 Always returns an unsigned 32-bit integer.
110
Georg Brandl9aae9e52012-06-26 08:51:17 +0200111 .. note::
112
113 To generate the same numeric value across all Python versions and
114 platforms, use ``crc32(data) & 0xffffffff``. If you are only using
115 the checksum in packed binary format this is not necessary as the
116 return value is the correct 32-bit binary representation
117 regardless of sign.
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Georg Brandl4ad934f2011-01-08 21:04:25 +0000120.. function:: decompress(data[, wbits[, bufsize]])
Georg Brandl116aa622007-08-15 14:28:22 +0000121
Georg Brandl4ad934f2011-01-08 21:04:25 +0000122 Decompresses the bytes in *data*, returning a bytes object containing the
Georg Brandl116aa622007-08-15 14:28:22 +0000123 uncompressed data. The *wbits* parameter controls the size of the window
Benjamin Peterson2614cda2010-03-21 22:36:19 +0000124 buffer, and is discussed further below.
125 If *bufsize* is given, it is used as the initial size of the output
Georg Brandl116aa622007-08-15 14:28:22 +0000126 buffer. Raises the :exc:`error` exception if any error occurs.
127
128 The absolute value of *wbits* is the base two logarithm of the size of the
129 history buffer (the "window size") used when compressing data. Its absolute
130 value should be between 8 and 15 for the most recent versions of the zlib
131 library, larger values resulting in better compression at the expense of greater
Benjamin Peterson2614cda2010-03-21 22:36:19 +0000132 memory usage. When decompressing a stream, *wbits* must not be smaller
133 than the size originally used to compress the stream; using a too-small
134 value will result in an exception. The default value is therefore the
135 highest value, 15. When *wbits* is negative, the standard
Jesus Ceafb7b6682010-05-03 16:14:58 +0000136 :program:`gzip` header is suppressed.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138 *bufsize* is the initial size of the buffer used to hold decompressed data. If
139 more space is required, the buffer size will be increased as needed, so you
140 don't have to get this value exactly right; tuning it will only save a few calls
Georg Brandl60203b42010-10-06 10:11:56 +0000141 to :c:func:`malloc`. The default size is 16384.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
Georg Brandl9aae9e52012-06-26 08:51:17 +0200144.. function:: decompressobj(wbits=15[, zdict])
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146 Returns a decompression object, to be used for decompressing data streams that
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200147 won't fit into memory at once.
148
149 The *wbits* parameter controls the size of the window buffer.
150
151 The *zdict* parameter specifies a predefined compression dictionary. If
152 provided, this must be the same dictionary as was used by the compressor that
153 produced the data that is to be decompressed.
154
Georg Brandl9aae9e52012-06-26 08:51:17 +0200155 .. note::
156
157 If *zdict* is a mutable object (such as a :class:`bytearray`), you must not
158 modify its contents between the call to :func:`decompressobj` and the first
159 call to the decompressor's ``decompress()`` method.
160
161 .. versionchanged:: 3.3
162 Added the *zdict* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Nadeem Vawda64d25dd2011-09-12 00:04:13 +0200164
Georg Brandl116aa622007-08-15 14:28:22 +0000165Compression objects support the following methods:
166
167
Georg Brandl4ad934f2011-01-08 21:04:25 +0000168.. method:: Compress.compress(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Georg Brandl4ad934f2011-01-08 21:04:25 +0000170 Compress *data*, returning a bytes object containing compressed data for at least
171 part of the data in *data*. This data should be concatenated to the output
Georg Brandl116aa622007-08-15 14:28:22 +0000172 produced by any preceding calls to the :meth:`compress` method. Some input may
173 be kept in internal buffers for later processing.
174
175
176.. method:: Compress.flush([mode])
177
Georg Brandl4ad934f2011-01-08 21:04:25 +0000178 All pending input is processed, and a bytes object containing the remaining compressed
Georg Brandl116aa622007-08-15 14:28:22 +0000179 output is returned. *mode* can be selected from the constants
180 :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
181 defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
Georg Brandl4ad934f2011-01-08 21:04:25 +0000182 :const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while
Georg Brandl116aa622007-08-15 14:28:22 +0000183 :const:`Z_FINISH` finishes the compressed stream and prevents compressing any
184 more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
185 the :meth:`compress` method cannot be called again; the only realistic action is
186 to delete the object.
187
188
189.. method:: Compress.copy()
190
191 Returns a copy of the compression object. This can be used to efficiently
192 compress a set of data that share a common initial prefix.
193
Georg Brandl116aa622007-08-15 14:28:22 +0000194
Nadeem Vawda1c385462011-08-13 15:22:40 +0200195Decompression objects support the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197
198.. attribute:: Decompress.unused_data
199
Georg Brandl4ad934f2011-01-08 21:04:25 +0000200 A bytes object which contains any bytes past the end of the compressed data. That is,
Georg Brandl116aa622007-08-15 14:28:22 +0000201 this remains ``""`` until the last byte that contains compression data is
Georg Brandl4ad934f2011-01-08 21:04:25 +0000202 available. If the whole bytestring turned out to contain compressed data, this is
203 ``b""``, an empty bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000204
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206.. attribute:: Decompress.unconsumed_tail
207
Georg Brandl4ad934f2011-01-08 21:04:25 +0000208 A bytes object that contains any data that was not consumed by the last
Georg Brandl116aa622007-08-15 14:28:22 +0000209 :meth:`decompress` call because it exceeded the limit for the uncompressed data
210 buffer. This data has not yet been seen by the zlib machinery, so you must feed
211 it (possibly with further data concatenated to it) back to a subsequent
212 :meth:`decompress` method call in order to get correct output.
213
214
Nadeem Vawda1c385462011-08-13 15:22:40 +0200215.. attribute:: Decompress.eof
216
217 A boolean indicating whether the end of the compressed data stream has been
218 reached.
219
220 This makes it possible to distinguish between a properly-formed compressed
221 stream, and an incomplete or truncated one.
222
223 .. versionadded:: 3.3
224
225
Georg Brandl4ad934f2011-01-08 21:04:25 +0000226.. method:: Decompress.decompress(data[, max_length])
Georg Brandl116aa622007-08-15 14:28:22 +0000227
Georg Brandl4ad934f2011-01-08 21:04:25 +0000228 Decompress *data*, returning a bytes object containing the uncompressed data
Georg Brandl116aa622007-08-15 14:28:22 +0000229 corresponding to at least part of the data in *string*. This data should be
230 concatenated to the output produced by any preceding calls to the
231 :meth:`decompress` method. Some of the input data may be preserved in internal
232 buffers for later processing.
233
234 If the optional parameter *max_length* is supplied then the return value will be
235 no longer than *max_length*. This may mean that not all of the compressed input
236 can be processed; and unconsumed data will be stored in the attribute
Georg Brandl4ad934f2011-01-08 21:04:25 +0000237 :attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to
Georg Brandl116aa622007-08-15 14:28:22 +0000238 :meth:`decompress` if decompression is to continue. If *max_length* is not
Georg Brandl4ad934f2011-01-08 21:04:25 +0000239 supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is
240 empty.
Georg Brandl116aa622007-08-15 14:28:22 +0000241
242
243.. method:: Decompress.flush([length])
244
Georg Brandl4ad934f2011-01-08 21:04:25 +0000245 All pending input is processed, and a bytes object containing the remaining
Georg Brandl116aa622007-08-15 14:28:22 +0000246 uncompressed output is returned. After calling :meth:`flush`, the
247 :meth:`decompress` method cannot be called again; the only realistic action is
248 to delete the object.
249
250 The optional parameter *length* sets the initial size of the output buffer.
251
252
253.. method:: Decompress.copy()
254
255 Returns a copy of the decompression object. This can be used to save the state
256 of the decompressor midway through the data stream in order to speed up random
257 seeks into the stream at a future point.
258
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Nadeem Vawda64d25dd2011-09-12 00:04:13 +0200260Information about the version of the zlib library in use is available through
261the following constants:
262
263
264.. data:: ZLIB_VERSION
265
266 The version string of the zlib library that was used for building the module.
267 This may be different from the zlib library actually used at runtime, which
268 is available as :const:`ZLIB_RUNTIME_VERSION`.
269
Nadeem Vawda64d25dd2011-09-12 00:04:13 +0200270
271.. data:: ZLIB_RUNTIME_VERSION
272
273 The version string of the zlib library actually loaded by the interpreter.
274
275 .. versionadded:: 3.3
276
277
Georg Brandl116aa622007-08-15 14:28:22 +0000278.. seealso::
279
280 Module :mod:`gzip`
281 Reading and writing :program:`gzip`\ -format files.
282
283 http://www.zlib.net
284 The zlib library home page.
285
286 http://www.zlib.net/manual.html
287 The zlib manual explains the semantics and usage of the library's many
288 functions.
289