Sophie

Sophie

distrib > Mageia > 3 > i586 > media > core-updates > by-pkgid > 50402eac2a16508b365658612a898528 > files > 741

python3-docs-3.3.0-4.3.mga3.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>13.4. lzma — Compression using the LZMA algorithm &mdash; Python v3.3.0 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.3.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.3.0 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.3.0 documentation" href="../index.html" />
    <link rel="up" title="13. Data Compression and Archiving" href="archiving.html" />
    <link rel="next" title="13.5. zipfile — Work with ZIP archives" href="zipfile.html" />
    <link rel="prev" title="13.3. bz2 — Support for bzip2 compression" href="bz2.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="zipfile.html" title="13.5. zipfile — Work with ZIP archives"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="bz2.html" title="13.3. bz2 — Support for bzip2 compression"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li><a href="../index.html">3.3.0 Documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" accesskey="U">13. Data Compression and Archiving</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-lzma">
<span id="lzma-compression-using-the-lzma-algorithm"></span><h1>13.4. <a class="reference internal" href="#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><tt class="xref py py-mod docutils literal"><span class="pre">lzma</span></tt></a> &#8212; Compression using the LZMA algorithm<a class="headerlink" href="#module-lzma" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 3.3.</span> </p>
<p>This module provides classes and convenience functions for compressing and
decompressing data using the LZMA compression algorithm. Also included is a file
interface supporting the <tt class="docutils literal"><span class="pre">.xz</span></tt> and legacy <tt class="docutils literal"><span class="pre">.lzma</span></tt> file formats used by the
<strong class="program">xz</strong> utility, as well as raw compressed streams.</p>
<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."><tt class="xref py py-mod docutils literal"><span class="pre">bz2</span></tt></a>
module. However, note that <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a> is <em>not</em> thread-safe, unlike
<a class="reference internal" href="bz2.html#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">bz2.BZ2File</span></tt></a>, so if you need to use a single <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a> instance
from multiple threads, it is necessary to protect it with a lock.</p>
<dl class="exception">
<dt id="lzma.LZMAError">
<em class="property">exception </em><tt class="descclassname">lzma.</tt><tt class="descname">LZMAError</tt><a class="headerlink" href="#lzma.LZMAError" title="Permalink to this definition">¶</a></dt>
<dd><p>This exception is raised when an error occurs during compression or
decompression, or while initializing the compressor/decompressor state.</p>
</dd></dl>

<div class="section" id="reading-and-writing-compressed-files">
<h2>13.4.1. Reading and writing compressed files<a class="headerlink" href="#reading-and-writing-compressed-files" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="lzma.open">
<tt class="descclassname">lzma.</tt><tt class="descname">open</tt><big>(</big><em>filename</em>, <em>mode=&quot;rb&quot;</em>, <em>*</em>, <em>format=None</em>, <em>check=-1</em>, <em>preset=None</em>, <em>filters=None</em>, <em>encoding=None</em>, <em>errors=None</em>, <em>newline=None</em><big>)</big><a class="headerlink" href="#lzma.open" title="Permalink to this definition">¶</a></dt>
<dd><p>Open an LZMA-compressed file in binary or text mode, returning a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file
object</em></a>.</p>
<p>The <em>filename</em> argument can be either an actual file name (given as a
<a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> or <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object), in which case the named file is
opened, or it can be an existing file object to read from or write to.</p>
<p>The <em>mode</em> argument can be any of <tt class="docutils literal"><span class="pre">&quot;r&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;rb&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;w&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;wb&quot;</span></tt>,
<tt class="docutils literal"><span class="pre">&quot;a&quot;</span></tt> or <tt class="docutils literal"><span class="pre">&quot;ab&quot;</span></tt> for binary mode, or <tt class="docutils literal"><span class="pre">&quot;rt&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;wt&quot;</span></tt>, or <tt class="docutils literal"><span class="pre">&quot;at&quot;</span></tt> for
text mode. The default is <tt class="docutils literal"><span class="pre">&quot;rb&quot;</span></tt>.</p>
<p>When opening a file for reading, the <em>format</em> and <em>filters</em> arguments have
the same meanings as for <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMADecompressor</span></tt></a>. In this case, the <em>check</em>
and <em>preset</em> arguments should not be used.</p>
<p>When opening a file for writing, the <em>format</em>, <em>check</em>, <em>preset</em> and
<em>filters</em> arguments have the same meanings as for <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMACompressor</span></tt></a>.</p>
<p>For binary mode, this function is equivalent to the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a>
constructor: <tt class="docutils literal"><span class="pre">LZMAFile(filename,</span> <span class="pre">mode,</span> <span class="pre">...)</span></tt>. In this case, the <em>encoding</em>,
<em>errors</em> and <em>newline</em> arguments must not be provided.</p>
<p>For text mode, a <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a> object is created, and wrapped in an
<a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><tt class="xref py py-class docutils literal"><span class="pre">io.TextIOWrapper</span></tt></a> instance with the specified encoding, error
handling behavior, and line ending(s).</p>
</dd></dl>

