/Doc/library/zlib.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 247 lines · 171 code · 76 blank · 0 comment · 0 complexity · 06de3a63d63a51e16295f5560467aa6f MD5 · raw file

  1. :mod:`zlib` --- Compression compatible with :program:`gzip`
  2. ===========================================================
  3. .. module:: zlib
  4. :synopsis: Low-level interface to compression and decompression routines compatible with
  5. gzip.
  6. For applications that require data compression, the functions in this module
  7. allow compression and decompression, using the zlib library. The zlib library
  8. has its own home page at http://www.zlib.net. There are known
  9. incompatibilities between the Python module and versions of the zlib library
  10. earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using
  11. 1.1.4 or later.
  12. zlib's functions have many options and often need to be used in a particular
  13. order. This documentation doesn't attempt to cover all of the permutations;
  14. consult the zlib manual at http://www.zlib.net/manual.html for authoritative
  15. information.
  16. For reading and writing ``.gz`` files see the :mod:`gzip` module. For
  17. other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and
  18. :mod:`tarfile` modules.
  19. The available exception and functions in this module are:
  20. .. exception:: error
  21. Exception raised on compression and decompression errors.
  22. .. function:: adler32(data[, value])
  23. Computes a Adler-32 checksum of *data*. (An Adler-32 checksum is almost as
  24. reliable as a CRC32 but can be computed much more quickly.) If *value* is
  25. present, it is used as the starting value of the checksum; otherwise, a fixed
  26. default value is used. This allows computing a running checksum over the
  27. concatenation of several inputs. The algorithm is not cryptographically
  28. strong, and should not be used for authentication or digital signatures. Since
  29. the algorithm is designed for use as a checksum algorithm, it is not suitable
  30. for use as a general hash algorithm.
  31. This function always returns an integer object.
  32. .. note::
  33. To generate the same numeric value across all Python versions and
  34. platforms use adler32(data) & 0xffffffff. If you are only using
  35. the checksum in packed binary format this is not necessary as the
  36. return value is the correct 32bit binary representation
  37. regardless of sign.
  38. .. versionchanged:: 2.6
  39. The return value is in the range [-2**31, 2**31-1]
  40. regardless of platform. In older versions the value is
  41. signed on some platforms and unsigned on others.
  42. .. versionchanged:: 3.0
  43. The return value is unsigned and in the range [0, 2**32-1]
  44. regardless of platform.
  45. .. function:: compress(string[, level])
  46. Compresses the data in *string*, returning a string contained compressed data.
  47. *level* is an integer from ``1`` to ``9`` controlling the level of compression;
  48. ``1`` is fastest and produces the least compression, ``9`` is slowest and
  49. produces the most. The default value is ``6``. Raises the :exc:`error`
  50. exception if any error occurs.
  51. .. function:: compressobj([level])
  52. Returns a compression object, to be used for compressing data streams that won't
  53. fit into memory at once. *level* is an integer from ``1`` to ``9`` controlling
  54. the level of compression; ``1`` is fastest and produces the least compression,
  55. ``9`` is slowest and produces the most. The default value is ``6``.
  56. .. function:: crc32(data[, value])
  57. .. index::
  58. single: Cyclic Redundancy Check
  59. single: checksum; Cyclic Redundancy Check
  60. Computes a CRC (Cyclic Redundancy Check) checksum of *data*. If *value* is
  61. present, it is used as the starting value of the checksum; otherwise, a fixed
  62. default value is used. This allows computing a running checksum over the
  63. concatenation of several inputs. The algorithm is not cryptographically
  64. strong, and should not be used for authentication or digital signatures. Since
  65. the algorithm is designed for use as a checksum algorithm, it is not suitable
  66. for use as a general hash algorithm.
  67. This function always returns an integer object.
  68. .. note::
  69. To generate the same numeric value across all Python versions and
  70. platforms use crc32(data) & 0xffffffff. If you are only using
  71. the checksum in packed binary format this is not necessary as the
  72. return value is the correct 32bit binary representation
  73. regardless of sign.
  74. .. versionchanged:: 2.6
  75. The return value is in the range [-2**31, 2**31-1]
  76. regardless of platform. In older versions the value would be
  77. signed on some platforms and unsigned on others.
  78. .. versionchanged:: 3.0
  79. The return value is unsigned and in the range [0, 2**32-1]
  80. regardless of platform.
  81. .. function:: decompress(string[, wbits[, bufsize]])
  82. Decompresses the data in *string*, returning a string containing the
  83. uncompressed data. The *wbits* parameter controls the size of the window
  84. buffer. If *bufsize* is given, it is used as the initial size of the output
  85. buffer. Raises the :exc:`error` exception if any error occurs.
  86. The absolute value of *wbits* is the base two logarithm of the size of the
  87. history buffer (the "window size") used when compressing data. Its absolute
  88. value should be between 8 and 15 for the most recent versions of the zlib
  89. library, larger values resulting in better compression at the expense of greater
  90. memory usage. The default value is 15. When *wbits* is negative, the standard
  91. :program:`gzip` header is suppressed; this is an undocumented feature of the
  92. zlib library, used for compatibility with :program:`unzip`'s compression file
  93. format.
  94. *bufsize* is the initial size of the buffer used to hold decompressed data. If
  95. more space is required, the buffer size will be increased as needed, so you
  96. don't have to get this value exactly right; tuning it will only save a few calls
  97. to :cfunc:`malloc`. The default size is 16384.
  98. .. function:: decompressobj([wbits])
  99. Returns a decompression object, to be used for decompressing data streams that
  100. won't fit into memory at once. The *wbits* parameter controls the size of the
  101. window buffer.
  102. Compression objects support the following methods:
  103. .. method:: Compress.compress(string)
  104. Compress *string*, returning a string containing compressed data for at least
  105. part of the data in *string*. This data should be concatenated to the output
  106. produced by any preceding calls to the :meth:`compress` method. Some input may
  107. be kept in internal buffers for later processing.
  108. .. method:: Compress.flush([mode])
  109. All pending input is processed, and a string containing the remaining compressed
  110. output is returned. *mode* can be selected from the constants
  111. :const:`Z_SYNC_FLUSH`, :const:`Z_FULL_FLUSH`, or :const:`Z_FINISH`,
  112. defaulting to :const:`Z_FINISH`. :const:`Z_SYNC_FLUSH` and
  113. :const:`Z_FULL_FLUSH` allow compressing further strings of data, while
  114. :const:`Z_FINISH` finishes the compressed stream and prevents compressing any
  115. more data. After calling :meth:`flush` with *mode* set to :const:`Z_FINISH`,
  116. the :meth:`compress` method cannot be called again; the only realistic action is
  117. to delete the object.
  118. .. method:: Compress.copy()
  119. Returns a copy of the compression object. This can be used to efficiently
  120. compress a set of data that share a common initial prefix.
  121. .. versionadded:: 2.5
  122. Decompression objects support the following methods, and two attributes:
  123. .. attribute:: Decompress.unused_data
  124. A string which contains any bytes past the end of the compressed data. That is,
  125. this remains ``""`` until the last byte that contains compression data is
  126. available. If the whole string turned out to contain compressed data, this is
  127. ``""``, the empty string.
  128. The only way to determine where a string of compressed data ends is by actually
  129. decompressing it. This means that when compressed data is contained part of a
  130. larger file, you can only find the end of it by reading data and feeding it
  131. followed by some non-empty string into a decompression object's
  132. :meth:`decompress` method until the :attr:`unused_data` attribute is no longer
  133. the empty string.
  134. .. attribute:: Decompress.unconsumed_tail
  135. A string that contains any data that was not consumed by the last
  136. :meth:`decompress` call because it exceeded the limit for the uncompressed data
  137. buffer. This data has not yet been seen by the zlib machinery, so you must feed
  138. it (possibly with further data concatenated to it) back to a subsequent
  139. :meth:`decompress` method call in order to get correct output.
  140. .. method:: Decompress.decompress(string[, max_length])
  141. Decompress *string*, returning a string containing the uncompressed data
  142. corresponding to at least part of the data in *string*. This data should be
  143. concatenated to the output produced by any preceding calls to the
  144. :meth:`decompress` method. Some of the input data may be preserved in internal
  145. buffers for later processing.
  146. If the optional parameter *max_length* is supplied then the return value will be
  147. no longer than *max_length*. This may mean that not all of the compressed input
  148. can be processed; and unconsumed data will be stored in the attribute
  149. :attr:`unconsumed_tail`. This string must be passed to a subsequent call to
  150. :meth:`decompress` if decompression is to continue. If *max_length* is not
  151. supplied then the whole input is decompressed, and :attr:`unconsumed_tail` is an
  152. empty string.
  153. .. method:: Decompress.flush([length])
  154. All pending input is processed, and a string containing the remaining
  155. uncompressed output is returned. After calling :meth:`flush`, the
  156. :meth:`decompress` method cannot be called again; the only realistic action is
  157. to delete the object.
  158. The optional parameter *length* sets the initial size of the output buffer.
  159. .. method:: Decompress.copy()
  160. Returns a copy of the decompression object. This can be used to save the state
  161. of the decompressor midway through the data stream in order to speed up random
  162. seeks into the stream at a future point.
  163. .. versionadded:: 2.5
  164. .. seealso::
  165. Module :mod:`gzip`
  166. Reading and writing :program:`gzip`\ -format files.
  167. http://www.zlib.net
  168. The zlib library home page.
  169. http://www.zlib.net/manual.html
  170. The zlib manual explains the semantics and usage of the library's many
  171. functions.