blob: 1de7baee54733db7a857600e19214fe7333066a1 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04008--------------
Georg Brandl116aa622007-08-15 14:28:22 +00009
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 Araujof2fbb9c2012-01-16 16:55:55 +010022For reading and writing ``.gz`` files see the :mod:`gzip` module.
Guido van Rossum77677112007-11-05 19:43:04 +000023
Georg Brandl116aa622007-08-15 14:28:22 +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
Benjamin Peterson058e31e2009-01-16 03:54:08 +000032.. function:: adler32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +000033
Serhiy Storchakad65c9492015-11-02 14:10:23 +020034 Computes an Adler-32 checksum of *data*. (An Adler-32 checksum is almost as
Martin Panterb82032f2015-12-11 05:19:29 +000035 reliable as a CRC32 but can be computed much more quickly.) The result
36 is an unsigned 32-bit integer. If *value* is present, it is used as
37 the starting value of the checksum; otherwise, a default value of 1
38 is used. Passing in *value* allows computing a running checksum over the
Benjamin Peterson058e31e2009-01-16 03:54:08 +000039 concatenation of several inputs. The algorithm is not cryptographically
Georg Brandl116aa622007-08-15 14:28:22 +000040 strong, and should not be used for authentication or digital signatures. Since
41 the algorithm is designed for use as a checksum algorithm, it is not suitable
42 for use as a general hash algorithm.
43
Martin Panterb82032f2015-12-11 05:19:29 +000044 .. versionchanged:: 3.0
45 Always returns an unsigned value.
46 To generate the same numeric value across all Python versions and
47 platforms, use ``adler32(data) & 0xffffffff``.
Benjamin Peterson058e31e2009-01-16 03:54:08 +000048
Georg Brandl116aa622007-08-15 14:28:22 +000049
Georg Brandl4ad934f2011-01-08 21:04:25 +000050.. function:: compress(data[, level])
Georg Brandl116aa622007-08-15 14:28:22 +000051
Georg Brandl4ad934f2011-01-08 21:04:25 +000052 Compresses the bytes in *data*, returning a bytes object containing compressed data.
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010053 *level* is an integer from ``0`` to ``9`` controlling the level of compression;
Georg Brandl116aa622007-08-15 14:28:22 +000054 ``1`` is fastest and produces the least compression, ``9`` is slowest and
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010055 produces the most. ``0`` is no compression. The default value is ``6``.
56 Raises the :exc:`error` exception if any error occurs.
Georg Brandl116aa622007-08-15 14:28:22 +000057
58
Martin Panterbf19d162015-09-09 01:01:13 +000059.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memLevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
Georg Brandl116aa622007-08-15 14:28:22 +000060
61 Returns a compression object, to be used for compressing data streams that won't
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020062 fit into memory at once.
63
Martin Panter567d5132016-02-03 07:06:33 +000064 *level* is the compression level -- an integer from ``0`` to ``9`` or ``-1``.
65 A value of ``1`` is fastest and produces the least compression, while a value of
Nadeem Vawda6ff262e2012-11-11 14:14:47 +010066 ``9`` is slowest and produces the most. ``0`` is no compression. The default
Martin Panter567d5132016-02-03 07:06:33 +000067 value is ``-1`` (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default
68 compromise between speed and compression (currently equivalent to level 6).
Nadeem Vawda2180c972012-06-22 01:40:49 +020069
70 *method* is the compression algorithm. Currently, the only supported value is
71 ``DEFLATED``.
72
Martin Panter0fdf41d2016-05-27 07:32:11 +000073 The *wbits* argument controls the size of the history buffer (or the
74 "window size") used when compressing data, and whether a header and
75 trailer is included in the output. It can take several ranges of values:
76
77 * +9 to +15: The base-two logarithm of the window size, which
78 therefore ranges between 512 and 32768. Larger values produce
79 better compression at the expense of greater memory usage. The
80 resulting output will include a zlib-specific header and trailer.
81
82 * −9 to −15: Uses the absolute value of *wbits* as the
83 window size logarithm, while producing a raw output stream with no
84 header or trailing checksum.
85
86 * +25 to +31 = 16 + (9 to 15): Uses the low 4 bits of the value as the
87 window size logarithm, while including a basic :program:`gzip` header
88 and trailing checksum in the output.
Nadeem Vawda2180c972012-06-22 01:40:49 +020089
Martin Panterbf19d162015-09-09 01:01:13 +000090 The *memLevel* argument controls the amount of memory used for the
91 internal compression state. Valid values range from ``1`` to ``9``.
92 Higher values use more memory, but are faster and produce smaller output.
Nadeem Vawda2180c972012-06-22 01:40:49 +020093
94 *strategy* is used to tune the compression algorithm. Possible values are
95 ``Z_DEFAULT_STRATEGY``, ``Z_FILTERED``, and ``Z_HUFFMAN_ONLY``.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +020096
97 *zdict* is a predefined compression dictionary. This is a sequence of bytes
98 (such as a :class:`bytes` object) containing subsequences that are expected
99 to occur frequently in the data that is to be compressed. Those subsequences
100 that are expected to be most common should come at the end of the dictionary.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Georg Brandl9aae9e52012-06-26 08:51:17 +0200102 .. versionchanged:: 3.3
Georg Brandl9ff06dc2013-10-17 19:51:34 +0200103 Added the *zdict* parameter and keyword argument support.
Georg Brandl9aae9e52012-06-26 08:51:17 +0200104
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000106.. function:: crc32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108 .. index::
109 single: Cyclic Redundancy Check
110 single: checksum; Cyclic Redundancy Check
111
Martin Panterb82032f2015-12-11 05:19:29 +0000112 Computes a CRC (Cyclic Redundancy Check) checksum of *data*. The
113 result is an unsigned 32-bit integer. If *value* is present, it is used
114 as the starting value of the checksum; otherwise, a default value of 0
115 is used. Passing in *value* allows computing a running checksum over the
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000116 concatenation of several inputs. The algorithm is not cryptographically
Georg Brandl116aa622007-08-15 14:28:22 +0000117 strong, and should not be used for authentication or digital signatures. Since
118 the algorithm is designed for use as a checksum algorithm, it is not suitable
119 for use as a general hash algorithm.
120
Martin Panterb82032f2015-12-11 05:19:29 +0000121 .. versionchanged:: 3.0
122 Always returns an unsigned value.
Georg Brandl9aae9e52012-06-26 08:51:17 +0200123 To generate the same numeric value across all Python versions and
Martin Panterb82032f2015-12-11 05:19:29 +0000124 platforms, use ``crc32(data) & 0xffffffff``.
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000125
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Georg Brandl4ad934f2011-01-08 21:04:25 +0000127.. function:: decompress(data[, wbits[, bufsize]])
Georg Brandl116aa622007-08-15 14:28:22 +0000128
Georg Brandl4ad934f2011-01-08 21:04:25 +0000129 Decompresses the bytes in *data*, returning a bytes object containing the
Martin Panter0fdf41d2016-05-27 07:32:11 +0000130 uncompressed data. The *wbits* parameter depends on
131 the format of *data*, and is discussed further below.
Benjamin Peterson2614cda2010-03-21 22:36:19 +0000132 If *bufsize* is given, it is used as the initial size of the output
Georg Brandl116aa622007-08-15 14:28:22 +0000133 buffer. Raises the :exc:`error` exception if any error occurs.
134
Martin Panter0fdf41d2016-05-27 07:32:11 +0000135 .. _decompress-wbits:
136
137 The *wbits* parameter controls the size of the history buffer
138 (or "window size"), and what header and trailer format is expected.
139 It is similar to the parameter for :func:`compressobj`, but accepts
140 more ranges of values:
141
142 * +8 to +15: The base-two logarithm of the window size. The input
143 must include a zlib header and trailer.
144
145 * 0: Automatically determine the window size from the zlib header.
Martin Panterc618ae82016-05-27 11:20:21 +0000146 Only supported since zlib 1.2.3.5.
Martin Panter0fdf41d2016-05-27 07:32:11 +0000147
148 * −8 to −15: Uses the absolute value of *wbits* as the window size
149 logarithm. The input must be a raw stream with no header or trailer.
150
151 * +24 to +31 = 16 + (8 to 15): Uses the low 4 bits of the value as
152 the window size logarithm. The input must include a gzip header and
153 trailer.
154
155 * +40 to +47 = 32 + (8 to 15): Uses the low 4 bits of the value as
156 the window size logarithm, and automatically accepts either
157 the zlib or gzip format.
158
159 When decompressing a stream, the window size must not be smaller
Benjamin Peterson2614cda2010-03-21 22:36:19 +0000160 than the size originally used to compress the stream; using a too-small
Martin Panter0fdf41d2016-05-27 07:32:11 +0000161 value may result in an :exc:`error` exception. The default *wbits* value
162 is 15, which corresponds to the largest window size and requires a zlib
163 header and trailer to be included.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165 *bufsize* is the initial size of the buffer used to hold decompressed data. If
166 more space is required, the buffer size will be increased as needed, so you
167 don't have to get this value exactly right; tuning it will only save a few calls
Georg Brandl60203b42010-10-06 10:11:56 +0000168 to :c:func:`malloc`. The default size is 16384.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170
Georg Brandl9aae9e52012-06-26 08:51:17 +0200171.. function:: decompressobj(wbits=15[, zdict])
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173 Returns a decompression object, to be used for decompressing data streams that
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200174 won't fit into memory at once.
175
Martin Panter0fdf41d2016-05-27 07:32:11 +0000176 The *wbits* parameter controls the size of the history buffer (or the
177 "window size"), and what header and trailer format is expected. It has
178 the same meaning as `described for decompress() <#decompress-wbits>`__.
Nadeem Vawdafd8a8382012-06-21 02:13:12 +0200179
180 The *zdict* parameter specifies a predefined compression dictionary. If
181 provided, this must be the same dictionary as was used by the compressor that
182 produced the data that is to be decompressed.
183
Georg Brandl9aae9e52012-06-26 08:51:17 +0200184 .. note::
185
186 If *zdict* is a mutable object (such as a :class:`bytearray`), you must not
187 modify its contents between the call to :func:`decompressobj` and the first
188 call to the decompressor's ``decompress()`` method.
189
190 .. versionchanged:: 3.3
191 Added the *zdict* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Nadeem Vawda64d25dd2011-09-12 00:04:13 +0200193
Georg Brandl116aa622007-08-15 14:28:22 +0000194Compression objects support the following methods:
195
196
Georg Brandl4ad934f2011-01-08 21:04:25 +0000197.. method:: Compress.compress(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000198
Georg Brandl4ad934f2011-01-08 21:04:25 +0000199 Compress *data*, returning a bytes object containing compressed data for at least
200 part of the data in *data*. This data should be concatenated to the output
Georg Brandl116aa622007-08-15 14:28:22 +0000201 produced by any preceding calls to the :meth:`compress` method. Some input may
202 be kept in internal buffers for later processing.
203
204
205.. method:: Compress.flush([mode])
206
Georg Brandl4ad934f2011-01-08 21:04:25 +0000207 All pending input is processed, and a bytes object containing the remaining compressed
Georg Brandl116aa622007-08-15 14:28:22 +0000208 output is returned. *mode* can be selected from the constants
209 :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
210 defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
Georg Brandl4ad934f2011-01-08 21:04:25 +0000211 :const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while
Georg Brandl116aa622007-08-15 14:28:22 +0000212 :const:`Z_FINISH` finishes the compressed stream and prevents compressing any
213 more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
214 the :meth:`compress` method cannot be called again; the only realistic action is
215 to delete the object.
216
217
218.. method:: Compress.copy()
219
220 Returns a copy of the compression object. This can be used to efficiently
221 compress a set of data that share a common initial prefix.
222
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Nadeem Vawda1c385462011-08-13 15:22:40 +0200224Decompression objects support the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000225
226
227.. attribute:: Decompress.unused_data
228
Georg Brandl4ad934f2011-01-08 21:04:25 +0000229 A bytes object which contains any bytes past the end of the compressed data. That is,
Serhiy Storchaka5e028ae2014-02-06 21:10:41 +0200230 this remains ``b""`` until the last byte that contains compression data is
Georg Brandl4ad934f2011-01-08 21:04:25 +0000231 available. If the whole bytestring turned out to contain compressed data, this is
232 ``b""``, an empty bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235.. attribute:: Decompress.unconsumed_tail
236
Georg Brandl4ad934f2011-01-08 21:04:25 +0000237 A bytes object that contains any data that was not consumed by the last
Georg Brandl116aa622007-08-15 14:28:22 +0000238 :meth:`decompress` call because it exceeded the limit for the uncompressed data
239 buffer. This data has not yet been seen by the zlib machinery, so you must feed
240 it (possibly with further data concatenated to it) back to a subsequent
241 :meth:`decompress` method call in order to get correct output.
242
243
Nadeem Vawda1c385462011-08-13 15:22:40 +0200244.. attribute:: Decompress.eof
245
246 A boolean indicating whether the end of the compressed data stream has been
247 reached.
248
249 This makes it possible to distinguish between a properly-formed compressed
250 stream, and an incomplete or truncated one.
251
252 .. versionadded:: 3.3
253
254
Georg Brandl4ad934f2011-01-08 21:04:25 +0000255.. method:: Decompress.decompress(data[, max_length])
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Georg Brandl4ad934f2011-01-08 21:04:25 +0000257 Decompress *data*, returning a bytes object containing the uncompressed data
Georg Brandl116aa622007-08-15 14:28:22 +0000258 corresponding to at least part of the data in *string*. This data should be
259 concatenated to the output produced by any preceding calls to the
260 :meth:`decompress` method. Some of the input data may be preserved in internal
261 buffers for later processing.
262
Martin Panter38fe4dc2015-11-18 00:59:17 +0000263 If the optional parameter *max_length* is non-zero then the return value will be
Georg Brandl116aa622007-08-15 14:28:22 +0000264 no longer than *max_length*. This may mean that not all of the compressed input
265 can be processed; and unconsumed data will be stored in the attribute
Georg Brandl4ad934f2011-01-08 21:04:25 +0000266 :attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to
Georg Brandl116aa622007-08-15 14:28:22 +0000267 :meth:`decompress` if decompression is to continue. If *max_length* is not
Georg Brandl4ad934f2011-01-08 21:04:25 +0000268 supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is
269 empty.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. method:: Decompress.flush([length])
273
Georg Brandl4ad934f2011-01-08 21:04:25 +0000274 All pending input is processed, and a bytes object containing the remaining
Georg Brandl116aa622007-08-15 14:28:22 +0000275 uncompressed output is returned. After calling :meth:`flush`, the
276 :meth:`decompress` method cannot be called again; the only realistic action is
277 to delete the object.
278
279 The optional parameter *length* sets the initial size of the output buffer.
280
281
282.. method:: Decompress.copy()
283
284 Returns a copy of the decompression object. This can be used to save the state
285 of the decompressor midway through the data stream in order to speed up random
286 seeks into the stream at a future point.
287
Georg Brandl116aa622007-08-15 14:28:22 +0000288
Nadeem Vawda64d25dd2011-09-12 00:04:13 +0200289Information about the version of the zlib library in use is available through
290the following constants:
291
292
293.. data:: ZLIB_VERSION
294
295 The version string of the zlib library that was used for building the module.
296 This may be different from the zlib library actually used at runtime, which
297 is available as :const:`ZLIB_RUNTIME_VERSION`.
298
Nadeem Vawda64d25dd2011-09-12 00:04:13 +0200299
300.. data:: ZLIB_RUNTIME_VERSION
301
302 The version string of the zlib library actually loaded by the interpreter.
303
304 .. versionadded:: 3.3
305
306
Georg Brandl116aa622007-08-15 14:28:22 +0000307.. seealso::
308
309 Module :mod:`gzip`
310 Reading and writing :program:`gzip`\ -format files.
311
312 http://www.zlib.net
313 The zlib library home page.
314
315 http://www.zlib.net/manual.html
316 The zlib manual explains the semantics and usage of the library's many
317 functions.
318