<dl class="class">
<dt id="lzma.LZMAFile">
<em class="property">class </em><tt class="descclassname">lzma.</tt><tt class="descname">LZMAFile</tt><big>(</big><em>filename=None</em>, <em>mode=&quot;r&quot;</em>, <em>*</em>, <em>format=None</em>, <em>check=-1</em>, <em>preset=None</em>, <em>filters=None</em><big>)</big><a class="headerlink" href="#lzma.LZMAFile" title="Permalink to this definition">¶</a></dt>
<dd><p>Open an LZMA-compressed file in binary mode.</p>
<p>An <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a> can wrap an already-open <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>, or operate
directly on a named file. The <em>filename</em> argument specifies either the file
object to wrap, or the name of the file to open (as a <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> or
<a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object). When wrapping an existing file object, the wrapped
file will not be closed when the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a> is closed.</p>
<p>The <em>mode</em> argument can be either <tt class="docutils literal"><span class="pre">&quot;r&quot;</span></tt> for reading (default), <tt class="docutils literal"><span class="pre">&quot;w&quot;</span></tt> for
overwriting, or <tt class="docutils literal"><span class="pre">&quot;a&quot;</span></tt> for appending. These can equivalently be given as
<tt class="docutils literal"><span class="pre">&quot;rb&quot;</span></tt>, <tt class="docutils literal"><span class="pre">&quot;wb&quot;</span></tt>, and <tt class="docutils literal"><span class="pre">&quot;ab&quot;</span></tt> respectively.</p>
<p>If <em>filename</em> is a file object (rather than an actual file name), a mode of
<tt class="docutils literal"><span class="pre">&quot;w&quot;</span></tt> does not truncate the file, and is instead equivalent to <tt class="docutils literal"><span class="pre">&quot;a&quot;</span></tt>.</p>
<p>When opening a file for reading, the input file may be the concatenation of
multiple separate compressed streams. These are transparently decoded as a
single logical stream.</p>
<p>When opening a file for reading, the <em>format</em> and <em>filters</em> arguments have
the same meanings as for <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMADecompressor</span></tt></a>. In this case, the <em>check</em>
and <em>preset</em> arguments should not be used.</p>
<p>When opening a file for writing, the <em>format</em>, <em>check</em>, <em>preset</em> and
<em>filters</em> arguments have the same meanings as for <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMACompressor</span></tt></a>.</p>
<p><a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a> supports all the members specified by
<a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedIOBase</span></tt></a>, except for <tt class="xref py py-meth docutils literal"><span class="pre">detach()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">truncate()</span></tt>.
Iteration and the <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement are supported.</p>
<p>The following method is also provided:</p>
<dl class="method">
<dt id="lzma.LZMAFile.peek">
<tt class="descname">peek</tt><big>(</big><em>size=-1</em><big>)</big><a class="headerlink" href="#lzma.LZMAFile.peek" title="Permalink to this definition">¶</a></dt>
<dd><p>Return buffered data without advancing the file position. At least one
byte of data will be returned, unless EOF has been reached. The exact
number of bytes returned is unspecified (the <em>size</em> argument is ignored).</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="compressing-and-decompressing-data-in-memory">
<h2>13.4.2. Compressing and decompressing data in memory<a class="headerlink" href="#compressing-and-decompressing-data-in-memory" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="lzma.LZMACompressor">
<em class="property">class </em><tt class="descclassname">lzma.</tt><tt class="descname">LZMACompressor</tt><big>(</big><em>format=FORMAT_XZ</em>, <em>check=-1</em>, <em>preset=None</em>, <em>filters=None</em><big>)</big><a class="headerlink" href="#lzma.LZMACompressor" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a compressor object, which can be used to compress data incrementally.</p>
<p>For a more convenient way of compressing a single chunk of data, see
<a class="reference internal" href="#lzma.compress" title="lzma.compress"><tt class="xref py py-func docutils literal"><span class="pre">compress()</span></tt></a>.</p>
<p>The <em>format</em> argument specifies what container format should be used.
Possible values are:</p>
<ul>
<li><dl class="first docutils">
<dt><tt class="xref py py-const docutils literal"><span class="pre">FORMAT_XZ</span></tt>: The <tt class="docutils literal"><span class="pre">.xz</span></tt> container format.</dt>
<dd><p class="first last">This is the default format.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="xref py py-const docutils literal"><span class="pre">FORMAT_ALONE</span></tt>: The legacy <tt class="docutils literal"><span class="pre">.lzma</span></tt> container format.</dt>
<dd><p class="first last">This format is more limited than <tt class="docutils literal"><span class="pre">.xz</span></tt> &#8211; it does not support integrity
checks or multiple filters.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><tt class="xref py py-const docutils literal"><span class="pre">FORMAT_RAW</span></tt>: A raw data stream, not using any container format.</dt>
<dd><p class="first last">This format specifier does not support integrity checks, and requires that
you always specify a custom filter chain (for both compression and
decompression). Additionally, data compressed in this manner cannot be
decompressed using <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_AUTO</span></tt> (see <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMADecompressor</span></tt></a>).</p>
</dd>
</dl>
</li>
</ul>
<p>The <em>check</em> argument specifies the type of integrity check to include in the
compressed data. This check is used when decompressing, to ensure that the
data has not been corrupted. Possible values are:</p>
<ul class="simple">
<li><tt class="xref py py-const docutils literal"><span class="pre">CHECK_NONE</span></tt>: No integrity check.
This is the default (and the only acceptable value) for
<tt class="xref py py-const docutils literal"><span class="pre">FORMAT_ALONE</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_RAW</span></tt>.</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">CHECK_CRC32</span></tt>: 32-bit Cyclic Redundancy Check.</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">CHECK_CRC64</span></tt>: 64-bit Cyclic Redundancy Check.
This is the default for <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_XZ</span></tt>.</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">CHECK_SHA256</span></tt>: 256-bit Secure Hash Algorithm.</li>
</ul>
<p>If the specified check is not supported, an <a class="reference internal" href="#lzma.LZMAError" title="lzma.LZMAError"><tt class="xref py py-class docutils literal"><span class="pre">LZMAError</span></tt></a> is raised.</p>
<p>The compression settings can be specified either as a preset compression
level (with the <em>preset</em> argument), or in detail as a custom filter chain
(with the <em>filters</em> argument).</p>
<p>The <em>preset</em> argument (if provided) should be an integer between <tt class="docutils literal"><span class="pre">0</span></tt> and
<tt class="docutils literal"><span class="pre">9</span></tt> (inclusive), optionally OR-ed with the constant
<tt class="xref py py-const docutils literal"><span class="pre">PRESET_EXTREME</span></tt>. If neither <em>preset</em> nor <em>filters</em> are given, the
default behavior is to use <tt class="xref py py-const docutils literal"><span class="pre">PRESET_DEFAULT</span></tt> (preset level <tt class="docutils literal"><span class="pre">6</span></tt>).
Higher presets produce smaller output, but make the compression process
slower.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In addition to being more CPU-intensive, compression with higher presets
also requires much more memory (and produces output that needs more memory
to decompress). With preset <tt class="docutils literal"><span class="pre">9</span></tt> for example, the overhead for an
<a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMACompressor</span></tt></a> object can be as high as 800MiB. For this reason,
it is generally best to stick with the default preset.</p>
</div>
<p>The <em>filters</em> argument (if provided) should be a filter chain specifier.
See <a class="reference internal" href="#filter-chain-specs"><em>Specifying custom filter chains</em></a> for details.</p>
<dl class="method">
<dt id="lzma.LZMACompressor.compress">
<tt class="descname">compress</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#lzma.LZMACompressor.compress" title="Permalink to this definition">¶</a></dt>
<dd><p>Compress <em>data</em> (a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object), returning a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>
object containing compressed data for at least part of the input. Some of
<em>data</em> may be buffered internally, for use in later calls to
<a class="reference internal" href="#lzma.compress" title="lzma.compress"><tt class="xref py py-meth docutils literal"><span class="pre">compress()</span></tt></a> and <a class="reference internal" href="#lzma.LZMACompressor.flush" title="lzma.LZMACompressor.flush"><tt class="xref py py-meth docutils literal"><span class="pre">flush()</span></tt></a>. The returned data should be
concatenated with the output of any previous calls to <a class="reference internal" href="#lzma.compress" title="lzma.compress"><tt class="xref py py-meth docutils literal"><span class="pre">compress()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="lzma.LZMACompressor.flush">
<tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#lzma.LZMACompressor.flush" title="Permalink to this definition">¶</a></dt>
<dd><p>Finish the compression process, returning a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object
containing any data stored in the compressor&#8217;s internal buffers.</p>
<p>The compressor cannot be used after this method has been called.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="lzma.LZMADecompressor">
<em class="property">class </em><tt class="descclassname">lzma.</tt><tt class="descname">LZMADecompressor</tt><big>(</big><em>format=FORMAT_AUTO</em>, <em>memlimit=None</em>, <em>filters=None</em><big>)</big><a class="headerlink" href="#lzma.LZMADecompressor" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a decompressor object, which can be used to decompress data
incrementally.</p>
<p>For a more convenient way of decompressing an entire compressed stream at
once, see <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><tt class="xref py py-func docutils literal"><span class="pre">decompress()</span></tt></a>.</p>
<p>The <em>format</em> argument specifies the container format that should be used. The
default is <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_AUTO</span></tt>, which can decompress both <tt class="docutils literal"><span class="pre">.xz</span></tt> and
<tt class="docutils literal"><span class="pre">.lzma</span></tt> files. Other possible values are <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_XZ</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">FORMAT_ALONE</span></tt>, and <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_RAW</span></tt>.</p>
<p>The <em>memlimit</em> argument specifies a limit (in bytes) on the amount of memory
that the decompressor can use. When this argument is used, decompression will
fail with an <a class="reference internal" href="#lzma.LZMAError" title="lzma.LZMAError"><tt class="xref py py-class docutils literal"><span class="pre">LZMAError</span></tt></a> if it is not possible to decompress the input
within the given memory limit.</p>
<p>The <em>filters</em> argument specifies the filter chain that was used to create
the stream being decompressed. This argument is required if <em>format</em> is
<tt class="xref py py-const docutils literal"><span class="pre">FORMAT_RAW</span></tt>, but should not be used for other formats.
See <a class="reference internal" href="#filter-chain-specs"><em>Specifying custom filter chains</em></a> for more information about filter chains.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This class does not transparently handle inputs containing multiple
compressed streams, unlike <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><tt class="xref py py-func docutils literal"><span class="pre">decompress()</span></tt></a> and <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><tt class="xref py py-class docutils literal"><span class="pre">LZMAFile</span></tt></a>. To
decompress a multi-stream input with <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMADecompressor</span></tt></a>, you must
create a new decompressor for each stream.</p>
</div>
<dl class="method">
<dt id="lzma.LZMADecompressor.decompress">
<tt class="descname">decompress</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#lzma.LZMADecompressor.decompress" title="Permalink to this definition">¶</a></dt>
<dd><p>Decompress <em>data</em> (a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object), returning a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>
object containing the decompressed data for at least part of the input.
Some of <em>data</em> may be buffered internally, for use in later calls to
<a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a>. The returned data should be concatenated with the
output of any previous calls to <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><tt class="xref py py-meth docutils literal"><span class="pre">decompress()</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="lzma.LZMADecompressor.check">
<tt class="descname">check</tt><a class="headerlink" href="#lzma.LZMADecompressor.check" title="Permalink to this definition">¶</a></dt>
<dd><p>The ID of the integrity check used by the input stream. This may be
<tt class="xref py py-const docutils literal"><span class="pre">CHECK_UNKNOWN</span></tt> until enough of the input has been decoded to
determine what integrity check it uses.</p>
</dd></dl>

