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.

763 line
69KB

  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="lzma — Compression using the LZMA algorithm" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/lzma.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/lzma.py This module provides classes and convenience functions for compressing and decompressing data using the LZMA compression algorithm. Also included is a file interface suppor..." />
  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="Source code: Lib/lzma.py This module provides classes and convenience functions for compressing and decompressing data using the LZMA compression algorithm. Also included is a file interface suppor..." />
  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>lzma — Compression using the LZMA algorithm &#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="zipfile — Work with ZIP archives" href="zipfile.html" />
  33. <link rel="prev" title="bz2 — Support for bzip2 compression" href="bz2.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/lzma.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. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a><ul>
  86. <li><a class="reference internal" href="#reading-and-writing-compressed-files">Reading and writing compressed files</a></li>
  87. <li><a class="reference internal" href="#compressing-and-decompressing-data-in-memory">Compressing and decompressing data in memory</a></li>
  88. <li><a class="reference internal" href="#miscellaneous">Miscellaneous</a></li>
  89. <li><a class="reference internal" href="#specifying-custom-filter-chains">Specifying custom filter chains</a></li>
  90. <li><a class="reference internal" href="#examples">Examples</a></li>
  91. </ul>
  92. </li>
  93. </ul>
  94. </div>
  95. <div>
  96. <h4>Previous topic</h4>
  97. <p class="topless"><a href="bz2.html"
  98. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> — Support for <strong class="program">bzip2</strong> compression</a></p>
  99. </div>
  100. <div>
  101. <h4>Next topic</h4>
  102. <p class="topless"><a href="zipfile.html"
  103. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a></p>
  104. </div>
  105. <div role="note" aria-label="source link">
  106. <h3>This Page</h3>
  107. <ul class="this-page-menu">
  108. <li><a href="../bugs.html">Report a Bug</a></li>
  109. <li>
  110. <a href="https://github.com/python/cpython/blob/main/Doc/library/lzma.rst"
  111. rel="nofollow">Show Source
  112. </a>
  113. </li>
  114. </ul>
  115. </div>
  116. </nav>
  117. </div>
  118. </div>
  119. <div class="related" role="navigation" aria-label="related navigation">
  120. <h3>Navigation</h3>
  121. <ul>
  122. <li class="right" style="margin-right: 10px">
  123. <a href="../genindex.html" title="General Index"
  124. accesskey="I">index</a></li>
  125. <li class="right" >
  126. <a href="../py-modindex.html" title="Python Module Index"
  127. >modules</a> |</li>
  128. <li class="right" >
  129. <a href="zipfile.html" title="zipfile — Work with ZIP archives"
  130. accesskey="N">next</a> |</li>
  131. <li class="right" >
  132. <a href="bz2.html" title="bz2 — Support for bzip2 compression"
  133. accesskey="P">previous</a> |</li>
  134. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  135. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  136. <li class="switchers">
  137. <div class="language_switcher_placeholder"></div>
  138. <div class="version_switcher_placeholder"></div>
  139. </li>
  140. <li>
  141. </li>
  142. <li id="cpython-language-and-version">
  143. <a href="../index.html">3.12.3 Documentation</a> &#187;
  144. </li>
  145. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  146. <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">Data Compression and Archiving</a> &#187;</li>
  147. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a></li>
  148. <li class="right">
  149. <div class="inline-search" role="search">
  150. <form class="inline-search" action="../search.html" method="get">
  151. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  152. <input type="submit" value="Go" />
  153. </form>
  154. </div>
  155. |
  156. </li>
  157. <li class="right">
  158. <label class="theme-selector-label">
  159. Theme
  160. <select class="theme-selector" oninput="activateTheme(this.value)">
  161. <option value="auto" selected>Auto</option>
  162. <option value="light">Light</option>
  163. <option value="dark">Dark</option>
  164. </select>
  165. </label> |</li>
  166. </ul>
  167. </div>
  168. <div class="document">
  169. <div class="documentwrapper">
  170. <div class="bodywrapper">
  171. <div class="body" role="main">
  172. <section id="module-lzma">
  173. <span id="lzma-compression-using-the-lzma-algorithm"></span><h1><a class="reference internal" href="#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> — Compression using the LZMA algorithm<a class="headerlink" href="#module-lzma" title="Link to this heading">¶</a></h1>
  174. <div class="versionadded">
  175. <p><span class="versionmodified added">New in version 3.3.</span></p>
  176. </div>
  177. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/lzma.py">Lib/lzma.py</a></p>
  178. <hr class="docutils" />
  179. <p>This module provides classes and convenience functions for compressing and
  180. decompressing data using the LZMA compression algorithm. Also included is a file
  181. interface supporting the <code class="docutils literal notranslate"><span class="pre">.xz</span></code> and legacy <code class="docutils literal notranslate"><span class="pre">.lzma</span></code> file formats used by the
  182. <strong class="program">xz</strong> utility, as well as raw compressed streams.</p>
  183. <p>The interface provided by this module is very similar to that of the <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a>
  184. module. Note that <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> and <a class="reference internal" href="bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2.BZ2File</span></code></a> are <em>not</em>
  185. thread-safe, so if you need to use a single <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> instance
  186. from multiple threads, it is necessary to protect it with a lock.</p>
  187. <dl class="py exception">
  188. <dt class="sig sig-object py" id="lzma.LZMAError">
  189. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">lzma.</span></span><span class="sig-name descname"><span class="pre">LZMAError</span></span><a class="headerlink" href="#lzma.LZMAError" title="Link to this definition">¶</a></dt>
  190. <dd><p>This exception is raised when an error occurs during compression or
  191. decompression, or while initializing the compressor/decompressor state.</p>
  192. </dd></dl>
  193. <section id="reading-and-writing-compressed-files">
  194. <h2>Reading and writing compressed files<a class="headerlink" href="#reading-and-writing-compressed-files" title="Link to this heading">¶</a></h2>
  195. <dl class="py function">
  196. <dt class="sig sig-object py" id="lzma.open">
  197. <span class="sig-prename descclassname"><span class="pre">lzma.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'rb'</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">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check</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">preset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.open" title="Link to this definition">¶</a></dt>
  198. <dd><p>Open an LZMA-compressed file in binary or text mode, returning a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file
  199. object</span></a>.</p>
  200. <p>The <em>filename</em> argument can be either an actual file name (given as a
  201. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></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> or <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like</span></a> object), in
  202. which case the named file is opened, or it can be an existing file object
  203. to read from or write to.</p>
  204. <p>The <em>mode</em> argument can be any of <code class="docutils literal notranslate"><span class="pre">&quot;r&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;rb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;w&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;wb&quot;</span></code>,
  205. <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;a&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;ab&quot;</span></code> for binary mode, or <code class="docutils literal notranslate"><span class="pre">&quot;rt&quot;</span></code>,
  206. <code class="docutils literal notranslate"><span class="pre">&quot;wt&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xt&quot;</span></code>, or <code class="docutils literal notranslate"><span class="pre">&quot;at&quot;</span></code> for text mode. The default is <code class="docutils literal notranslate"><span class="pre">&quot;rb&quot;</span></code>.</p>
  207. <p>When opening a file for reading, the <em>format</em> and <em>filters</em> arguments have
  208. the same meanings as for <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a>. In this case, the <em>check</em>
  209. and <em>preset</em> arguments should not be used.</p>
  210. <p>When opening a file for writing, the <em>format</em>, <em>check</em>, <em>preset</em> and
  211. <em>filters</em> arguments have the same meanings as for <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a>.</p>
  212. <p>For binary mode, this function is equivalent to the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a>
  213. constructor: <code class="docutils literal notranslate"><span class="pre">LZMAFile(filename,</span> <span class="pre">mode,</span> <span class="pre">...)</span></code>. In this case, the <em>encoding</em>,
  214. <em>errors</em> and <em>newline</em> arguments must not be provided.</p>
  215. <p>For text mode, a <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> object is created, and wrapped in an
  216. <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> instance with the specified encoding, error
  217. handling behavior, and line ending(s).</p>
  218. <div class="versionchanged">
  219. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for the <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;xt&quot;</span></code> modes.</p>
  220. </div>
  221. <div class="versionchanged">
  222. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  223. </div>
  224. </dd></dl>
  225. <dl class="py class">
  226. <dt class="sig sig-object py" id="lzma.LZMAFile">
  227. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">lzma.</span></span><span class="sig-name descname"><span class="pre">LZMAFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</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">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check</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">preset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMAFile" title="Link to this definition">¶</a></dt>
  228. <dd><p>Open an LZMA-compressed file in binary mode.</p>
  229. <p>An <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> can wrap an already-open <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>, or operate
  230. directly on a named file. The <em>filename</em> argument specifies either the file
  231. object to wrap, or the name of the file to open (as a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>,
  232. <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> or <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like</span></a> object). When wrapping an
  233. existing file object, the wrapped file will not be closed when the
  234. <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> is closed.</p>
  235. <p>The <em>mode</em> argument can be either <code class="docutils literal notranslate"><span class="pre">&quot;r&quot;</span></code> for reading (default), <code class="docutils literal notranslate"><span class="pre">&quot;w&quot;</span></code> for
  236. overwriting, <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code> for exclusive creation, or <code class="docutils literal notranslate"><span class="pre">&quot;a&quot;</span></code> for appending. These
  237. can equivalently be given as <code class="docutils literal notranslate"><span class="pre">&quot;rb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;wb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;ab&quot;</span></code>
  238. respectively.</p>
  239. <p>If <em>filename</em> is a file object (rather than an actual file name), a mode of
  240. <code class="docutils literal notranslate"><span class="pre">&quot;w&quot;</span></code> does not truncate the file, and is instead equivalent to <code class="docutils literal notranslate"><span class="pre">&quot;a&quot;</span></code>.</p>
  241. <p>When opening a file for reading, the input file may be the concatenation of
  242. multiple separate compressed streams. These are transparently decoded as a
  243. single logical stream.</p>
  244. <p>When opening a file for reading, the <em>format</em> and <em>filters</em> arguments have
  245. the same meanings as for <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a>. In this case, the <em>check</em>
  246. and <em>preset</em> arguments should not be used.</p>
  247. <p>When opening a file for writing, the <em>format</em>, <em>check</em>, <em>preset</em> and
  248. <em>filters</em> arguments have the same meanings as for <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a>.</p>
  249. <p><a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> supports all the members specified by
  250. <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a>, except for <a class="reference internal" href="io.html#io.BufferedIOBase.detach" title="io.BufferedIOBase.detach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code></a>
  251. and <a class="reference internal" href="io.html#io.IOBase.truncate" title="io.IOBase.truncate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">truncate()</span></code></a>.
  252. Iteration and the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement are supported.</p>
  253. <p>The following method is also provided:</p>
  254. <dl class="py method">
  255. <dt class="sig sig-object py" id="lzma.LZMAFile.peek">
  256. <span class="sig-name descname"><span class="pre">peek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMAFile.peek" title="Link to this definition">¶</a></dt>
  257. <dd><p>Return buffered data without advancing the file position. At least one
  258. byte of data will be returned, unless EOF has been reached. The exact
  259. number of bytes returned is unspecified (the <em>size</em> argument is ignored).</p>
  260. <div class="admonition note">
  261. <p class="admonition-title">Note</p>
  262. <p>While calling <a class="reference internal" href="#lzma.LZMAFile.peek" title="lzma.LZMAFile.peek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">peek()</span></code></a> does not change the file position of
  263. the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a>, it may change the position of the underlying
  264. file object (e.g. if the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> was constructed by passing a
  265. file object for <em>filename</em>).</p>
  266. </div>
  267. </dd></dl>
  268. <div class="versionchanged">
  269. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for the <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code> and <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code> modes.</p>
  270. </div>
  271. <div class="versionchanged">
  272. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <a class="reference internal" href="io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> method now accepts an argument of
  273. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  274. </div>
  275. <div class="versionchanged">
  276. <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  277. </div>
  278. </dd></dl>
  279. </section>
  280. <section id="compressing-and-decompressing-data-in-memory">
  281. <h2>Compressing and decompressing data in memory<a class="headerlink" href="#compressing-and-decompressing-data-in-memory" title="Link to this heading">¶</a></h2>
  282. <dl class="py class">
  283. <dt class="sig sig-object py" id="lzma.LZMACompressor">
  284. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">lzma.</span></span><span class="sig-name descname"><span class="pre">LZMACompressor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">FORMAT_XZ</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check</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">preset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMACompressor" title="Link to this definition">¶</a></dt>
  285. <dd><p>Create a compressor object, which can be used to compress data incrementally.</p>
  286. <p>For a more convenient way of compressing a single chunk of data, see
  287. <a class="reference internal" href="#lzma.compress" title="lzma.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">compress()</span></code></a>.</p>
  288. <p>The <em>format</em> argument specifies what container format should be used.
  289. Possible values are:</p>
  290. <ul class="simple">
  291. <li><dl class="simple">
  292. <dt><code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code>: The <code class="docutils literal notranslate"><span class="pre">.xz</span></code> container format.</dt><dd><p>This is the default format.</p>
  293. </dd>
  294. </dl>
  295. </li>
  296. <li><dl class="simple">
  297. <dt><code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code>: The legacy <code class="docutils literal notranslate"><span class="pre">.lzma</span></code> container format.</dt><dd><p>This format is more limited than <code class="docutils literal notranslate"><span class="pre">.xz</span></code> – it does not support integrity
  298. checks or multiple filters.</p>
  299. </dd>
  300. </dl>
  301. </li>
  302. <li><dl class="simple">
  303. <dt><code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code>: A raw data stream, not using any container format.</dt><dd><p>This format specifier does not support integrity checks, and requires that
  304. you always specify a custom filter chain (for both compression and
  305. decompression). Additionally, data compressed in this manner cannot be
  306. decompressed using <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_AUTO</span></code> (see <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a>).</p>
  307. </dd>
  308. </dl>
  309. </li>
  310. </ul>
  311. <p>The <em>check</em> argument specifies the type of integrity check to include in the
  312. compressed data. This check is used when decompressing, to ensure that the
  313. data has not been corrupted. Possible values are:</p>
  314. <ul class="simple">
  315. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_NONE</span></code>: No integrity check.
  316. This is the default (and the only acceptable value) for
  317. <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code>.</p></li>
  318. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC32</span></code>: 32-bit Cyclic Redundancy Check.</p></li>
  319. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC64</span></code>: 64-bit Cyclic Redundancy Check.
  320. This is the default for <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code>.</p></li>
  321. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_SHA256</span></code>: 256-bit Secure Hash Algorithm.</p></li>
  322. </ul>
  323. <p>If the specified check is not supported, an <a class="reference internal" href="#lzma.LZMAError" title="lzma.LZMAError"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAError</span></code></a> is raised.</p>
  324. <p>The compression settings can be specified either as a preset compression
  325. level (with the <em>preset</em> argument), or in detail as a custom filter chain
  326. (with the <em>filters</em> argument).</p>
  327. <p>The <em>preset</em> argument (if provided) should be an integer between <code class="docutils literal notranslate"><span class="pre">0</span></code> and
  328. <code class="docutils literal notranslate"><span class="pre">9</span></code> (inclusive), optionally OR-ed with the constant
  329. <code class="xref py py-const docutils literal notranslate"><span class="pre">PRESET_EXTREME</span></code>. If neither <em>preset</em> nor <em>filters</em> are given, the
  330. default behavior is to use <code class="xref py py-const docutils literal notranslate"><span class="pre">PRESET_DEFAULT</span></code> (preset level <code class="docutils literal notranslate"><span class="pre">6</span></code>).
  331. Higher presets produce smaller output, but make the compression process
  332. slower.</p>
  333. <div class="admonition note">
  334. <p class="admonition-title">Note</p>
  335. <p>In addition to being more CPU-intensive, compression with higher presets
  336. also requires much more memory (and produces output that needs more memory
  337. to decompress). With preset <code class="docutils literal notranslate"><span class="pre">9</span></code> for example, the overhead for an
  338. <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a> object can be as high as 800 MiB. For this reason,
  339. it is generally best to stick with the default preset.</p>
  340. </div>
  341. <p>The <em>filters</em> argument (if provided) should be a filter chain specifier.
  342. See <a class="reference internal" href="#filter-chain-specs"><span class="std std-ref">Specifying custom filter chains</span></a> for details.</p>
  343. <dl class="py method">
  344. <dt class="sig sig-object py" id="lzma.LZMACompressor.compress">
  345. <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="#lzma.LZMACompressor.compress" title="Link to this definition">¶</a></dt>
  346. <dd><p>Compress <em>data</em> (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), returning 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>
  347. object containing compressed data for at least part of the input. Some of
  348. <em>data</em> may be buffered internally, for use in later calls to
  349. <a class="reference internal" href="#lzma.compress" title="lzma.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a> and <a class="reference internal" href="#lzma.LZMACompressor.flush" title="lzma.LZMACompressor.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a>. The returned data should be
  350. concatenated with the output of any previous calls to <a class="reference internal" href="#lzma.compress" title="lzma.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a>.</p>
  351. </dd></dl>
  352. <dl class="py method">
  353. <dt class="sig sig-object py" id="lzma.LZMACompressor.flush">
  354. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMACompressor.flush" title="Link to this definition">¶</a></dt>
  355. <dd><p>Finish the compression process, returning 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
  356. containing any data stored in the compressor’s internal buffers.</p>
  357. <p>The compressor cannot be used after this method has been called.</p>
  358. </dd></dl>
  359. </dd></dl>
  360. <dl class="py class">
  361. <dt class="sig sig-object py" id="lzma.LZMADecompressor">
  362. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">lzma.</span></span><span class="sig-name descname"><span class="pre">LZMADecompressor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">FORMAT_AUTO</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">memlimit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMADecompressor" title="Link to this definition">¶</a></dt>
  363. <dd><p>Create a decompressor object, which can be used to decompress data
  364. incrementally.</p>
  365. <p>For a more convenient way of decompressing an entire compressed stream at
  366. once, see <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a>.</p>
  367. <p>The <em>format</em> argument specifies the container format that should be used. The
  368. default is <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_AUTO</span></code>, which can decompress both <code class="docutils literal notranslate"><span class="pre">.xz</span></code> and
  369. <code class="docutils literal notranslate"><span class="pre">.lzma</span></code> files. Other possible values are <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code>,
  370. <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code>, and <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code>.</p>
  371. <p>The <em>memlimit</em> argument specifies a limit (in bytes) on the amount of memory
  372. that the decompressor can use. When this argument is used, decompression will
  373. fail with an <a class="reference internal" href="#lzma.LZMAError" title="lzma.LZMAError"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAError</span></code></a> if it is not possible to decompress the input
  374. within the given memory limit.</p>
  375. <p>The <em>filters</em> argument specifies the filter chain that was used to create
  376. the stream being decompressed. This argument is required if <em>format</em> is
  377. <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code>, but should not be used for other formats.
  378. See <a class="reference internal" href="#filter-chain-specs"><span class="std std-ref">Specifying custom filter chains</span></a> for more information about filter chains.</p>
  379. <div class="admonition note">
  380. <p class="admonition-title">Note</p>
  381. <p>This class does not transparently handle inputs containing multiple
  382. compressed streams, unlike <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> and <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a>. To
  383. decompress a multi-stream input with <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a>, you must
  384. create a new decompressor for each stream.</p>
  385. </div>
  386. <dl class="py method">
  387. <dt class="sig sig-object py" id="lzma.LZMADecompressor.decompress">
  388. <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">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMADecompressor.decompress" title="Link to this definition">¶</a></dt>
  389. <dd><p>Decompress <em>data</em> (a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>), returning
  390. uncompressed data as bytes. Some of <em>data</em> may be buffered
  391. internally, for use in later calls to <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a>. The
  392. returned data should be concatenated with the output of any
  393. previous calls to <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a>.</p>
  394. <p>If <em>max_length</em> is nonnegative, returns at most <em>max_length</em>
  395. bytes of decompressed data. If this limit is reached and further
  396. output can be produced, the <a class="reference internal" href="#lzma.LZMADecompressor.needs_input" title="lzma.LZMADecompressor.needs_input"><code class="xref py py-attr docutils literal notranslate"><span class="pre">needs_input</span></code></a> attribute will
  397. be set to <code class="docutils literal notranslate"><span class="pre">False</span></code>. In this case, the next call to
  398. <a class="reference internal" href="#lzma.LZMADecompressor.decompress" title="lzma.LZMADecompressor.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> may provide <em>data</em> as <code class="docutils literal notranslate"><span class="pre">b''</span></code> to obtain
  399. more of the output.</p>
  400. <p>If all of the input data was decompressed and returned (either
  401. because this was less than <em>max_length</em> bytes, or because
  402. <em>max_length</em> was negative), the <a class="reference internal" href="#lzma.LZMADecompressor.needs_input" title="lzma.LZMADecompressor.needs_input"><code class="xref py py-attr docutils literal notranslate"><span class="pre">needs_input</span></code></a> attribute
  403. will be set to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  404. <p>Attempting to decompress data after the end of stream is reached
  405. raises an <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a>. Any data found after the end of the
  406. stream is ignored and saved in the <a class="reference internal" href="#lzma.LZMADecompressor.unused_data" title="lzma.LZMADecompressor.unused_data"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unused_data</span></code></a> attribute.</p>
  407. <div class="versionchanged">
  408. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>max_length</em> parameter.</p>
  409. </div>
  410. </dd></dl>
  411. <dl class="py attribute">
  412. <dt class="sig sig-object py" id="lzma.LZMADecompressor.check">
  413. <span class="sig-name descname"><span class="pre">check</span></span><a class="headerlink" href="#lzma.LZMADecompressor.check" title="Link to this definition">¶</a></dt>
  414. <dd><p>The ID of the integrity check used by the input stream. This may be
  415. <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_UNKNOWN</span></code> until enough of the input has been decoded to
  416. determine what integrity check it uses.</p>
  417. </dd></dl>
  418. <dl class="py attribute">
  419. <dt class="sig sig-object py" id="lzma.LZMADecompressor.eof">
  420. <span class="sig-name descname"><span class="pre">eof</span></span><a class="headerlink" href="#lzma.LZMADecompressor.eof" title="Link to this definition">¶</a></dt>
  421. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the end-of-stream marker has been reached.</p>
  422. </dd></dl>
  423. <dl class="py attribute">
  424. <dt class="sig sig-object py" id="lzma.LZMADecompressor.unused_data">
  425. <span class="sig-name descname"><span class="pre">unused_data</span></span><a class="headerlink" href="#lzma.LZMADecompressor.unused_data" title="Link to this definition">¶</a></dt>
  426. <dd><p>Data found after the end of the compressed stream.</p>
  427. <p>Before the end of the stream is reached, this will be <code class="docutils literal notranslate"><span class="pre">b&quot;&quot;</span></code>.</p>
  428. </dd></dl>
  429. <dl class="py attribute">
  430. <dt class="sig sig-object py" id="lzma.LZMADecompressor.needs_input">
  431. <span class="sig-name descname"><span class="pre">needs_input</span></span><a class="headerlink" href="#lzma.LZMADecompressor.needs_input" title="Link to this definition">¶</a></dt>
  432. <dd><p><code class="docutils literal notranslate"><span class="pre">False</span></code> if the <a class="reference internal" href="#lzma.LZMADecompressor.decompress" title="lzma.LZMADecompressor.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method can provide more
  433. decompressed data before requiring new uncompressed input.</p>
  434. <div class="versionadded">
  435. <p><span class="versionmodified added">New in version 3.5.</span></p>
  436. </div>
  437. </dd></dl>
  438. </dd></dl>
  439. <dl class="py function">
  440. <dt class="sig sig-object py" id="lzma.compress">
  441. <span class="sig-prename descclassname"><span class="pre">lzma.</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="n"><span class="pre">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">FORMAT_XZ</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check</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">preset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.compress" title="Link to this definition">¶</a></dt>
  442. <dd><p>Compress <em>data</em> (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), returning the compressed data as a
  443. <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.</p>
  444. <p>See <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a> above for a description of the <em>format</em>, <em>check</em>,
  445. <em>preset</em> and <em>filters</em> arguments.</p>
  446. </dd></dl>
  447. <dl class="py function">
  448. <dt class="sig sig-object py" id="lzma.decompress">
  449. <span class="sig-prename descclassname"><span class="pre">lzma.</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">format</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">FORMAT_AUTO</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">memlimit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.decompress" title="Link to this definition">¶</a></dt>
  450. <dd><p>Decompress <em>data</em> (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), returning the uncompressed data
  451. 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.</p>
  452. <p>If <em>data</em> is the concatenation of multiple distinct compressed streams,
  453. decompress all of these streams, and return the concatenation of the results.</p>
  454. <p>See <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a> above for a description of the <em>format</em>,
  455. <em>memlimit</em> and <em>filters</em> arguments.</p>
  456. </dd></dl>
  457. </section>
  458. <section id="miscellaneous">
  459. <h2>Miscellaneous<a class="headerlink" href="#miscellaneous" title="Link to this heading">¶</a></h2>
  460. <dl class="py function">
  461. <dt class="sig sig-object py" id="lzma.is_check_supported">
  462. <span class="sig-prename descclassname"><span class="pre">lzma.</span></span><span class="sig-name descname"><span class="pre">is_check_supported</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">check</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.is_check_supported" title="Link to this definition">¶</a></dt>
  463. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the given integrity check is supported on this system.</p>
  464. <p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_NONE</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC32</span></code> are always supported.
  465. <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC64</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_SHA256</span></code> may be unavailable if you are
  466. using a version of <strong class="program">liblzma</strong> that was compiled with a limited
  467. feature set.</p>
  468. </dd></dl>
  469. </section>
  470. <section id="specifying-custom-filter-chains">
  471. <span id="filter-chain-specs"></span><h2>Specifying custom filter chains<a class="headerlink" href="#specifying-custom-filter-chains" title="Link to this heading">¶</a></h2>
  472. <p>A filter chain specifier is a sequence of dictionaries, where each dictionary
  473. contains the ID and options for a single filter. Each dictionary must contain
  474. the key <code class="docutils literal notranslate"><span class="pre">&quot;id&quot;</span></code>, and may contain additional keys to specify filter-dependent
  475. options. Valid filter IDs are as follows:</p>
  476. <ul class="simple">
  477. <li><p>Compression filters:</p>
  478. <ul>
  479. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_LZMA1</span></code> (for use with <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code>)</p></li>
  480. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_LZMA2</span></code> (for use with <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code>)</p></li>
  481. </ul>
  482. </li>
  483. <li><p>Delta filter:</p>
  484. <ul>
  485. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_DELTA</span></code></p></li>
  486. </ul>
  487. </li>
  488. <li><p>Branch-Call-Jump (BCJ) filters:</p>
  489. <ul>
  490. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_X86</span></code></p></li>
  491. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_IA64</span></code></p></li>
  492. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_ARM</span></code></p></li>
  493. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_ARMTHUMB</span></code></p></li>
  494. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_POWERPC</span></code></p></li>
  495. <li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_SPARC</span></code></p></li>
  496. </ul>
  497. </li>
  498. </ul>
  499. <p>A filter chain can consist of up to 4 filters, and cannot be empty. The last
  500. filter in the chain must be a compression filter, and any other filters must be
  501. delta or BCJ filters.</p>
  502. <p>Compression filters support the following options (specified as additional
  503. entries in the dictionary representing the filter):</p>
  504. <ul class="simple">
  505. <li><p><code class="docutils literal notranslate"><span class="pre">preset</span></code>: A compression preset to use as a source of default values for
  506. options that are not specified explicitly.</p></li>
  507. <li><p><code class="docutils literal notranslate"><span class="pre">dict_size</span></code>: Dictionary size in bytes. This should be between 4 KiB and
  508. 1.5 GiB (inclusive).</p></li>
  509. <li><p><code class="docutils literal notranslate"><span class="pre">lc</span></code>: Number of literal context bits.</p></li>
  510. <li><p><code class="docutils literal notranslate"><span class="pre">lp</span></code>: Number of literal position bits. The sum <code class="docutils literal notranslate"><span class="pre">lc</span> <span class="pre">+</span> <span class="pre">lp</span></code> must be at
  511. most 4.</p></li>
  512. <li><p><code class="docutils literal notranslate"><span class="pre">pb</span></code>: Number of position bits; must be at most 4.</p></li>
  513. <li><p><code class="docutils literal notranslate"><span class="pre">mode</span></code>: <code class="xref py py-const docutils literal notranslate"><span class="pre">MODE_FAST</span></code> or <code class="xref py py-const docutils literal notranslate"><span class="pre">MODE_NORMAL</span></code>.</p></li>
  514. <li><p><code class="docutils literal notranslate"><span class="pre">nice_len</span></code>: What should be considered a “nice length” for a match.
  515. This should be 273 or less.</p></li>
  516. <li><p><code class="docutils literal notranslate"><span class="pre">mf</span></code>: What match finder to use – <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_HC3</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_HC4</span></code>,
  517. <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_BT2</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_BT3</span></code>, or <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_BT4</span></code>.</p></li>
  518. <li><p><code class="docutils literal notranslate"><span class="pre">depth</span></code>: Maximum search depth used by match finder. 0 (default) means to
  519. select automatically based on other filter options.</p></li>
  520. </ul>
  521. <p>The delta filter stores the differences between bytes, producing more repetitive
  522. input for the compressor in certain circumstances. It supports one option,
  523. <code class="docutils literal notranslate"><span class="pre">dist</span></code>. This indicates the distance between bytes to be subtracted. The
  524. default is 1, i.e. take the differences between adjacent bytes.</p>
  525. <p>The BCJ filters are intended to be applied to machine code. They convert
  526. relative branches, calls and jumps in the code to use absolute addressing, with
  527. the aim of increasing the redundancy that can be exploited by the compressor.
  528. These filters support one option, <code class="docutils literal notranslate"><span class="pre">start_offset</span></code>. This specifies the address
  529. that should be mapped to the beginning of the input data. The default is 0.</p>
  530. </section>
  531. <section id="examples">
  532. <h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  533. <p>Reading in a compressed file:</p>
  534. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
  535. <span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  536. <span class="n">file_content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  537. </pre></div>
  538. </div>
  539. <p>Creating a compressed file:</p>
  540. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
  541. <span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;Insert Data Here&quot;</span>
  542. <span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  543. <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
  544. </pre></div>
  545. </div>
  546. <p>Compressing data in memory:</p>
  547. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
  548. <span class="n">data_in</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;Insert Data Here&quot;</span>
  549. <span class="n">data_out</span> <span class="o">=</span> <span class="n">lzma</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">data_in</span><span class="p">)</span>
  550. </pre></div>
  551. </div>
  552. <p>Incremental compression:</p>
  553. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
  554. <span class="n">lzc</span> <span class="o">=</span> <span class="n">lzma</span><span class="o">.</span><span class="n">LZMACompressor</span><span class="p">()</span>
  555. <span class="n">out1</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Some data</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  556. <span class="n">out2</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Another piece of data</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  557. <span class="n">out3</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Even more data</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  558. <span class="n">out4</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  559. <span class="c1"># Concatenate all the partial results:</span>
  560. <span class="n">result</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">out1</span><span class="p">,</span> <span class="n">out2</span><span class="p">,</span> <span class="n">out3</span><span class="p">,</span> <span class="n">out4</span><span class="p">])</span>
  561. </pre></div>
  562. </div>
  563. <p>Writing compressed data to an already-open file:</p>
  564. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
  565. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  566. <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;This data will not be compressed</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  567. <span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">lzf</span><span class="p">:</span>
  568. <span class="n">lzf</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;This *will* be compressed</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  569. <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Not compressed</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  570. </pre></div>
  571. </div>
  572. <p>Creating a compressed file using a custom filter chain:</p>
  573. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
  574. <span class="n">my_filters</span> <span class="o">=</span> <span class="p">[</span>
  575. <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="n">lzma</span><span class="o">.</span><span class="n">FILTER_DELTA</span><span class="p">,</span> <span class="s2">&quot;dist&quot;</span><span class="p">:</span> <span class="mi">5</span><span class="p">},</span>
  576. <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="n">lzma</span><span class="o">.</span><span class="n">FILTER_LZMA2</span><span class="p">,</span> <span class="s2">&quot;preset&quot;</span><span class="p">:</span> <span class="mi">7</span> <span class="o">|</span> <span class="n">lzma</span><span class="o">.</span><span class="n">PRESET_EXTREME</span><span class="p">},</span>
  577. <span class="p">]</span>
  578. <span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">,</span> <span class="n">filters</span><span class="o">=</span><span class="n">my_filters</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  579. <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;blah blah blah&quot;</span><span class="p">)</span>
  580. </pre></div>
  581. </div>
  582. </section>
  583. </section>
  584. <div class="clearer"></div>
  585. </div>
  586. </div>
  587. </div>
  588. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  589. <div class="sphinxsidebarwrapper">
  590. <div>
  591. <h3><a href="../contents.html">Table of Contents</a></h3>
  592. <ul>
  593. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a><ul>
  594. <li><a class="reference internal" href="#reading-and-writing-compressed-files">Reading and writing compressed files</a></li>
  595. <li><a class="reference internal" href="#compressing-and-decompressing-data-in-memory">Compressing and decompressing data in memory</a></li>
  596. <li><a class="reference internal" href="#miscellaneous">Miscellaneous</a></li>
  597. <li><a class="reference internal" href="#specifying-custom-filter-chains">Specifying custom filter chains</a></li>
  598. <li><a class="reference internal" href="#examples">Examples</a></li>
  599. </ul>
  600. </li>
  601. </ul>
  602. </div>
  603. <div>
  604. <h4>Previous topic</h4>
  605. <p class="topless"><a href="bz2.html"
  606. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> — Support for <strong class="program">bzip2</strong> compression</a></p>
  607. </div>
  608. <div>
  609. <h4>Next topic</h4>
  610. <p class="topless"><a href="zipfile.html"
  611. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> — Work with ZIP archives</a></p>
  612. </div>
  613. <div role="note" aria-label="source link">
  614. <h3>This Page</h3>
  615. <ul class="this-page-menu">
  616. <li><a href="../bugs.html">Report a Bug</a></li>
  617. <li>
  618. <a href="https://github.com/python/cpython/blob/main/Doc/library/lzma.rst"
  619. rel="nofollow">Show Source
  620. </a>
  621. </li>
  622. </ul>
  623. </div>
  624. </div>
  625. <div id="sidebarbutton" title="Collapse sidebar">
  626. <span>«</span>
  627. </div>
  628. </div>
  629. <div class="clearer"></div>
  630. </div>
  631. <div class="related" role="navigation" aria-label="related navigation">
  632. <h3>Navigation</h3>
  633. <ul>
  634. <li class="right" style="margin-right: 10px">
  635. <a href="../genindex.html" title="General Index"
  636. >index</a></li>
  637. <li class="right" >
  638. <a href="../py-modindex.html" title="Python Module Index"
  639. >modules</a> |</li>
  640. <li class="right" >
  641. <a href="zipfile.html" title="zipfile — Work with ZIP archives"
  642. >next</a> |</li>
  643. <li class="right" >
  644. <a href="bz2.html" title="bz2 — Support for bzip2 compression"
  645. >previous</a> |</li>
  646. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  647. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  648. <li class="switchers">
  649. <div class="language_switcher_placeholder"></div>
  650. <div class="version_switcher_placeholder"></div>
  651. </li>
  652. <li>
  653. </li>
  654. <li id="cpython-language-and-version">
  655. <a href="../index.html">3.12.3 Documentation</a> &#187;
  656. </li>
  657. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  658. <li class="nav-item nav-item-2"><a href="archiving.html" >Data Compression and Archiving</a> &#187;</li>
  659. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a></li>
  660. <li class="right">
  661. <div class="inline-search" role="search">
  662. <form class="inline-search" action="../search.html" method="get">
  663. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  664. <input type="submit" value="Go" />
  665. </form>
  666. </div>
  667. |
  668. </li>
  669. <li class="right">
  670. <label class="theme-selector-label">
  671. Theme
  672. <select class="theme-selector" oninput="activateTheme(this.value)">
  673. <option value="auto" selected>Auto</option>
  674. <option value="light">Light</option>
  675. <option value="dark">Dark</option>
  676. </select>
  677. </label> |</li>
  678. </ul>
  679. </div>
  680. <div class="footer">
  681. &copy;
  682. <a href="../copyright.html">
  683. Copyright
  684. </a>
  685. 2001-2024, Python Software Foundation.
  686. <br />
  687. This page is licensed under the Python Software Foundation License Version 2.
  688. <br />
  689. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  690. <br />
  691. See <a href="/license.html">History and License</a> for more information.<br />
  692. <br />
  693. The Python Software Foundation is a non-profit corporation.
  694. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  695. <br />
  696. <br />
  697. Last updated on Apr 09, 2024 (13:47 UTC).
  698. <a href="/bugs.html">Found a bug</a>?
  699. <br />
  700. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  701. </div>
  702. </body>
  703. </html>
上海开阖软件有限公司 沪ICP备12045867号-1