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 | b044b2a | 2009-09-16 16:05:59 +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 | |
Guido van Rossum | 7767711 | 2007-11-05 19:43:04 +0000 | [diff] [blame] | 21 | For reading and writing ``.gz`` files see the :mod:`gzip` module. For |
| 22 | other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and |
| 23 | :mod:`tarfile` modules. |
| 24 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | The 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 Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 33 | .. function:: adler32(data[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 35 | 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] | 36 | 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 Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 39 | concatenation of several inputs. The algorithm is not cryptographically |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Gregory P. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 44 | Always returns an unsigned 32-bit integer. |
| 45 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 46 | .. 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. Smith | fa6cf39 | 2009-02-01 00:30:50 +0000 | [diff] [blame] | 50 | return value is the correct 32bit binary representation |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 51 | regardless of sign. |
| 52 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 54 | .. function:: compress(data[, level]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 56 | Compresses the bytes in *data*, returning a bytes object containing compressed data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | *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 Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 71 | .. function:: crc32(data[, value]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
| 73 | .. index:: |
| 74 | single: Cyclic Redundancy Check |
| 75 | single: checksum; Cyclic Redundancy Check |
| 76 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 77 | Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | 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 Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 80 | concatenation of several inputs. The algorithm is not cryptographically |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | 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. Smith | ab0d8a1 | 2008-03-17 20:24:09 +0000 | [diff] [blame] | 85 | Always returns an unsigned 32-bit integer. |
| 86 | |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 87 | .. 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. Smith | fa6cf39 | 2009-02-01 00:30:50 +0000 | [diff] [blame] | 91 | return value is the correct 32bit binary representation |
Benjamin Peterson | 058e31e | 2009-01-16 03:54:08 +0000 | [diff] [blame] | 92 | regardless of sign. |
| 93 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 95 | .. function:: decompress(data[, wbits[, bufsize]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 97 | Decompresses the bytes in *data*, returning a bytes object containing the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | uncompressed data. The *wbits* parameter controls the size of the window |
| 99 | buffer. If *bufsize* is given, it is used as the initial size of the output |
| 100 | buffer. Raises the :exc:`error` exception if any error occurs. |
| 101 | |
| 102 | The absolute value of *wbits* is the base two logarithm of the size of the |
| 103 | history buffer (the "window size") used when compressing data. Its absolute |
| 104 | value should be between 8 and 15 for the most recent versions of the zlib |
| 105 | library, larger values resulting in better compression at the expense of greater |
| 106 | memory usage. The default value is 15. When *wbits* is negative, the standard |
Jesus Cea | 2be7ec3 | 2010-05-03 16:17:30 +0000 | [diff] [blame] | 107 | :program:`gzip` header is suppressed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
| 109 | *bufsize* is the initial size of the buffer used to hold decompressed data. If |
| 110 | more space is required, the buffer size will be increased as needed, so you |
| 111 | don't have to get this value exactly right; tuning it will only save a few calls |
| 112 | to :cfunc:`malloc`. The default size is 16384. |
| 113 | |
| 114 | |
| 115 | .. function:: decompressobj([wbits]) |
| 116 | |
| 117 | Returns a decompression object, to be used for decompressing data streams that |
| 118 | won't fit into memory at once. The *wbits* parameter controls the size of the |
| 119 | window buffer. |
| 120 | |
| 121 | Compression objects support the following methods: |
| 122 | |
| 123 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 124 | .. method:: Compress.compress(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 126 | Compress *data*, returning a bytes object containing compressed data for at least |
| 127 | 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] | 128 | produced by any preceding calls to the :meth:`compress` method. Some input may |
| 129 | be kept in internal buffers for later processing. |
| 130 | |
| 131 | |
| 132 | .. method:: Compress.flush([mode]) |
| 133 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 134 | 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] | 135 | output is returned. *mode* can be selected from the constants |
| 136 | :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`, |
| 137 | defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 138 | :const:`Z_FULL_FLUSH` allow compressing further bytestrings of data, while |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | :const:`Z_FINISH` finishes the compressed stream and prevents compressing any |
| 140 | more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`, |
| 141 | the :meth:`compress` method cannot be called again; the only realistic action is |
| 142 | to delete the object. |
| 143 | |
| 144 | |
| 145 | .. method:: Compress.copy() |
| 146 | |
| 147 | Returns a copy of the compression object. This can be used to efficiently |
| 148 | compress a set of data that share a common initial prefix. |
| 149 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
| 151 | Decompression objects support the following methods, and two attributes: |
| 152 | |
| 153 | |
| 154 | .. attribute:: Decompress.unused_data |
| 155 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 156 | 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] | 157 | this remains ``""`` until the last byte that contains compression data is |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 158 | available. If the whole bytestring turned out to contain compressed data, this is |
| 159 | ``b""``, an empty bytes object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 160 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 161 | The only way to determine where a bytestring of compressed data ends is by actually |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | decompressing it. This means that when compressed data is contained part of a |
| 163 | larger file, you can only find the end of it by reading data and feeding it |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 164 | followed by some non-empty bytestring into a decompression object's |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 165 | :meth:`decompress` method until the :attr:`unused_data` attribute is no longer |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 166 | empty. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | .. attribute:: Decompress.unconsumed_tail |
| 170 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 171 | 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] | 172 | :meth:`decompress` call because it exceeded the limit for the uncompressed data |
| 173 | buffer. This data has not yet been seen by the zlib machinery, so you must feed |
| 174 | it (possibly with further data concatenated to it) back to a subsequent |
| 175 | :meth:`decompress` method call in order to get correct output. |
| 176 | |
| 177 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 178 | .. method:: Decompress.decompress(data[, max_length]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 180 | Decompress *data*, returning a bytes object containing the uncompressed data |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | corresponding to at least part of the data in *string*. This data should be |
| 182 | concatenated to the output produced by any preceding calls to the |
| 183 | :meth:`decompress` method. Some of the input data may be preserved in internal |
| 184 | buffers for later processing. |
| 185 | |
| 186 | If the optional parameter *max_length* is supplied then the return value will be |
| 187 | no longer than *max_length*. This may mean that not all of the compressed input |
| 188 | can be processed; and unconsumed data will be stored in the attribute |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 189 | :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] | 190 | :meth:`decompress` if decompression is to continue. If *max_length* is not |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 191 | supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is |
| 192 | empty. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | .. method:: Decompress.flush([length]) |
| 196 | |
Georg Brandl | ec78b8b | 2011-01-09 07:59:02 +0000 | [diff] [blame] | 197 | All pending input is processed, and a bytes object containing the remaining |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 198 | uncompressed output is returned. After calling :meth:`flush`, the |
| 199 | :meth:`decompress` method cannot be called again; the only realistic action is |
| 200 | to delete the object. |
| 201 | |
| 202 | The optional parameter *length* sets the initial size of the output buffer. |
| 203 | |
| 204 | |
| 205 | .. method:: Decompress.copy() |
| 206 | |
| 207 | Returns a copy of the decompression object. This can be used to save the state |
| 208 | of the decompressor midway through the data stream in order to speed up random |
| 209 | seeks into the stream at a future point. |
| 210 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 211 | |
| 212 | .. seealso:: |
| 213 | |
| 214 | Module :mod:`gzip` |
| 215 | Reading and writing :program:`gzip`\ -format files. |
| 216 | |
| 217 | http://www.zlib.net |
| 218 | The zlib library home page. |
| 219 | |
| 220 | http://www.zlib.net/manual.html |
| 221 | The zlib manual explains the semantics and usage of the library's many |
| 222 | functions. |
| 223 | |