blob: 54835e7f7ee3fc4b6c1842a1e7618261163177aa [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
Guido van Rossum77677112007-11-05 19:43:04 +000021For reading and writing ``.gz`` files see the :mod:`gzip` module. For
22other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and
23:mod:`tarfile` modules.
24
Georg Brandl116aa622007-08-15 14:28:22 +000025The available exception and functions in this module are:
26
27
28.. exception:: error
29
30 Exception raised on compression and decompression errors.
31
32
Benjamin Peterson058e31e2009-01-16 03:54:08 +000033.. function:: adler32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +000034
Benjamin Peterson058e31e2009-01-16 03:54:08 +000035 Computes a Adler-32 checksum of *data*. (An Adler-32 checksum is almost as
Georg Brandl116aa622007-08-15 14:28:22 +000036 reliable as a CRC32 but can be computed much more quickly.) If *value* is
37 present, it is used as the starting value of the checksum; otherwise, a fixed
38 default value is used. This 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
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000044 Always returns an unsigned 32-bit integer.
45
Benjamin Peterson058e31e2009-01-16 03:54:08 +000046.. note::
47 To generate the same numeric value across all Python versions and
48 platforms use adler32(data) & 0xffffffff. If you are only using
49 the checksum in packed binary format this is not necessary as the
Gregory P. Smithfa6cf392009-02-01 00:30:50 +000050 return value is the correct 32bit binary representation
Benjamin Peterson058e31e2009-01-16 03:54:08 +000051 regardless of sign.
52
Georg Brandl116aa622007-08-15 14:28:22 +000053
Georg Brandl4ad934f2011-01-08 21:04:25 +000054.. function:: compress(data[, level])
Georg Brandl116aa622007-08-15 14:28:22 +000055
Georg Brandl4ad934f2011-01-08 21:04:25 +000056 Compresses the bytes in *data*, returning a bytes object containing compressed data.
Georg Brandl116aa622007-08-15 14:28:22 +000057 *level* is an integer from ``1`` to ``9`` controlling the level of compression;
58 ``1`` is fastest and produces the least compression, ``9`` is slowest and
59 produces the most. The default value is ``6``. Raises the :exc:`error`
60 exception if any error occurs.
61
62
63.. function:: compressobj([level])
64
65 Returns a compression object, to be used for compressing data streams that won't
66 fit into memory at once. *level* is an integer from ``1`` to ``9`` controlling
67 the level of compression; ``1`` is fastest and produces the least compression,
68 ``9`` is slowest and produces the most. The default value is ``6``.
69
70
Benjamin Peterson058e31e2009-01-16 03:54:08 +000071.. function:: crc32(data[, value])
Georg Brandl116aa622007-08-15 14:28:22 +000072
73 .. index::
74 single: Cyclic Redundancy Check
75 single: checksum; Cyclic Redundancy Check
76
Benjamin Peterson058e31e2009-01-16 03:54:08 +000077 Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is
Georg Brandl116aa622007-08-15 14:28:22 +000078 present, it is used as the starting value of the checksum; otherwise, a fixed
79 default value is used. This allows computing a running checksum over the
Benjamin Peterson058e31e2009-01-16 03:54:08 +000080 concatenation of several inputs. The algorithm is not cryptographically
Georg Brandl116aa622007-08-15 14:28:22 +000081 strong, and should not be used for authentication or digital signatures. Since
82 the algorithm is designed for use as a checksum algorithm, it is not suitable
83 for use as a general hash algorithm.
84
Gregory P. Smithab0d8a12008-03-17 20:24:09 +000085 Always returns an unsigned 32-bit integer.
86
Benjamin Peterson058e31e2009-01-16 03:54:08 +000087.. note::
88 To generate the same numeric value across all Python versions and
89 platforms use crc32(data) & 0xffffffff. If you are only using
90 the checksum in packed binary format this is not necessary as the
Gregory P. Smithfa6cf392009-02-01 00:30:50 +000091 return value is the correct 32bit binary representation
Benjamin Peterson058e31e2009-01-16 03:54:08 +000092 regardless of sign.
93
Georg Brandl116aa622007-08-15 14:28:22 +000094
Georg Brandl4ad934f2011-01-08 21:04:25 +000095.. function:: decompress(data[, wbits[, bufsize]])
Georg Brandl116aa622007-08-15 14:28:22 +000096
Georg Brandl4ad934f2011-01-08 21:04:25 +000097 Decompresses the bytes in *data*, returning a bytes object containing the
Georg Brandl116aa622007-08-15 14:28:22 +000098 uncompressed data. The *wbits* parameter controls the size of the window
Benjamin Peterson2614cda2010-03-21 22:36:19 +000099 buffer, and is discussed further below.
100 If *bufsize* is given, it is used as the initial size of the output
Georg Brandl116aa622007-08-15 14:28:22 +0000101 buffer. Raises the :exc:`error` exception if any error occurs.
102
103 The absolute value of *wbits* is the base two logarithm of the size of the
104 history buffer (the "window size") used when compressing data. Its absolute
105 value should be between 8 and 15 for the most recent versions of the zlib
106 library, larger values resulting in better compression at the expense of greater
Benjamin Peterson2614cda2010-03-21 22:36:19 +0000107 memory usage. When decompressing a stream, *wbits* must not be smaller
108 than the size originally used to compress the stream; using a too-small
109 value will result in an exception. The default value is therefore the
110 highest value, 15. When *wbits* is negative, the standard
Jesus Ceafb7b6682010-05-03 16:14:58 +0000111 :program:`gzip` header is suppressed.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113 *bufsize* is the initial size of the buffer used to hold decompressed data. If
114 more space is required, the buffer size will be increased as needed, so you
115 don't have to get this value exactly right; tuning it will only save a few calls
Georg Brandl60203b42010-10-06 10:11:56 +0000116 to :c:func:`malloc`. The default size is 16384.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
119.. function:: decompressobj([wbits])
120
121 Returns a decompression object, to be used for decompressing data streams that
122 won't fit into memory at once. The *wbits* parameter controls the size of the
123 window buffer.
124
125Compression objects support the following methods:
126
127
Georg Brandl4ad934f2011-01-08 21:04:25 +0000128.. method:: Compress.compress(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000129
Georg Brandl4ad934f2011-01-08 21:04:25 +0000130 Compress *data*, returning a bytes object containing compressed data for at least
131 part of the data in *data*. This data should be concatenated to the output
Georg Brandl116aa622007-08-15 14:28:22 +0000132 produced by any preceding calls to the :meth:`compress` method. Some input may
133 be kept in internal buffers for later processing.
134
135
136.. method:: Compress.flush([mode])
137
Georg Brandl4ad934f2011-01-08 21:04:25 +0000138 All pending input is processed, and a bytes object containing the remaining compressed
Georg Brandl116aa622007-08-15 14:28:22 +0000139 output is returned. *mode* can be selected from the constants
140 :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
141 defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
Georg Brandl4ad934f2011-01-08 21:04:25 +0000142 :const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while
Georg Brandl116aa622007-08-15 14:28:22 +0000143 :const:`Z_FINISH` finishes the compressed stream and prevents compressing any
144 more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
145 the :meth:`compress` method cannot be called again; the only realistic action is
146 to delete the object.
147
148
149.. method:: Compress.copy()
150
151 Returns a copy of the compression object. This can be used to efficiently
152 compress a set of data that share a common initial prefix.
153
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Nadeem Vawda1c385462011-08-13 15:22:40 +0200155Decompression objects support the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157
158.. attribute:: Decompress.unused_data
159
Georg Brandl4ad934f2011-01-08 21:04:25 +0000160 A bytes object which contains any bytes past the end of the compressed data. That is,
Georg Brandl116aa622007-08-15 14:28:22 +0000161 this remains ``""`` until the last byte that contains compression data is
Georg Brandl4ad934f2011-01-08 21:04:25 +0000162 available. If the whole bytestring turned out to contain compressed data, this is
163 ``b""``, an empty bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166.. attribute:: Decompress.unconsumed_tail
167
Georg Brandl4ad934f2011-01-08 21:04:25 +0000168 A bytes object that contains any data that was not consumed by the last
Georg Brandl116aa622007-08-15 14:28:22 +0000169 :meth:`decompress` call because it exceeded the limit for the uncompressed data
170 buffer. This data has not yet been seen by the zlib machinery, so you must feed
171 it (possibly with further data concatenated to it) back to a subsequent
172 :meth:`decompress` method call in order to get correct output.
173
174
Nadeem Vawda1c385462011-08-13 15:22:40 +0200175.. attribute:: Decompress.eof
176
177 A boolean indicating whether the end of the compressed data stream has been
178 reached.
179
180 This makes it possible to distinguish between a properly-formed compressed
181 stream, and an incomplete or truncated one.
182
183 .. versionadded:: 3.3
184
185
Georg Brandl4ad934f2011-01-08 21:04:25 +0000186.. method:: Decompress.decompress(data[, max_length])
Georg Brandl116aa622007-08-15 14:28:22 +0000187
Georg Brandl4ad934f2011-01-08 21:04:25 +0000188 Decompress *data*, returning a bytes object containing the uncompressed data
Georg Brandl116aa622007-08-15 14:28:22 +0000189 corresponding to at least part of the data in *string*. This data should be
190 concatenated to the output produced by any preceding calls to the
191 :meth:`decompress` method. Some of the input data may be preserved in internal
192 buffers for later processing.
193
194 If the optional parameter *max_length* is supplied then the return value will be
195 no longer than *max_length*. This may mean that not all of the compressed input
196 can be processed; and unconsumed data will be stored in the attribute
Georg Brandl4ad934f2011-01-08 21:04:25 +0000197 :attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to
Georg Brandl116aa622007-08-15 14:28:22 +0000198 :meth:`decompress` if decompression is to continue. If *max_length* is not
Georg Brandl4ad934f2011-01-08 21:04:25 +0000199 supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is
200 empty.
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202
203.. method:: Decompress.flush([length])
204
Georg Brandl4ad934f2011-01-08 21:04:25 +0000205 All pending input is processed, and a bytes object containing the remaining
Georg Brandl116aa622007-08-15 14:28:22 +0000206 uncompressed output is returned. After calling :meth:`flush`, the
207 :meth:`decompress` method cannot be called again; the only realistic action is
208 to delete the object.
209
210 The optional parameter *length* sets the initial size of the output buffer.
211
212
213.. method:: Decompress.copy()
214
215 Returns a copy of the decompression object. This can be used to save the state
216 of the decompressor midway through the data stream in order to speed up random
217 seeks into the stream at a future point.
218
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220.. seealso::
221
222 Module :mod:`gzip`
223 Reading and writing :program:`gzip`\ -format files.
224
225 http://www.zlib.net
226 The zlib library home page.
227
228 http://www.zlib.net/manual.html
229 The zlib manual explains the semantics and usage of the library's many
230 functions.
231