gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

629 lines
45KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="zlib — Compression compatible with gzip" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/zlib.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. The zlib library has its own home page at https://www.zlib...." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. The zlib library has its own home page at https://www.zlib...." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>zlib — Compression compatible with gzip &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="gzip — Support for gzip files" href="gzip.html" />
  33. <link rel="prev" title="Data Compression and Archiving" href="archiving.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/zlib.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="archiving.html"
  85. title="previous chapter">Data Compression and Archiving</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="gzip.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code> — Support for <strong class="program">gzip</strong> files</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/zlib.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="gzip.html" title="gzip — Support for gzip files"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="archiving.html" title="Data Compression and Archiving"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">Data Compression and Archiving</a> &#187;</li>
  134. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code> — Compression compatible with <strong class="program">gzip</strong></a></li>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-zlib">
  160. <span id="zlib-compression-compatible-with-gzip"></span><h1><a class="reference internal" href="#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> — Compression compatible with <strong class="program">gzip</strong><a class="headerlink" href="#module-zlib" title="Link to this heading">¶</a></h1>
  161. <hr class="docutils" />
  162. <p>For applications that require data compression, the functions in this module
  163. allow compression and decompression, using the zlib library. The zlib library
  164. has its own home page at <a class="reference external" href="https://www.zlib.net">https://www.zlib.net</a>. There are known
  165. incompatibilities between the Python module and versions of the zlib library
  166. earlier than 1.1.3; 1.1.3 has a <a class="reference external" href="https://zlib.net/zlib_faq.html#faq33">security vulnerability</a>, so we recommend using
  167. 1.1.4 or later.</p>
  168. <p>zlib’s functions have many options and often need to be used in a particular
  169. order. This documentation doesn’t attempt to cover all of the permutations;
  170. consult the zlib manual at <a class="reference external" href="http://www.zlib.net/manual.html">http://www.zlib.net/manual.html</a> for authoritative
  171. information.</p>
  172. <p>For reading and writing <code class="docutils literal notranslate"><span class="pre">.gz</span></code> files see the <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> module.</p>
  173. <p>The available exception and functions in this module are:</p>
  174. <dl class="py exception">
  175. <dt class="sig sig-object py" id="zlib.error">
  176. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">error</span></span><a class="headerlink" href="#zlib.error" title="Link to this definition">¶</a></dt>
  177. <dd><p>Exception raised on compression and decompression errors.</p>
  178. </dd></dl>
  179. <dl class="py function">
  180. <dt class="sig sig-object py" id="zlib.adler32">
  181. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">adler32</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.adler32" title="Link to this definition">¶</a></dt>
  182. <dd><p>Computes an Adler-32 checksum of <em>data</em>. (An Adler-32 checksum is almost as
  183. reliable as a CRC32 but can be computed much more quickly.) The result
  184. is an unsigned 32-bit integer. If <em>value</em> is present, it is used as
  185. the starting value of the checksum; otherwise, a default value of 1
  186. is used. Passing in <em>value</em> allows computing a running checksum over the
  187. concatenation of several inputs. The algorithm is not cryptographically
  188. strong, and should not be used for authentication or digital signatures. Since
  189. the algorithm is designed for use as a checksum algorithm, it is not suitable
  190. for use as a general hash algorithm.</p>
  191. <div class="versionchanged">
  192. <p><span class="versionmodified changed">Changed in version 3.0: </span>The result is always unsigned.</p>
  193. </div>
  194. </dd></dl>
  195. <dl class="py function">
  196. <dt class="sig sig-object py" id="zlib.compress">
  197. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">compress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wbits</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">MAX_WBITS</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.compress" title="Link to this definition">¶</a></dt>
  198. <dd><p>Compresses the bytes in <em>data</em>, returning a bytes object containing compressed data.
  199. <em>level</em> is an integer from <code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">9</span></code> or <code class="docutils literal notranslate"><span class="pre">-1</span></code> controlling the level of compression;
  200. <code class="docutils literal notranslate"><span class="pre">1</span></code> (Z_BEST_SPEED) is fastest and produces the least compression, <code class="docutils literal notranslate"><span class="pre">9</span></code> (Z_BEST_COMPRESSION)
  201. is slowest and produces the most. <code class="docutils literal notranslate"><span class="pre">0</span></code> (Z_NO_COMPRESSION) is no compression.
  202. The default value is <code class="docutils literal notranslate"><span class="pre">-1</span></code> (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default
  203. compromise between speed and compression (currently equivalent to level 6).</p>
  204. <p id="compress-wbits">The <em>wbits</em> argument controls the size of the history buffer (or the
  205. “window size”) used when compressing data, and whether a header and
  206. trailer is included in the output. It can take several ranges of values,
  207. defaulting to <code class="docutils literal notranslate"><span class="pre">15</span></code> (MAX_WBITS):</p>
  208. <ul class="simple">
  209. <li><p>+9 to +15: The base-two logarithm of the window size, which
  210. therefore ranges between 512 and 32768. Larger values produce
  211. better compression at the expense of greater memory usage. The
  212. resulting output will include a zlib-specific header and trailer.</p></li>
  213. <li><p>−9 to −15: Uses the absolute value of <em>wbits</em> as the
  214. window size logarithm, while producing a raw output stream with no
  215. header or trailing checksum.</p></li>
  216. <li><p>+25 to +31 = 16 + (9 to 15): Uses the low 4 bits of the value as the
  217. window size logarithm, while including a basic <strong class="program">gzip</strong> header
  218. and trailing checksum in the output.</p></li>
  219. </ul>
  220. <p>Raises the <a class="reference internal" href="#zlib.error" title="zlib.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error</span></code></a> exception if any error occurs.</p>
  221. <div class="versionchanged">
  222. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>level</em> can now be used as a keyword parameter.</p>
  223. </div>
  224. <div class="versionchanged">
  225. <p><span class="versionmodified changed">Changed in version 3.11: </span>The <em>wbits</em> parameter is now available to set window bits and
  226. compression type.</p>
  227. </div>
  228. </dd></dl>
  229. <dl class="py function">
  230. <dt class="sig sig-object py" id="zlib.compressobj">
  231. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">compressobj</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">level=-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">method=DEFLATED</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wbits=MAX_WBITS</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">memLevel=DEF_MEM_LEVEL</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strategy=Z_DEFAULT_STRATEGY</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">zdict</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.compressobj" title="Link to this definition">¶</a></dt>
  232. <dd><p>Returns a compression object, to be used for compressing data streams that won’t
  233. fit into memory at once.</p>
  234. <p><em>level</em> is the compression level – an integer from <code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">9</span></code> or <code class="docutils literal notranslate"><span class="pre">-1</span></code>.
  235. A value of <code class="docutils literal notranslate"><span class="pre">1</span></code> (Z_BEST_SPEED) is fastest and produces the least compression,
  236. while a value of <code class="docutils literal notranslate"><span class="pre">9</span></code> (Z_BEST_COMPRESSION) is slowest and produces the most.
  237. <code class="docutils literal notranslate"><span class="pre">0</span></code> (Z_NO_COMPRESSION) is no compression. The default value is <code class="docutils literal notranslate"><span class="pre">-1</span></code> (Z_DEFAULT_COMPRESSION).
  238. Z_DEFAULT_COMPRESSION represents a default compromise between speed and compression
  239. (currently equivalent to level 6).</p>
  240. <p><em>method</em> is the compression algorithm. Currently, the only supported value is
  241. <code class="xref py py-const docutils literal notranslate"><span class="pre">DEFLATED</span></code>.</p>
  242. <p>The <em>wbits</em> parameter controls the size of the history buffer (or the
  243. “window size”), and what header and trailer format will be used. It has
  244. the same meaning as <a class="reference external" href="#compress-wbits">described for compress()</a>.</p>
  245. <p>The <em>memLevel</em> argument controls the amount of memory used for the
  246. internal compression state. Valid values range from <code class="docutils literal notranslate"><span class="pre">1</span></code> to <code class="docutils literal notranslate"><span class="pre">9</span></code>.
  247. Higher values use more memory, but are faster and produce smaller output.</p>
  248. <p><em>strategy</em> is used to tune the compression algorithm. Possible values are
  249. <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_DEFAULT_STRATEGY</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FILTERED</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_HUFFMAN_ONLY</span></code>,
  250. <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_RLE</span></code> (zlib 1.2.0.1) and <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FIXED</span></code> (zlib 1.2.2.2).</p>
  251. <p><em>zdict</em> is a predefined compression dictionary. This is a sequence of bytes
  252. (such as a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object) containing subsequences that are expected
  253. to occur frequently in the data that is to be compressed. Those subsequences
  254. that are expected to be most common should come at the end of the dictionary.</p>
  255. <div class="versionchanged">
  256. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>zdict</em> parameter and keyword argument support.</p>
  257. </div>
  258. </dd></dl>
  259. <dl class="py function">
  260. <dt class="sig sig-object py" id="zlib.crc32">
  261. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">crc32</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.crc32" title="Link to this definition">¶</a></dt>
  262. <dd><p id="index-0">Computes a CRC (Cyclic Redundancy Check) checksum of <em>data</em>. The
  263. result is an unsigned 32-bit integer. If <em>value</em> is present, it is used
  264. as the starting value of the checksum; otherwise, a default value of 0
  265. is used. Passing in <em>value</em> allows computing a running checksum over the
  266. concatenation of several inputs. The algorithm is not cryptographically
  267. strong, and should not be used for authentication or digital signatures. Since
  268. the algorithm is designed for use as a checksum algorithm, it is not suitable
  269. for use as a general hash algorithm.</p>
  270. <div class="versionchanged">
  271. <p><span class="versionmodified changed">Changed in version 3.0: </span>The result is always unsigned.</p>
  272. </div>
  273. </dd></dl>
  274. <dl class="py function">
  275. <dt class="sig sig-object py" id="zlib.decompress">
  276. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">decompress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wbits</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">MAX_WBITS</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bufsize</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEF_BUF_SIZE</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.decompress" title="Link to this definition">¶</a></dt>
  277. <dd><p>Decompresses the bytes in <em>data</em>, returning a bytes object containing the
  278. uncompressed data. The <em>wbits</em> parameter depends on
  279. the format of <em>data</em>, and is discussed further below.
  280. If <em>bufsize</em> is given, it is used as the initial size of the output
  281. buffer. Raises the <a class="reference internal" href="#zlib.error" title="zlib.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error</span></code></a> exception if any error occurs.</p>
  282. <p id="decompress-wbits">The <em>wbits</em> parameter controls the size of the history buffer
  283. (or “window size”), and what header and trailer format is expected.
  284. It is similar to the parameter for <a class="reference internal" href="#zlib.compressobj" title="zlib.compressobj"><code class="xref py py-func docutils literal notranslate"><span class="pre">compressobj()</span></code></a>, but accepts
  285. more ranges of values:</p>
  286. <ul class="simple">
  287. <li><p>+8 to +15: The base-two logarithm of the window size. The input
  288. must include a zlib header and trailer.</p></li>
  289. <li><p>0: Automatically determine the window size from the zlib header.
  290. Only supported since zlib 1.2.3.5.</p></li>
  291. <li><p>−8 to −15: Uses the absolute value of <em>wbits</em> as the window size
  292. logarithm. The input must be a raw stream with no header or trailer.</p></li>
  293. <li><p>+24 to +31 = 16 + (8 to 15): Uses the low 4 bits of the value as
  294. the window size logarithm. The input must include a gzip header and
  295. trailer.</p></li>
  296. <li><p>+40 to +47 = 32 + (8 to 15): Uses the low 4 bits of the value as
  297. the window size logarithm, and automatically accepts either
  298. the zlib or gzip format.</p></li>
  299. </ul>
  300. <p>When decompressing a stream, the window size must not be smaller
  301. than the size originally used to compress the stream; using a too-small
  302. value may result in an <a class="reference internal" href="#zlib.error" title="zlib.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error</span></code></a> exception. The default <em>wbits</em> value
  303. corresponds to the largest window size and requires a zlib header and
  304. trailer to be included.</p>
  305. <p><em>bufsize</em> is the initial size of the buffer used to hold decompressed data. If
  306. more space is required, the buffer size will be increased as needed, so you
  307. don’t have to get this value exactly right; tuning it will only save a few calls
  308. to <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code>.</p>
  309. <div class="versionchanged">
  310. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>wbits</em> and <em>bufsize</em> can be used as keyword arguments.</p>
  311. </div>
  312. </dd></dl>
  313. <dl class="py function">
  314. <dt class="sig sig-object py" id="zlib.decompressobj">
  315. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">decompressobj</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">wbits=MAX_WBITS</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">zdict</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.decompressobj" title="Link to this definition">¶</a></dt>
  316. <dd><p>Returns a decompression object, to be used for decompressing data streams that
  317. won’t fit into memory at once.</p>
  318. <p>The <em>wbits</em> parameter controls the size of the history buffer (or the
  319. “window size”), and what header and trailer format is expected. It has
  320. the same meaning as <a class="reference external" href="#decompress-wbits">described for decompress()</a>.</p>
  321. <p>The <em>zdict</em> parameter specifies a predefined compression dictionary. If
  322. provided, this must be the same dictionary as was used by the compressor that
  323. produced the data that is to be decompressed.</p>
  324. <div class="admonition note">
  325. <p class="admonition-title">Note</p>
  326. <p>If <em>zdict</em> is a mutable object (such as a <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>), you must not
  327. modify its contents between the call to <a class="reference internal" href="#zlib.decompressobj" title="zlib.decompressobj"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompressobj()</span></code></a> and the first
  328. call to the decompressor’s <code class="docutils literal notranslate"><span class="pre">decompress()</span></code> method.</p>
  329. </div>
  330. <div class="versionchanged">
  331. <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>zdict</em> parameter.</p>
  332. </div>
  333. </dd></dl>
  334. <p>Compression objects support the following methods:</p>
  335. <dl class="py method">
  336. <dt class="sig sig-object py" id="zlib.Compress.compress">
  337. <span class="sig-prename descclassname"><span class="pre">Compress.</span></span><span class="sig-name descname"><span class="pre">compress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Compress.compress" title="Link to this definition">¶</a></dt>
  338. <dd><p>Compress <em>data</em>, returning a bytes object containing compressed data for at least
  339. part of the data in <em>data</em>. This data should be concatenated to the output
  340. produced by any preceding calls to the <a class="reference internal" href="#zlib.compress" title="zlib.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a> method. Some input may
  341. be kept in internal buffers for later processing.</p>
  342. </dd></dl>
  343. <dl class="py method">
  344. <dt class="sig sig-object py" id="zlib.Compress.flush">
  345. <span class="sig-prename descclassname"><span class="pre">Compress.</span></span><span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Compress.flush" title="Link to this definition">¶</a></dt>
  346. <dd><p>All pending input is processed, and a bytes object containing the remaining compressed
  347. output is returned. <em>mode</em> can be selected from the constants
  348. <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_NO_FLUSH</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_PARTIAL_FLUSH</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_SYNC_FLUSH</span></code>,
  349. <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FULL_FLUSH</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_BLOCK</span></code> (zlib 1.2.3.4), or <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code>,
  350. defaulting to <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code>. Except <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code>, all constants
  351. allow compressing further bytestrings of data, while <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code> finishes the
  352. compressed stream and prevents compressing any more data. After calling <a class="reference internal" href="#zlib.Compress.flush" title="zlib.Compress.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a>
  353. with <em>mode</em> set to <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code>, the <a class="reference internal" href="#zlib.compress" title="zlib.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a> method cannot be called again;
  354. the only realistic action is to delete the object.</p>
  355. </dd></dl>
  356. <dl class="py method">
  357. <dt class="sig sig-object py" id="zlib.Compress.copy">
  358. <span class="sig-prename descclassname"><span class="pre">Compress.</span></span><span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Compress.copy" title="Link to this definition">¶</a></dt>
  359. <dd><p>Returns a copy of the compression object. This can be used to efficiently
  360. compress a set of data that share a common initial prefix.</p>
  361. </dd></dl>
  362. <div class="versionchanged">
  363. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <a class="reference internal" href="copy.html#copy.copy" title="copy.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.copy()</span></code></a> and <a class="reference internal" href="copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.deepcopy()</span></code></a> support to compression
  364. objects.</p>
  365. </div>
  366. <p>Decompression objects support the following methods and attributes:</p>
  367. <dl class="py attribute">
  368. <dt class="sig sig-object py" id="zlib.Decompress.unused_data">
  369. <span class="sig-prename descclassname"><span class="pre">Decompress.</span></span><span class="sig-name descname"><span class="pre">unused_data</span></span><a class="headerlink" href="#zlib.Decompress.unused_data" title="Link to this definition">¶</a></dt>
  370. <dd><p>A bytes object which contains any bytes past the end of the compressed data. That is,
  371. this remains <code class="docutils literal notranslate"><span class="pre">b&quot;&quot;</span></code> until the last byte that contains compression data is
  372. available. If the whole bytestring turned out to contain compressed data, this is
  373. <code class="docutils literal notranslate"><span class="pre">b&quot;&quot;</span></code>, an empty bytes object.</p>
  374. </dd></dl>
  375. <dl class="py attribute">
  376. <dt class="sig sig-object py" id="zlib.Decompress.unconsumed_tail">
  377. <span class="sig-prename descclassname"><span class="pre">Decompress.</span></span><span class="sig-name descname"><span class="pre">unconsumed_tail</span></span><a class="headerlink" href="#zlib.Decompress.unconsumed_tail" title="Link to this definition">¶</a></dt>
  378. <dd><p>A bytes object that contains any data that was not consumed by the last
  379. <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> call because it exceeded the limit for the uncompressed data
  380. buffer. This data has not yet been seen by the zlib machinery, so you must feed
  381. it (possibly with further data concatenated to it) back to a subsequent
  382. <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method call in order to get correct output.</p>
  383. </dd></dl>
  384. <dl class="py attribute">
  385. <dt class="sig sig-object py" id="zlib.Decompress.eof">
  386. <span class="sig-prename descclassname"><span class="pre">Decompress.</span></span><span class="sig-name descname"><span class="pre">eof</span></span><a class="headerlink" href="#zlib.Decompress.eof" title="Link to this definition">¶</a></dt>
  387. <dd><p>A boolean indicating whether the end of the compressed data stream has been
  388. reached.</p>
  389. <p>This makes it possible to distinguish between a properly formed compressed
  390. stream, and an incomplete or truncated one.</p>
  391. <div class="versionadded">
  392. <p><span class="versionmodified added">New in version 3.3.</span></p>
  393. </div>
  394. </dd></dl>
  395. <dl class="py method">
  396. <dt class="sig sig-object py" id="zlib.Decompress.decompress">
  397. <span class="sig-prename descclassname"><span class="pre">Decompress.</span></span><span class="sig-name descname"><span class="pre">decompress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_length</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Decompress.decompress" title="Link to this definition">¶</a></dt>
  398. <dd><p>Decompress <em>data</em>, returning a bytes object containing the uncompressed data
  399. corresponding to at least part of the data in <em>string</em>. This data should be
  400. concatenated to the output produced by any preceding calls to the
  401. <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method. Some of the input data may be preserved in internal
  402. buffers for later processing.</p>
  403. <p>If the optional parameter <em>max_length</em> is non-zero then the return value will be
  404. no longer than <em>max_length</em>. This may mean that not all of the compressed input
  405. can be processed; and unconsumed data will be stored in the attribute
  406. <a class="reference internal" href="#zlib.Decompress.unconsumed_tail" title="zlib.Decompress.unconsumed_tail"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unconsumed_tail</span></code></a>. This bytestring must be passed to a subsequent call to
  407. <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> if decompression is to continue. If <em>max_length</em> is zero
  408. then the whole input is decompressed, and <a class="reference internal" href="#zlib.Decompress.unconsumed_tail" title="zlib.Decompress.unconsumed_tail"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unconsumed_tail</span></code></a> is empty.</p>
  409. <div class="versionchanged">
  410. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>max_length</em> can be used as a keyword argument.</p>
  411. </div>
  412. </dd></dl>
  413. <dl class="py method">
  414. <dt class="sig sig-object py" id="zlib.Decompress.flush">
  415. <span class="sig-prename descclassname"><span class="pre">Decompress.</span></span><span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">length</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Decompress.flush" title="Link to this definition">¶</a></dt>
  416. <dd><p>All pending input is processed, and a bytes object containing the remaining
  417. uncompressed output is returned. After calling <a class="reference internal" href="#zlib.Decompress.flush" title="zlib.Decompress.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a>, the
  418. <a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method cannot be called again; the only realistic action is
  419. to delete the object.</p>
  420. <p>The optional parameter <em>length</em> sets the initial size of the output buffer.</p>
  421. </dd></dl>
  422. <dl class="py method">
  423. <dt class="sig sig-object py" id="zlib.Decompress.copy">
  424. <span class="sig-prename descclassname"><span class="pre">Decompress.</span></span><span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Decompress.copy" title="Link to this definition">¶</a></dt>
  425. <dd><p>Returns a copy of the decompression object. This can be used to save the state
  426. of the decompressor midway through the data stream in order to speed up random
  427. seeks into the stream at a future point.</p>
  428. </dd></dl>
  429. <div class="versionchanged">
  430. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <a class="reference internal" href="copy.html#copy.copy" title="copy.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.copy()</span></code></a> and <a class="reference internal" href="copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal notranslate"><span class="pre">copy.deepcopy()</span></code></a> support to decompression
  431. objects.</p>
  432. </div>
  433. <p>Information about the version of the zlib library in use is available through
  434. the following constants:</p>
  435. <dl class="py data">
  436. <dt class="sig sig-object py" id="zlib.ZLIB_VERSION">
  437. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">ZLIB_VERSION</span></span><a class="headerlink" href="#zlib.ZLIB_VERSION" title="Link to this definition">¶</a></dt>
  438. <dd><p>The version string of the zlib library that was used for building the module.
  439. This may be different from the zlib library actually used at runtime, which
  440. is available as <a class="reference internal" href="#zlib.ZLIB_RUNTIME_VERSION" title="zlib.ZLIB_RUNTIME_VERSION"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZLIB_RUNTIME_VERSION</span></code></a>.</p>
  441. </dd></dl>
  442. <dl class="py data">
  443. <dt class="sig sig-object py" id="zlib.ZLIB_RUNTIME_VERSION">
  444. <span class="sig-prename descclassname"><span class="pre">zlib.</span></span><span class="sig-name descname"><span class="pre">ZLIB_RUNTIME_VERSION</span></span><a class="headerlink" href="#zlib.ZLIB_RUNTIME_VERSION" title="Link to this definition">¶</a></dt>
  445. <dd><p>The version string of the zlib library actually loaded by the interpreter.</p>
  446. <div class="versionadded">
  447. <p><span class="versionmodified added">New in version 3.3.</span></p>
  448. </div>
  449. </dd></dl>
  450. <div class="admonition seealso">
  451. <p class="admonition-title">See also</p>
  452. <dl class="simple">
  453. <dt>Module <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a></dt><dd><p>Reading and writing <strong class="program">gzip</strong>-format files.</p>
  454. </dd>
  455. <dt><a class="reference external" href="http://www.zlib.net">http://www.zlib.net</a></dt><dd><p>The zlib library home page.</p>
  456. </dd>
  457. <dt><a class="reference external" href="http://www.zlib.net/manual.html">http://www.zlib.net/manual.html</a></dt><dd><p>The zlib manual explains the semantics and usage of the library’s many
  458. functions.</p>
  459. </dd>
  460. </dl>
  461. </div>
  462. </section>
  463. <div class="clearer"></div>
  464. </div>
  465. </div>
  466. </div>
  467. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  468. <div class="sphinxsidebarwrapper">
  469. <div>
  470. <h4>Previous topic</h4>
  471. <p class="topless"><a href="archiving.html"
  472. title="previous chapter">Data Compression and Archiving</a></p>
  473. </div>
  474. <div>
  475. <h4>Next topic</h4>
  476. <p class="topless"><a href="gzip.html"
  477. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code> — Support for <strong class="program">gzip</strong> files</a></p>
  478. </div>
  479. <div role="note" aria-label="source link">
  480. <h3>This Page</h3>
  481. <ul class="this-page-menu">
  482. <li><a href="../bugs.html">Report a Bug</a></li>
  483. <li>
  484. <a href="https://github.com/python/cpython/blob/main/Doc/library/zlib.rst"
  485. rel="nofollow">Show Source
  486. </a>
  487. </li>
  488. </ul>
  489. </div>
  490. </div>
  491. <div id="sidebarbutton" title="Collapse sidebar">
  492. <span>«</span>
  493. </div>
  494. </div>
  495. <div class="clearer"></div>
  496. </div>
  497. <div class="related" role="navigation" aria-label="related navigation">
  498. <h3>Navigation</h3>
  499. <ul>
  500. <li class="right" style="margin-right: 10px">
  501. <a href="../genindex.html" title="General Index"
  502. >index</a></li>
  503. <li class="right" >
  504. <a href="../py-modindex.html" title="Python Module Index"
  505. >modules</a> |</li>
  506. <li class="right" >
  507. <a href="gzip.html" title="gzip — Support for gzip files"
  508. >next</a> |</li>
  509. <li class="right" >
  510. <a href="archiving.html" title="Data Compression and Archiving"
  511. >previous</a> |</li>
  512. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  513. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  514. <li class="switchers">
  515. <div class="language_switcher_placeholder"></div>
  516. <div class="version_switcher_placeholder"></div>
  517. </li>
  518. <li>
  519. </li>
  520. <li id="cpython-language-and-version">
  521. <a href="../index.html">3.12.3 Documentation</a> &#187;
  522. </li>
  523. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  524. <li class="nav-item nav-item-2"><a href="archiving.html" >Data Compression and Archiving</a> &#187;</li>
  525. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code> — Compression compatible with <strong class="program">gzip</strong></a></li>
  526. <li class="right">
  527. <div class="inline-search" role="search">
  528. <form class="inline-search" action="../search.html" method="get">
  529. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  530. <input type="submit" value="Go" />
  531. </form>
  532. </div>
  533. |
  534. </li>
  535. <li class="right">
  536. <label class="theme-selector-label">
  537. Theme
  538. <select class="theme-selector" oninput="activateTheme(this.value)">
  539. <option value="auto" selected>Auto</option>
  540. <option value="light">Light</option>
  541. <option value="dark">Dark</option>
  542. </select>
  543. </label> |</li>
  544. </ul>
  545. </div>
  546. <div class="footer">
  547. &copy;
  548. <a href="../copyright.html">
  549. Copyright
  550. </a>
  551. 2001-2024, Python Software Foundation.
  552. <br />
  553. This page is licensed under the Python Software Foundation License Version 2.
  554. <br />
  555. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  556. <br />
  557. See <a href="/license.html">History and License</a> for more information.<br />
  558. <br />
  559. The Python Software Foundation is a non-profit corporation.
  560. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  561. <br />
  562. <br />
  563. Last updated on Apr 09, 2024 (13:47 UTC).
  564. <a href="/bugs.html">Found a bug</a>?
  565. <br />
  566. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  567. </div>
  568. </body>
  569. </html>
上海开阖软件有限公司 沪ICP备12045867号-1