1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
<span id="lzma-compression-using-the-lzma-algorithm"></span><h1>lzma — Compression using the LZMA algorithm</h1> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <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> <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 <code>.xz</code> and legacy <code>.lzma</code> 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#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code>bz2</code></a> module. Note that <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a> and <a class="reference internal" href="bz2#bz2.BZ2File" title="bz2.BZ2File"><code>bz2.BZ2File</code></a> are <em>not</em> thread-safe, so if you need to use a single <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a> instance from multiple threads, it is necessary to protect it with a lock.</p> <dl class="py exception"> <dt class="sig sig-object py" id="lzma.LZMAError">
<code>exception lzma.LZMAError</code> </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> <section id="reading-and-writing-compressed-files"> <h2>Reading and writing compressed files</h2> <dl class="py function"> <dt class="sig sig-object py" id="lzma.open">
<code>lzma.open(filename, mode='rb', *, format=None, check=- 1, preset=None, filters=None, encoding=None, errors=None, newline=None)</code> </dt> <dd>
<p>Open an LZMA-compressed file in binary or text mode, returning a <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file object</span></a>.</p> <p>The <em>filename</em> argument can be either an actual file name (given as a <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a>, <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> or <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like</span></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 <code>"r"</code>, <code>"rb"</code>, <code>"w"</code>, <code>"wb"</code>, <code>"x"</code>, <code>"xb"</code>, <code>"a"</code> or <code>"ab"</code> for binary mode, or <code>"rt"</code>, <code>"wt"</code>, <code>"xt"</code>, or <code>"at"</code> for text mode. The default is <code>"rb"</code>.</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"><code>LZMADecompressor</code></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"><code>LZMACompressor</code></a>.</p> <p>For binary mode, this function is equivalent to the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a> constructor: <code>LZMAFile(filename, mode, ...)</code>. 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"><code>LZMAFile</code></a> object is created, and wrapped in an <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> instance with the specified encoding, error handling behavior, and line ending(s).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for the <code>"x"</code>, <code>"xb"</code> and <code>"xt"</code> modes.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="lzma.LZMAFile">
<code>class lzma.LZMAFile(filename=None, mode='r', *, format=None, check=- 1, preset=None, filters=None)</code> </dt> <dd>
<p>Open an LZMA-compressed file in binary mode.</p> <p>An <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a> can wrap an already-open <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file object</span></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="stdtypes#str" title="str"><code>str</code></a>, <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> or <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like</span></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"><code>LZMAFile</code></a> is closed.</p> <p>The <em>mode</em> argument can be either <code>"r"</code> for reading (default), <code>"w"</code> for overwriting, <code>"x"</code> for exclusive creation, or <code>"a"</code> for appending. These can equivalently be given as <code>"rb"</code>, <code>"wb"</code>, <code>"xb"</code> and <code>"ab"</code> respectively.</p> <p>If <em>filename</em> is a file object (rather than an actual file name), a mode of <code>"w"</code> does not truncate the file, and is instead equivalent to <code>"a"</code>.</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"><code>LZMADecompressor</code></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"><code>LZMACompressor</code></a>.</p> <p><a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a> supports all the members specified by <a class="reference internal" href="io#io.BufferedIOBase" title="io.BufferedIOBase"><code>io.BufferedIOBase</code></a>, except for <a class="reference internal" href="io#io.BufferedIOBase.detach" title="io.BufferedIOBase.detach"><code>detach()</code></a> and <a class="reference internal" href="io#io.IOBase.truncate" title="io.IOBase.truncate"><code>truncate()</code></a>. Iteration and the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement are supported.</p> <p>The following method is also provided:</p> <dl class="py method"> <dt class="sig sig-object py" id="lzma.LZMAFile.peek">
<code>peek(size=- 1)</code> </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> <div class="admonition note"> <p class="admonition-title">Note</p> <p>While calling <a class="reference internal" href="#lzma.LZMAFile.peek" title="lzma.LZMAFile.peek"><code>peek()</code></a> does not change the file position of the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a>, it may change the position of the underlying file object (e.g. if the <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a> was constructed by passing a file object for <em>filename</em>).</p> </div> </dd>
</dl> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for the <code>"x"</code> and <code>"xb"</code> modes.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>The <a class="reference internal" href="io#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code>read()</code></a> method now accepts an argument of <code>None</code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> </section> <section id="compressing-and-decompressing-data-in-memory"> <h2>Compressing and decompressing data in memory</h2> <dl class="py class"> <dt class="sig sig-object py" id="lzma.LZMACompressor">
<code>class lzma.LZMACompressor(format=FORMAT_XZ, check=- 1, preset=None, filters=None)</code> </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"><code>compress()</code></a>.</p> <p>The <em>format</em> argument specifies what container format should be used. Possible values are:</p> <ul class="simple"> <li>
<dl class="simple"> <dt>
<code>FORMAT_XZ: The .xz container format.</code> </dt>
<dd>
<p>This is the default format.</p> </dd> </dl> </li> <li>
<dl class="simple"> <dt>
<code>FORMAT_ALONE: The legacy .lzma container format.</code> </dt>
<dd>
<p>This format is more limited than <code>.xz</code> – it does not support integrity checks or multiple filters.</p> </dd> </dl> </li> <li>
<dl class="simple"> <dt>
<code>FORMAT_RAW: A raw data stream, not using any container format.</code> </dt>
<dd>
<p>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 <code>FORMAT_AUTO</code> (see <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code>LZMADecompressor</code></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>
<code>CHECK_NONE</code>: No integrity check. This is the default (and the only acceptable value) for <code>FORMAT_ALONE</code> and <code>FORMAT_RAW</code>.</li> <li>
<code>CHECK_CRC32</code>: 32-bit Cyclic Redundancy Check.</li> <li>
<code>CHECK_CRC64</code>: 64-bit Cyclic Redundancy Check. This is the default for <code>FORMAT_XZ</code>.</li> <li>
<code>CHECK_SHA256</code>: 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"><code>LZMAError</code></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 <code>0</code> and <code>9</code> (inclusive), optionally OR-ed with the constant <code>PRESET_EXTREME</code>. If neither <em>preset</em> nor <em>filters</em> are given, the default behavior is to use <code>PRESET_DEFAULT</code> (preset level <code>6</code>). Higher presets produce smaller output, but make the compression process slower.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>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 <code>9</code> for example, the overhead for an <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code>LZMACompressor</code></a> object can be as high as 800 MiB. 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"><span class="std std-ref">Specifying custom filter chains</span></a> for details.</p> <dl class="py method"> <dt class="sig sig-object py" id="lzma.LZMACompressor.compress">
<code>compress(data)</code> </dt> <dd>
<p>Compress <em>data</em> (a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object), returning a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></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"><code>compress()</code></a> and <a class="reference internal" href="#lzma.LZMACompressor.flush" title="lzma.LZMACompressor.flush"><code>flush()</code></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"><code>compress()</code></a>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="lzma.LZMACompressor.flush">
<code>flush()</code> </dt> <dd>
<p>Finish the compression process, returning a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object containing any data stored in the compressor’s internal buffers.</p> <p>The compressor cannot be used after this method has been called.</p> </dd>
</dl> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="lzma.LZMADecompressor">
<code>class lzma.LZMADecompressor(format=FORMAT_AUTO, memlimit=None, filters=None)</code> </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"><code>decompress()</code></a>.</p> <p>The <em>format</em> argument specifies the container format that should be used. The default is <code>FORMAT_AUTO</code>, which can decompress both <code>.xz</code> and <code>.lzma</code> files. Other possible values are <code>FORMAT_XZ</code>, <code>FORMAT_ALONE</code>, and <code>FORMAT_RAW</code>.</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"><code>LZMAError</code></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 <code>FORMAT_RAW</code>, but should not be used for other formats. 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> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This class does not transparently handle inputs containing multiple compressed streams, unlike <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code>decompress()</code></a> and <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code>LZMAFile</code></a>. To decompress a multi-stream input with <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code>LZMADecompressor</code></a>, you must create a new decompressor for each stream.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="lzma.LZMADecompressor.decompress">
<code>decompress(data, max_length=- 1)</code> </dt> <dd>
<p>Decompress <em>data</em> (a <a class="reference internal" href="../glossary#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>), returning uncompressed data as bytes. 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"><code>decompress()</code></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"><code>decompress()</code></a>.</p> <p>If <em>max_length</em> is nonnegative, returns at most <em>max_length</em> bytes of decompressed data. If this limit is reached and further output can be produced, the <a class="reference internal" href="#lzma.LZMADecompressor.needs_input" title="lzma.LZMADecompressor.needs_input"><code>needs_input</code></a> attribute will be set to <code>False</code>. In this case, the next call to <a class="reference internal" href="#lzma.LZMADecompressor.decompress" title="lzma.LZMADecompressor.decompress"><code>decompress()</code></a> may provide <em>data</em> as <code>b''</code> to obtain more of the output.</p> <p>If all of the input data was decompressed and returned (either because this was less than <em>max_length</em> bytes, or because <em>max_length</em> was negative), the <a class="reference internal" href="#lzma.LZMADecompressor.needs_input" title="lzma.LZMADecompressor.needs_input"><code>needs_input</code></a> attribute will be set to <code>True</code>.</p> <p>Attempting to decompress data after the end of stream is reached raises an <a class="reference internal" href="exceptions#EOFError" title="EOFError"><code>EOFError</code></a>. Any data found after the end of the stream is ignored and saved in the <a class="reference internal" href="#lzma.LZMADecompressor.unused_data" title="lzma.LZMADecompressor.unused_data"><code>unused_data</code></a> attribute.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>max_length</em> parameter.</p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="lzma.LZMADecompressor.check">
<code>check</code> </dt> <dd>
<p>The ID of the integrity check used by the input stream. This may be <code>CHECK_UNKNOWN</code> until enough of the input has been decoded to determine what integrity check it uses.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="lzma.LZMADecompressor.eof">
<code>eof</code> </dt> <dd>
<p><code>True</code> if the end-of-stream marker has been reached.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="lzma.LZMADecompressor.unused_data">
<code>unused_data</code> </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 <code>b""</code>.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="lzma.LZMADecompressor.needs_input">
<code>needs_input</code> </dt> <dd>
<p><code>False</code> if the <a class="reference internal" href="#lzma.LZMADecompressor.decompress" title="lzma.LZMADecompressor.decompress"><code>decompress()</code></a> method can provide more decompressed data before requiring new uncompressed input.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd>
</dl> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="lzma.compress">
<code>lzma.compress(data, format=FORMAT_XZ, check=- 1, preset=None, filters=None)</code> </dt> <dd>
<p>Compress <em>data</em> (a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object), returning the compressed data as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object.</p> <p>See <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code>LZMACompressor</code></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="py function"> <dt class="sig sig-object py" id="lzma.decompress">
<code>lzma.decompress(data, format=FORMAT_AUTO, memlimit=None, filters=None)</code> </dt> <dd>
<p>Decompress <em>data</em> (a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object), returning the uncompressed data as a <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></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"><code>LZMADecompressor</code></a> above for a description of the <em>format</em>, <em>memlimit</em> and <em>filters</em> arguments.</p> </dd>
</dl> </section> <section id="miscellaneous"> <h2>Miscellaneous</h2> <dl class="py function"> <dt class="sig sig-object py" id="lzma.is_check_supported">
<code>lzma.is_check_supported(check)</code> </dt> <dd>
<p>Return <code>True</code> if the given integrity check is supported on this system.</p> <p><code>CHECK_NONE</code> and <code>CHECK_CRC32</code> are always supported. <code>CHECK_CRC64</code> and <code>CHECK_SHA256</code> 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> </section> <section id="specifying-custom-filter-chains"> <span id="filter-chain-specs"></span><h2>Specifying custom filter chains</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 <code>"id"</code>, and may contain additional keys to specify filter-dependent options. Valid filter IDs are as follows:</p> <ul class="simple"> <li>
<p>Compression filters:</p> <ul> <li>
<code>FILTER_LZMA1</code> (for use with <code>FORMAT_ALONE</code>)</li> <li>
<code>FILTER_LZMA2</code> (for use with <code>FORMAT_XZ</code> and <code>FORMAT_RAW</code>)</li> </ul> </li> <li>
<p>Delta filter:</p> <ul> <li><code>FILTER_DELTA</code></li> </ul> </li> <li>
<p>Branch-Call-Jump (BCJ) filters:</p> <ul> <li><code>FILTER_X86</code></li> <li><code>FILTER_IA64</code></li> <li><code>FILTER_ARM</code></li> <li><code>FILTER_ARMTHUMB</code></li> <li><code>FILTER_POWERPC</code></li> <li><code>FILTER_SPARC</code></li> </ul> </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> <ul class="simple"> <li>
<code>preset</code>: A compression preset to use as a source of default values for options that are not specified explicitly.</li> <li>
<code>dict_size</code>: Dictionary size in bytes. This should be between 4 KiB and 1.5 GiB (inclusive).</li> <li>
<code>lc</code>: Number of literal context bits.</li> <li>
<code>lp</code>: Number of literal position bits. The sum <code>lc + lp</code> must be at most 4.</li> <li>
<code>pb</code>: Number of position bits; must be at most 4.</li> <li>
<code>mode</code>: <code>MODE_FAST</code> or <code>MODE_NORMAL</code>.</li> <li>
<code>nice_len</code>: What should be considered a “nice length” for a match. This should be 273 or less.</li> <li>
<code>mf</code>: What match finder to use – <code>MF_HC3</code>, <code>MF_HC4</code>, <code>MF_BT2</code>, <code>MF_BT3</code>, or <code>MF_BT4</code>.</li> <li>
<code>depth</code>: Maximum search depth used by match finder. 0 (default) means to select automatically based on other filter options.</li> </ul> <p>The delta filter stores the differences between bytes, producing more repetitive input for the compressor in certain circumstances. It supports one option, <code>dist</code>. 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, <code>start_offset</code>. This specifies the address that should be mapped to the beginning of the input data. The default is 0.</p> </section> <section id="examples"> <h2>Examples</h2> <p>Reading in a compressed file:</p> <pre data-language="python">import lzma
with lzma.open("file.xz") as f:
file_content = f.read()
</pre> <p>Creating a compressed file:</p> <pre data-language="python">import lzma
data = b"Insert Data Here"
with lzma.open("file.xz", "w") as f:
f.write(data)
</pre> <p>Compressing data in memory:</p> <pre data-language="python">import lzma
data_in = b"Insert Data Here"
data_out = lzma.compress(data_in)
</pre> <p>Incremental compression:</p> <pre data-language="python">import lzma
lzc = lzma.LZMACompressor()
out1 = lzc.compress(b"Some data\n")
out2 = lzc.compress(b"Another piece of data\n")
out3 = lzc.compress(b"Even more data\n")
out4 = lzc.flush()
# Concatenate all the partial results:
result = b"".join([out1, out2, out3, out4])
</pre> <p>Writing compressed data to an already-open file:</p> <pre data-language="python">import lzma
with open("file.xz", "wb") as f:
f.write(b"This data will not be compressed\n")
with lzma.open(f, "w") as lzf:
lzf.write(b"This *will* be compressed\n")
f.write(b"Not compressed\n")
</pre> <p>Creating a compressed file using a custom filter chain:</p> <pre data-language="python">import lzma
my_filters = [
{"id": lzma.FILTER_DELTA, "dist": 5},
{"id": lzma.FILTER_LZMA2, "preset": 7 | lzma.PRESET_EXTREME},
]
with lzma.open("file.xz", "w", filters=my_filters) as f:
f.write(b"blah blah blah")
</pre> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/lzma.html" class="_attribution-link">https://docs.python.org/3.12/library/lzma.html</a>
</p>
</div>
|