<dl class="attribute">
<dt id="lzma.LZMADecompressor.eof">
<tt class="descname">eof</tt><a class="headerlink" href="#lzma.LZMADecompressor.eof" title="Permalink to this definition">¶</a></dt>
<dd><p>True if the end-of-stream marker has been reached.</p>
</dd></dl>

<dl class="attribute">
<dt id="lzma.LZMADecompressor.unused_data">
<tt class="descname">unused_data</tt><a class="headerlink" href="#lzma.LZMADecompressor.unused_data" title="Permalink to this definition">¶</a></dt>
<dd><p>Data found after the end of the compressed stream.</p>
<p>Before the end of the stream is reached, this will be <tt class="docutils literal"><span class="pre">b&quot;&quot;</span></tt>.</p>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="lzma.compress">
<tt class="descclassname">lzma.</tt><tt class="descname">compress</tt><big>(</big><em>data</em>, <em>format=FORMAT_XZ</em>, <em>check=-1</em>, <em>preset=None</em>, <em>filters=None</em><big>)</big><a class="headerlink" href="#lzma.compress" title="Permalink to this definition">¶</a></dt>
<dd><p>Compress <em>data</em> (a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object), returning the compressed data as a
<a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object.</p>
<p>See <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMACompressor</span></tt></a> above for a description of the <em>format</em>, <em>check</em>,
<em>preset</em> and <em>filters</em> arguments.</p>
</dd></dl>

