• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`zlib` --- Compression compatible with :program:`gzip`
2===========================================================
3
4.. module:: zlib
5   :synopsis: Low-level interface to compression and decompression routines
6              compatible with gzip.
7
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
22For reading and writing ``.gz`` files see the :mod:`gzip` module.
23
24The available exception and functions in this module are:
25
26
27.. exception:: error
28
29   Exception raised on compression and decompression errors.
30
31
32.. function:: adler32(data[, value])
33
34   Computes an Adler-32 checksum of *data*.  (An Adler-32 checksum is almost as
35   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
39   concatenation of several inputs.  The algorithm is not cryptographically
40   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
44   .. 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``.
48
49
50.. function:: compress(data, level=-1)
51
52   Compresses the bytes in *data*, returning a bytes object containing compressed data.
53   *level* is an integer from ``0`` to ``9`` or ``-1`` controlling the level of compression;
54   ``1`` is fastest and produces the least compression, ``9`` is slowest and
55   produces the most.  ``0`` is no compression.  The default value is ``-1``
56   (Z_DEFAULT_COMPRESSION).  Z_DEFAULT_COMPRESSION represents a default
57   compromise between speed and compression (currently equivalent to level 6).
58   Raises the :exc:`error` exception if any error occurs.
59
60   .. versionchanged:: 3.6
61      *level* can now be used as a keyword parameter.
62
63
64.. function:: compressobj(level=-1, method=DEFLATED, wbits=15, memLevel=8, strategy=Z_DEFAULT_STRATEGY[, zdict])
65
66   Returns a compression object, to be used for compressing data streams that won't
67   fit into memory at once.
68
69   *level* is the compression level -- an integer from ``0`` to ``9`` or ``-1``.
70   A value of ``1`` is fastest and produces the least compression, while a value of
71   ``9`` is slowest and produces the most. ``0`` is no compression. The default
72   value is ``-1`` (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default
73   compromise between speed and compression (currently equivalent to level 6).
74
75   *method* is the compression algorithm. Currently, the only supported value is
76   ``DEFLATED``.
77
78   The *wbits* argument controls the size of the history buffer (or the
79   "window size") used when compressing data, and whether a header and
80   trailer is included in the output.  It can take several ranges of values:
81
82   * +9 to +15: The base-two logarithm of the window size, which
83     therefore ranges between 512 and 32768.  Larger values produce
84     better compression at the expense of greater memory usage.  The
85     resulting output will include a zlib-specific header and trailer.
86
87   * −9 to −15: Uses the absolute value of *wbits* as the
88     window size logarithm, while producing a raw output stream with no
89     header or trailing checksum.
90
91   * +25 to +31 = 16 + (9 to 15): Uses the low 4 bits of the value as the
92     window size logarithm, while including a basic :program:`gzip` header
93     and trailing checksum in the output.
94
95   The *memLevel* argument controls the amount of memory used for the
96   internal compression state. Valid values range from ``1`` to ``9``.
97   Higher values use more memory, but are faster and produce smaller output.
98
99   *strategy* is used to tune the compression algorithm. Possible values are
100   ``Z_DEFAULT_STRATEGY``, ``Z_FILTERED``, and ``Z_HUFFMAN_ONLY``.
101
102   *zdict* is a predefined compression dictionary. This is a sequence of bytes
103   (such as a :class:`bytes` object) containing subsequences that are expected
104   to occur frequently in the data that is to be compressed. Those subsequences
105   that are expected to be most common should come at the end of the dictionary.
106
107   .. versionchanged:: 3.3
108      Added the *zdict* parameter and keyword argument support.
109
110
111.. function:: crc32(data[, value])
112
113   .. index::
114      single: Cyclic Redundancy Check
115      single: checksum; Cyclic Redundancy Check
116
117   Computes a CRC (Cyclic Redundancy Check) checksum of *data*. The
118   result is an unsigned 32-bit integer. If *value* is present, it is used
119   as the starting value of the checksum; otherwise, a default value of 0
120   is used.  Passing in *value* allows computing a running checksum over the
121   concatenation of several inputs.  The algorithm is not cryptographically
122   strong, and should not be used for authentication or digital signatures.  Since
123   the algorithm is designed for use as a checksum algorithm, it is not suitable
124   for use as a general hash algorithm.
125
126   .. versionchanged:: 3.0
127      Always returns an unsigned value.
128      To generate the same numeric value across all Python versions and
129      platforms, use ``crc32(data) & 0xffffffff``.
130
131
132.. function:: decompress(data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)
133
134   Decompresses the bytes in *data*, returning a bytes object containing the
135   uncompressed data.  The *wbits* parameter depends on
136   the format of *data*, and is discussed further below.
137   If *bufsize* is given, it is used as the initial size of the output
138   buffer.  Raises the :exc:`error` exception if any error occurs.
139
140   .. _decompress-wbits:
141
142   The *wbits* parameter controls the size of the history buffer
143   (or "window size"), and what header and trailer format is expected.
144   It is similar to the parameter for :func:`compressobj`, but accepts
145   more ranges of values:
146
147   * +8 to +15: The base-two logarithm of the window size.  The input
148     must include a zlib header and trailer.
149
150   * 0: Automatically determine the window size from the zlib header.
151     Only supported since zlib 1.2.3.5.
152
153   * −8 to −15: Uses the absolute value of *wbits* as the window size
154     logarithm.  The input must be a raw stream with no header or trailer.
155
156   * +24 to +31 = 16 + (8 to 15): Uses the low 4 bits of the value as
157     the window size logarithm.  The input must include a gzip header and
158     trailer.
159
160   * +40 to +47 = 32 + (8 to 15): Uses the low 4 bits of the value as
161     the window size logarithm, and automatically accepts either
162     the zlib or gzip format.
163
164   When decompressing a stream, the window size must not be smaller
165   than the size originally used to compress the stream; using a too-small
166   value may result in an :exc:`error` exception. The default *wbits* value
167   corresponds to the largest window size and requires a zlib header and
168   trailer to be included.
169
170   *bufsize* is the initial size of the buffer used to hold decompressed data.  If
171   more space is required, the buffer size will be increased as needed, so you
172   don't have to get this value exactly right; tuning it will only save a few calls
173   to :c:func:`malloc`.
174
175   .. versionchanged:: 3.6
176      *wbits* and *bufsize* can be used as keyword arguments.
177
178.. function:: decompressobj(wbits=15[, zdict])
179
180   Returns a decompression object, to be used for decompressing data streams that
181   won't fit into memory at once.
182
183   The *wbits* parameter controls the size of the history buffer (or the
184   "window size"), and what header and trailer format is expected.  It has
185   the same meaning as `described for decompress() <#decompress-wbits>`__.
186
187   The *zdict* parameter specifies a predefined compression dictionary. If
188   provided, this must be the same dictionary as was used by the compressor that
189   produced the data that is to be decompressed.
190
191   .. note::
192
193      If *zdict* is a mutable object (such as a :class:`bytearray`), you must not
194      modify its contents between the call to :func:`decompressobj` and the first
195      call to the decompressor's ``decompress()`` method.
196
197   .. versionchanged:: 3.3
198      Added the *zdict* parameter.
199
200
201Compression objects support the following methods:
202
203
204.. method:: Compress.compress(data)
205
206   Compress *data*, returning a bytes object containing compressed data for at least
207   part of the data in *data*.  This data should be concatenated to the output
208   produced by any preceding calls to the :meth:`compress` method.  Some input may
209   be kept in internal buffers for later processing.
210
211
212.. method:: Compress.flush([mode])
213
214   All pending input is processed, and a bytes object containing the remaining compressed
215   output is returned.  *mode* can be selected from the constants
216   :const:`Z_SYNC_FLUSH`,  :const:`Z_FULL_FLUSH`,  or  :const:`Z_FINISH`,
217   defaulting to :const:`Z_FINISH`.  :const:`Z_SYNC_FLUSH` and
218   :const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while
219   :const:`Z_FINISH` finishes the compressed stream and  prevents compressing any
220   more data.  After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
221   the :meth:`compress` method cannot be called again; the only realistic action is
222   to delete the object.
223
224
225.. method:: Compress.copy()
226
227   Returns a copy of the compression object.  This can be used to efficiently
228   compress a set of data that share a common initial prefix.
229
230
231Decompression objects support the following methods and attributes:
232
233
234.. attribute:: Decompress.unused_data
235
236   A bytes object which contains any bytes past the end of the compressed data. That is,
237   this remains ``b""`` until the last byte that contains compression data is
238   available.  If the whole bytestring turned out to contain compressed data, this is
239   ``b""``, an empty bytes object.
240
241
242.. attribute:: Decompress.unconsumed_tail
243
244   A bytes object that contains any data that was not consumed by the last
245   :meth:`decompress` call because it exceeded the limit for the uncompressed data
246   buffer.  This data has not yet been seen by the zlib machinery, so you must feed
247   it (possibly with further data concatenated to it) back to a subsequent
248   :meth:`decompress` method call in order to get correct output.
249
250
251.. attribute:: Decompress.eof
252
253   A boolean indicating whether the end of the compressed data stream has been
254   reached.
255
256   This makes it possible to distinguish between a properly-formed compressed
257   stream, and an incomplete or truncated one.
258
259   .. versionadded:: 3.3
260
261
262.. method:: Decompress.decompress(data, max_length=0)
263
264   Decompress *data*, returning a bytes object containing the uncompressed data
265   corresponding to at least part of the data in *string*.  This data should be
266   concatenated to the output produced by any preceding calls to the
267   :meth:`decompress` method.  Some of the input data may be preserved in internal
268   buffers for later processing.
269
270   If the optional parameter *max_length* is non-zero then the return value will be
271   no longer than *max_length*. This may mean that not all of the compressed input
272   can be processed; and unconsumed data will be stored in the attribute
273   :attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to
274   :meth:`decompress` if decompression is to continue.  If *max_length* is zero
275   then the whole input is decompressed, and :attr:`unconsumed_tail` is empty.
276
277   .. versionchanged:: 3.6
278      *max_length* can be used as a keyword argument.
279
280
281.. method:: Decompress.flush([length])
282
283   All pending input is processed, and a bytes object containing the remaining
284   uncompressed output is returned.  After calling :meth:`flush`, the
285   :meth:`decompress` method cannot be called again; the only realistic action is
286   to delete the object.
287
288   The optional parameter *length* sets the initial size of the output buffer.
289
290
291.. method:: Decompress.copy()
292
293   Returns a copy of the decompression object.  This can be used to save the state
294   of the decompressor midway through the data stream in order to speed up random
295   seeks into the stream at a future point.
296
297
298Information about the version of the zlib library in use is available through
299the following constants:
300
301
302.. data:: ZLIB_VERSION
303
304   The version string of the zlib library that was used for building the module.
305   This may be different from the zlib library actually used at runtime, which
306   is available as :const:`ZLIB_RUNTIME_VERSION`.
307
308
309.. data:: ZLIB_RUNTIME_VERSION
310
311   The version string of the zlib library actually loaded by the interpreter.
312
313   .. versionadded:: 3.3
314
315
316.. seealso::
317
318   Module :mod:`gzip`
319      Reading and writing :program:`gzip`\ -format files.
320
321   http://www.zlib.net
322      The zlib library home page.
323
324   http://www.zlib.net/manual.html
325      The zlib manual explains  the semantics and usage of the library's many
326      functions.
327
328