Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`zlib` --- Compression compatible with :program:`gzip` |
| 2 | =========================================================== |
| 3 | |
| 4 | .. module:: zlib |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 5 | :synopsis: Low-level interface to compression and decompression routines |
| 6 | compatible with gzip. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | |
| 8 | |
| 9 | For applications that require data compression, the functions in this module |
| 10 | allow compression and decompression, using the zlib library. The zlib library |
| 11 | has its own home page at http://www.zlib.net. There are known |
| 12 | incompatibilities between the Python module and versions of the zlib library |
| 13 | earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using |
| 14 | 1.1.4 or later. |
| 15 | |
| 16 | zlib's functions have many options and often need to be used in a particular |
| 17 | order. This documentation doesn't attempt to cover all of the permutations; |
| 18 | consult the zlib manual at http://www.zlib.net/manual.html for authoritative |
| 19 | information. |
| 20 | |
Éric Araujo | f2fbb9c | 2012-01-16 16:55:55 +0100 | [diff] [blame] | 21 | For reading and writing ``.gz`` files see the :mod:`gzip` module. |
Guido van Rossum | 7767711 | 2007-11-05 19:43:04 +0000 | [diff] [blame] | 22 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | The 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 Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 31 | .. function:: adler32(data[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 33 | Computes a Adler-32 checksum of *data*. (An Adler-32 checksum is almost as |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | 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 Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 37 | concatenation of several inputs. The algorithm is not cryptographically |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | 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. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 42 | Always returns an unsigned 32-bit integer. |
| 43 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 44 | .. 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. Smith | fa6cf39 | 2009-02-01 00:30:50 +0000 | [diff] [blame] | 48 | return value is the correct 32bit binary representation |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 49 | regardless of sign. |
| 50 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 52 | .. function:: compress(data[, level]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 54 | Compresses the bytes in *data*, returning a bytes object containing compressed data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | *level* is an integer from ``1`` to ``9`` controlling the level of compression; |
| 56 | ``1`` is fastest and produces the least compression, ``9`` is slowest and |
| 57 | produces the most. The default value is ``6``. Raises the :exc:`error` |
| 58 | exception if any error occurs. |
| 59 | |
| 60 | |
| 61 | .. function:: compressobj([level]) |
| 62 | |
| 63 | Returns a compression object, to be used for compressing data streams that won't |
| 64 | fit into memory at once. *level* is an integer from ``1`` to ``9`` controlling |
| 65 | the level of compression; ``1`` is fastest and produces the least compression, |
| 66 | ``9`` is slowest and produces the most. The default value is ``6``. |
| 67 | |
| 68 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 69 | .. function:: crc32(data[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
| 71 | .. index:: |
| 72 | single: Cyclic Redundancy Check |
| 73 | single: checksum; Cyclic Redundancy Check |
| 74 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 75 | Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | present, it is used as the starting value of the checksum; otherwise, a fixed |
| 77 | default value is used. This allows computing a running checksum over the |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 78 | concatenation of several inputs. The algorithm is not cryptographically |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | strong, and should not be used for authentication or digital signatures. Since |
| 80 | the algorithm is designed for use as a checksum algorithm, it is not suitable |
| 81 | for use as a general hash algorithm. |
| 82 | |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 83 | Always returns an unsigned 32-bit integer. |
| 84 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 85 | .. note:: |
| 86 | To generate the same numeric value across all Python versions and |
| 87 | platforms use crc32(data) & 0xffffffff. If you are only using |
| 88 | the checksum in packed binary format this is not necessary as the |
Gregory P. Smith | fa6cf39 | 2009-02-01 00:30:50 +0000 | [diff] [blame] | 89 | return value is the correct 32bit binary representation |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 90 | regardless of sign. |
| 91 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 93 | .. function:: decompress(data[, wbits[, bufsize]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 95 | Decompresses the bytes in *data*, returning a bytes object containing the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | uncompressed data. The *wbits* parameter controls the size of the window |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 97 | buffer, and is discussed further below. |
| 98 | If *bufsize* is given, it is used as the initial size of the output |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | buffer. Raises the :exc:`error` exception if any error occurs. |
| 100 | |
| 101 | The absolute value of *wbits* is the base two logarithm of the size of the |
| 102 | history buffer (the "window size") used when compressing data. Its absolute |
| 103 | value should be between 8 and 15 for the most recent versions of the zlib |
| 104 | library, larger values resulting in better compression at the expense of greater |
Benjamin Peterson | 2614cda | 2010-03-21 22:36:19 +0000 | [diff] [blame] | 105 | memory usage. When decompressing a stream, *wbits* must not be smaller |
| 106 | than the size originally used to compress the stream; using a too-small |
| 107 | value will result in an exception. The default value is therefore the |
| 108 | highest value, 15. When *wbits* is negative, the standard |
Jesus Cea | fb7b668 | 2010-05-03 16:14:58 +0000 | [diff] [blame] | 109 | :program:`gzip` header is suppressed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | |
| 111 | *bufsize* is the initial size of the buffer used to hold decompressed data. If |
| 112 | more space is required, the buffer size will be increased as needed, so you |
| 113 | don't have to get this value exactly right; tuning it will only save a few calls |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 114 | to :c:func:`malloc`. The default size is 16384. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | .. function:: decompressobj([wbits]) |
| 118 | |
| 119 | Returns a decompression object, to be used for decompressing data streams that |
| 120 | won't fit into memory at once. The *wbits* parameter controls the size of the |
| 121 | window buffer. |
| 122 | |
Nadeem Vawda | 64d25dd | 2011-09-12 00:04:13 +0200 | [diff] [blame] | 123 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | Compression objects support the following methods: |
| 125 | |
| 126 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 127 | .. method:: Compress.compress(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 129 | Compress *data*, returning a bytes object containing compressed data for at least |
| 130 | part of the data in *data*. This data should be concatenated to the output |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | produced by any preceding calls to the :meth:`compress` method. Some input may |
| 132 | be kept in internal buffers for later processing. |
| 133 | |
| 134 | |
| 135 | .. method:: Compress.flush([mode]) |
| 136 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 137 | All pending input is processed, and a bytes object containing the remaining compressed |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | output is returned. *mode* can be selected from the constants |
| 139 | :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`, |
| 140 | defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 141 | :const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | :const:`Z_FINISH` finishes the compressed stream and prevents compressing any |
| 143 | more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`, |
| 144 | the :meth:`compress` method cannot be called again; the only realistic action is |
| 145 | to delete the object. |
| 146 | |
| 147 | |
| 148 | .. method:: Compress.copy() |
| 149 | |
| 150 | Returns a copy of the compression object. This can be used to efficiently |
| 151 | compress a set of data that share a common initial prefix. |
| 152 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 154 | Decompression objects support the following methods and attributes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | .. attribute:: Decompress.unused_data |
| 158 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 159 | A bytes object which contains any bytes past the end of the compressed data. That is, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | this remains ``""`` until the last byte that contains compression data is |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 161 | available. If the whole bytestring turned out to contain compressed data, this is |
| 162 | ``b""``, an empty bytes object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 | |
| 165 | .. attribute:: Decompress.unconsumed_tail |
| 166 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 167 | A bytes object that contains any data that was not consumed by the last |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | :meth:`decompress` call because it exceeded the limit for the uncompressed data |
| 169 | buffer. This data has not yet been seen by the zlib machinery, so you must feed |
| 170 | it (possibly with further data concatenated to it) back to a subsequent |
| 171 | :meth:`decompress` method call in order to get correct output. |
| 172 | |
| 173 | |
Nadeem Vawda | 1c38546 | 2011-08-13 15:22:40 +0200 | [diff] [blame] | 174 | .. attribute:: Decompress.eof |
| 175 | |
| 176 | A boolean indicating whether the end of the compressed data stream has been |
| 177 | reached. |
| 178 | |
| 179 | This makes it possible to distinguish between a properly-formed compressed |
| 180 | stream, and an incomplete or truncated one. |
| 181 | |
| 182 | .. versionadded:: 3.3 |
| 183 | |
| 184 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 185 | .. method:: Decompress.decompress(data[, max_length]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 187 | Decompress *data*, returning a bytes object containing the uncompressed data |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | corresponding to at least part of the data in *string*. This data should be |
| 189 | concatenated to the output produced by any preceding calls to the |
| 190 | :meth:`decompress` method. Some of the input data may be preserved in internal |
| 191 | buffers for later processing. |
| 192 | |
| 193 | If the optional parameter *max_length* is supplied then the return value will be |
| 194 | no longer than *max_length*. This may mean that not all of the compressed input |
| 195 | can be processed; and unconsumed data will be stored in the attribute |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 196 | :attr:`unconsumed_tail`. This bytestring must be passed to a subsequent call to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 197 | :meth:`decompress` if decompression is to continue. If *max_length* is not |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 198 | supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is |
| 199 | empty. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | .. method:: Decompress.flush([length]) |
| 203 | |
Georg Brandl | 4ad934f | 2011-01-08 21:04:25 +0000 | [diff] [blame] | 204 | All pending input is processed, and a bytes object containing the remaining |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 | uncompressed output is returned. After calling :meth:`flush`, the |
| 206 | :meth:`decompress` method cannot be called again; the only realistic action is |
| 207 | to delete the object. |
| 208 | |
| 209 | The optional parameter *length* sets the initial size of the output buffer. |
| 210 | |
| 211 | |
| 212 | .. method:: Decompress.copy() |
| 213 | |
| 214 | Returns a copy of the decompression object. This can be used to save the state |
| 215 | of the decompressor midway through the data stream in order to speed up random |
| 216 | seeks into the stream at a future point. |
| 217 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 218 | |
Nadeem Vawda | 64d25dd | 2011-09-12 00:04:13 +0200 | [diff] [blame] | 219 | Information about the version of the zlib library in use is available through |
| 220 | the following constants: |
| 221 | |
| 222 | |
| 223 | .. data:: ZLIB_VERSION |
| 224 | |
| 225 | The version string of the zlib library that was used for building the module. |
| 226 | This may be different from the zlib library actually used at runtime, which |
| 227 | is available as :const:`ZLIB_RUNTIME_VERSION`. |
| 228 | |
Nadeem Vawda | 64d25dd | 2011-09-12 00:04:13 +0200 | [diff] [blame] | 229 | |
| 230 | .. data:: ZLIB_RUNTIME_VERSION |
| 231 | |
| 232 | The version string of the zlib library actually loaded by the interpreter. |
| 233 | |
| 234 | .. versionadded:: 3.3 |
| 235 | |
| 236 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | .. seealso:: |
| 238 | |
| 239 | Module :mod:`gzip` |
| 240 | Reading and writing :program:`gzip`\ -format files. |
| 241 | |
| 242 | http://www.zlib.net |
| 243 | The zlib library home page. |
| 244 | |
| 245 | http://www.zlib.net/manual.html |
| 246 | The zlib manual explains the semantics and usage of the library's many |
| 247 | functions. |
| 248 | |