<dl class="function">
<dt id="lzma.decompress">
<tt class="descclassname">lzma.</tt><tt class="descname">decompress</tt><big>(</big><em>data</em>, <em>format=FORMAT_AUTO</em>, <em>memlimit=None</em>, <em>filters=None</em><big>)</big><a class="headerlink" href="#lzma.decompress" title="Permalink to this definition">¶</a></dt>
<dd><p>Decompress <em>data</em> (a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object), returning the uncompressed data
as a <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object.</p>
<p>If <em>data</em> is the concatenation of multiple distinct compressed streams,
decompress all of these streams, and return the concatenation of the results.</p>
<p>See <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><tt class="xref py py-class docutils literal"><span class="pre">LZMADecompressor</span></tt></a> above for a description of the <em>format</em>,
<em>memlimit</em> and <em>filters</em> arguments.</p>
</dd></dl>

</div>
<div class="section" id="miscellaneous">
<h2>13.4.3. Miscellaneous<a class="headerlink" href="#miscellaneous" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="lzma.is_check_supported">
<tt class="descclassname">lzma.</tt><tt class="descname">is_check_supported</tt><big>(</big><em>check</em><big>)</big><a class="headerlink" href="#lzma.is_check_supported" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns true if the given integrity check is supported on this system.</p>
<p><tt class="xref py py-const docutils literal"><span class="pre">CHECK_NONE</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">CHECK_CRC32</span></tt> are always supported.
<tt class="xref py py-const docutils literal"><span class="pre">CHECK_CRC64</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">CHECK_SHA256</span></tt> may be unavailable if you are
using a version of <strong class="program">liblzma</strong> that was compiled with a limited
feature set.</p>
</dd></dl>

</div>
<div class="section" id="specifying-custom-filter-chains">
<span id="filter-chain-specs"></span><h2>13.4.4. Specifying custom filter chains<a class="headerlink" href="#specifying-custom-filter-chains" title="Permalink to this headline">¶</a></h2>
<p>A filter chain specifier is a sequence of dictionaries, where each dictionary
contains the ID and options for a single filter. Each dictionary must contain
the key <tt class="docutils literal"><span class="pre">&quot;id&quot;</span></tt>, and may contain additional keys to specify filter-dependent
options. Valid filter IDs are as follows:</p>
<ul>
<li><dl class="first docutils">
<dt>Compression filters:</dt>
<dd><ul class="first last simple">
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_LZMA1</span></tt> (for use with <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_ALONE</span></tt>)</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_LZMA2</span></tt> (for use with <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_XZ</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">FORMAT_RAW</span></tt>)</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Delta filter:</dt>
<dd><ul class="first last simple">
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_DELTA</span></tt></li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>Branch-Call-Jump (BCJ) filters:</dt>
<dd><ul class="first last simple">
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_X86</span></tt></li>
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_IA64</span></tt></li>
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_ARM</span></tt></li>
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_ARMTHUMB</span></tt></li>
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_POWERPC</span></tt></li>
<li><tt class="xref py py-const docutils literal"><span class="pre">FILTER_SPARC</span></tt></li>
</ul>
</dd>
</dl>
</li>
</ul>
<p>A filter chain can consist of up to 4 filters, and cannot be empty. The last
filter in the chain must be a compression filter, and any other filters must be
delta or BCJ filters.</p>
<p>Compression filters support the following options (specified as additional
entries in the dictionary representing the filter):</p>
<blockquote>
<div><ul class="simple">
<li><tt class="docutils literal"><span class="pre">preset</span></tt>: A compression preset to use as a source of default values for
options that are not specified explicitly.</li>
<li><tt class="docutils literal"><span class="pre">dict_size</span></tt>: Dictionary size in bytes. This should be between 4KiB and
1.5GiB (inclusive).</li>
<li><tt class="docutils literal"><span class="pre">lc</span></tt>: Number of literal context bits.</li>
<li><tt class="docutils literal"><span class="pre">lp</span></tt>: Number of literal position bits. The sum <tt class="docutils literal"><span class="pre">lc</span> <span class="pre">+</span> <span class="pre">lp</span></tt> must be at
most 4.</li>
<li><tt class="docutils literal"><span class="pre">pb</span></tt>: Number of position bits; must be at most 4.</li>
<li><tt class="docutils literal"><span class="pre">mode</span></tt>: <tt class="xref py py-const docutils literal"><span class="pre">MODE_FAST</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">MODE_NORMAL</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">nice_len</span></tt>: What should be considered a &#8220;nice length&#8221; for a match.
This should be 273 or less.</li>
<li><tt class="docutils literal"><span class="pre">mf</span></tt>: What match finder to use &#8211; <tt class="xref py py-const docutils literal"><span class="pre">MF_HC3</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">MF_HC4</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">MF_BT2</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">MF_BT3</span></tt>, or <tt class="xref py py-const docutils literal"><span class="pre">MF_BT4</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">depth</span></tt>: Maximum search depth used by match finder. 0 (default) means to
select automatically based on other filter options.</li>
</ul>
</div></blockquote>
<p>The delta filter stores the differences between bytes, producing more repetitive
input for the compressor in certain circumstances. It only supports a single
The delta filter supports only one option, <tt class="docutils literal"><span class="pre">dist</span></tt>. This indicates the distance
between bytes to be subtracted. The default is 1, i.e. take the differences
between adjacent bytes.</p>
<p>The BCJ filters are intended to be applied to machine code. They convert
relative branches, calls and jumps in the code to use absolute addressing, with
the aim of increasing the redundancy that can be exploited by the compressor.
These filters support one option, <tt class="docutils literal"><span class="pre">start_offset</span></tt>. This specifies the address
that should be mapped to the beginning of the input data. The default is 0.</p>
</div>
<div class="section" id="examples">
<h2>13.4.5. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>Reading in a compressed file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">lzma</span>
<span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">LZMAFile</span><span class="p">(</span><span class="s">&quot;file.xz&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
   <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>
</pre></div>
</div>
<p>Creating a compressed file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">lzma</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">b</span><span class="s">&quot;Insert Data Here&quot;</span>
<span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">LZMAFile</span><span class="p">(</span><span class="s">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
   <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>
</pre></div>
</div>
<p>Compressing data in memory:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">lzma</span>
<span class="n">data_in</span> <span class="o">=</span> <span class="n">b</span><span class="s">&quot;Insert Data Here&quot;</span>
<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>
</pre></div>
</div>
<p>Incremental compression:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">lzma</span>
<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>
<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="n">b</span><span class="s">&quot;Some data</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<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="n">b</span><span class="s">&quot;Another piece of data</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<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="n">b</span><span class="s">&quot;Even more data</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<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>
<span class="c"># Concatenate all the partial results:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">b</span><span class="s">&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>
</pre></div>
</div>
<p>Writing compressed data to an already-open file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">lzma</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="s">&quot;This data will not be compressed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
    <span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">LZMAFile</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">lzf</span><span class="p">:</span>
        <span class="n">lzf</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="s">&quot;This *will* be compressed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="s">&quot;Not compressed</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Creating a compressed file using a custom filter chain:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">lzma</span>
<span class="n">my_filters</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s">&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="s">&quot;dist&quot;</span><span class="p">:</span> <span class="mi">5</span><span class="p">},</span>
    <span class="p">{</span><span class="s">&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="s">&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>
<span class="p">]</span>
<span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">LZMAFile</span><span class="p">(</span><span class="s">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s">&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>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="s">&quot;blah blah blah&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">13.4. <tt class="docutils literal"><span class="pre">lzma</span></tt> &#8212; Compression using the LZMA algorithm</a><ul>
<li><a class="reference internal" href="#reading-and-writing-compressed-files">13.4.1. Reading and writing compressed files</a></li>
<li><a class="reference internal" href="#compressing-and-decompressing-data-in-memory">13.4.2. Compressing and decompressing data in memory</a></li>
<li><a class="reference internal" href="#miscellaneous">13.4.3. Miscellaneous</a></li>
<li><a class="reference internal" href="#specifying-custom-filter-chains">13.4.4. Specifying custom filter chains</a></li>
<li><a class="reference internal" href="#examples">13.4.5. Examples</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="bz2.html"
                        title="previous chapter">13.3. <tt class="docutils literal docutils literal docutils literal"><span class="pre">bz2</span></tt> &#8212; Support for <strong class="program">bzip2</strong> compression</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="zipfile.html"
                        title="next chapter">13.5. <tt class="docutils literal"><span class="pre">zipfile</span></tt> &#8212; Work with ZIP archives</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/lzma.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="zipfile.html" title="13.5. zipfile — Work with ZIP archives"
             >next</a> |</li>
        <li class="right" >
          <a href="bz2.html" title="13.3. bz2 — Support for bzip2 compression"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li><a href="../index.html">3.3.0 Documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" >13. Data Compression and Archiving</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2012, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 29, 2012.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>