lzma - Interface to the liblzma compression library.
| Use Case | Command | Description |
|---|---|---|
| Compress bytes | lzma.compress(data) | Compress using default XZ format |
| Decompress bytes | lzma.decompress(data) | Decompress XZβcompressed data |
| Open XZ for reading | lzma.open('file.xz', 'rb') | Binary read from compressed file |
| Open XZ for writing | lzma.open('file.xz', 'wb') | Binary write to compressed file |
| Incremental compression | c = lzma.LZMACompressor(); c.compress(chunk); c.flush() | Compress data in chunks |
| Incremental decompression | d = lzma.LZMADecompressor(); d.decompress(chunk) | Decompress stream progressively |
| Raw LZMA2 compression | lzma.compress(data, format=lzma.FORMAT_RAW, filters=[{'id': lzma.FILTER_LZMA2}]) | Compress with custom filter chain |
| Check integrity support | lzma.is_check_supported(lzma.CHECK_CRC64) | Test if a check ID is available |
https://docs.python.org/3.10/library/lzma.html
The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.
This module provides a class for reading and writing compressed files, classes for incremental (de)compression, and convenience functions for oneβshot (de)compression.
These classes and functions support both the XZ and legacy LZMA container formats, as well as raw compressed data streams.
LZMAFile inherits _compression.BaseStream (io.BufferedIOBase)LZMAError inherits builtins.Exception (builtins.BaseException)LZMACompressor inherits builtins.objectLZMADecompressor inherits builtins.objectLZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)
Create a compressor object for compressing data incrementally.
format specifies the container format to use for the output. This can be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.
check specifies the integrity check to use. For FORMAT_XZ, the default is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not support integrity checks; for these formats, check must be omitted, or be CHECK_NONE.
The settings used by the compressor can be specified either as a preset compression level (with the preset argument), or in detail as a custom filter chain (with the filters argument). For FORMAT_XZ and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset level. For FORMAT_RAW, the caller must always specify a filter chain; the raw compressor does not support preset compression levels.
preset (if provided) should be an integer in the range 0β9, optionally ORβed with the constant PRESET_EXTREME.
filters (if provided) should be a sequence of dicts. Each dict should have an entry for "id" indicating the ID of the filter, plus additional entries for options to the filter.
For oneβshot compression, use the compress() function instead.
__init__(self, /, *args, **kwargs) β Initialize self.compress(self, data, /) β Provide data to the compressor object. Returns a chunk of compressed data if possible, or b'' otherwise. When finished, call flush() to finish the compression process.flush(self, /) β Finish the compression process. Returns the compressed data left in internal buffers. The compressor object may not be used after this method is called.__new__(*args, **kwargs) β Create and return a new object (static method).LZMADecompressor(format=0, memlimit=None, filters=None)
Create a decompressor object for decompressing data incrementally.
format Specifies the container format of the input stream. If this isFORMAT_AUTO (the default), the decompressor will automatically detect whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with FORMAT_RAW cannot be autodetected.
memlimit
Limit the amount of memory used by the decompressor. This will cause decompression to fail if the input cannot be decompressed within the given limit.
filters
A custom filter chain. This argument is required for FORMAT_RAW, and not accepted with any other format. When provided, this should be a sequence of dicts, each indicating the ID and options for a single filter.
For oneβshot decompression, use the decompress() function instead.
__init__(self, /, *args, **kwargs) β Initialize self.decompress(self, /, data, max_length=-1) β Decompress data, returning uncompressed data as bytes. If max_length is nonnegative, returns at most that many bytes of decompressed data. If this limit is reached and further output can be produced, self.needs_input will be set to False. In this case, the next call to decompress() may provide data as b'' to obtain more of the output. If all of the input data was decompressed and returned, self.needs_input will be set to True. Attempting to decompress data after the end of stream is reached raises an EOFError. Any data found after the end of the stream is ignored and saved in the unused_data attribute.__new__(*args, **kwargs) β Create and return a new object (static method).check β ID of the integrity check used by the input stream.eof β True if the endβofβstream marker has been reached.needs_input β True if more input is needed before more decompressed data can be produced.unused_data β Data found after the end of the compressed stream.Call to liblzma failed.
Method resolution order: LZMAError β builtins.Exception β builtins.BaseException β builtins.object
__weakref__ β list of weak references to the object (if defined)__init__(self, /, *args, **kwargs) β Initialize self.__new__(*args, **kwargs) β Create and return a new object.__delattr__(self, name, /) β Implement delattr(self, name).__getattribute__(self, name, /) β Return getattr(self, name).__reduce__(...) β Helper for pickle.__repr__(self, /) β Return repr(self).__setattr__(self, name, value, /) β Implement setattr(self, name, value).__setstate__(...)__str__(self, /) β Return str(self).with_traceback(...) β Exception.with_traceback(tb) β set self.__traceback__ to tb and return self.__cause__ β exception cause__context__ β exception context__dict____suppress_context____traceback__argsLZMAFile(filename=None, mode='r', *, format=None, check=-1, preset=None, filters=None)
A file object providing transparent LZMA (de)compression.
An LZMAFile can act as a wrapper for an existing file object, or refer directly to a named file on disk.
Note that LZMAFile provides a binary file interface β data read is returned as bytes, and data to be written must be given as bytes.
Method resolution order: LZMAFile β _compression.BaseStream β io.BufferedIOBase β _io._BufferedIOBase β io.IOBase β _io._IOBase β builtins.object
__init__(self, filename=None, mode='r', *, format=None, check=-1, preset=None, filters=None) β Open an LZMAβcompressed file in binary mode. filename can be an actual file name (str, bytes, or PathLike object) or an existing file object. mode can be "r" (default), "w", "x", or "a" (and equivalently "rb", "wb", "xb", "ab"). format specifies the container format; for reading defaults to FORMAT_AUTO, otherwise FORMAT_XZ. check (writing only) specifies the integrity check. preset (writing) compression level 0β9, optionally ORβed with PRESET_EXTREME. filters (writing) custom filter chain. For FORMAT_RAW a filter chain is always required.close(self) β Flush and close the file. May be called more than once; after closing any other operation raises ValueError.fileno(self) β Return the file descriptor for the underlying file.peek(self, size=-1) β Return buffered data without advancing the file position. Always returns at least one byte of data, unless at EOF.read(self, size=-1) β Read up to size uncompressed bytes. If negative or omitted, read until EOF. Returns b"" if already at EOF.read1(self, size=-1) β Read up to size uncompressed bytes, trying to avoid multiple reads from the underlying stream. Returns b"" if at EOF.readable(self) β Return whether the file was opened for reading.readline(self, size=-1) β Read a line of uncompressed bytes. The terminating newline (if present) is retained. If size is nonβnegative, no more than that many bytes will be read. Returns b'' if already at EOF.seek(self, offset, whence=0) β Change the file position. whence values: 0 (start of stream, offset β₯ 0), 1 (current position), 2 (end of stream, offset β€ 0). Returns the new file position. Seeking is emulated and may be extremely slow.seekable(self) β Return whether the file supports seeking.tell(self) β Return the current file position.writable(self) β Return whether the file was opened for writing.write(self, data) β Write a bytes object to the file. Returns the number of uncompressed bytes written (always the length of data). Note that due to buffering, the file on disk may not reflect the data until close() is called.closed β True if this file is closed.__abstractmethods__ = frozenset()detach(self, /) β Disconnect this buffer from its underlying raw stream and return it. After detaching, the buffer is in an unusable state.readinto(self, buffer, /)readinto1(self, buffer, /)__del__(...)__enter__(...)__exit__(...)__iter__(self, /) β Implement iter(self).__next__(self, /) β Implement next(self).flush(self, /) β Flush write buffers, if applicable. Not implemented for readβonly and nonβblocking streams.isatty(self, /) β Return whether this is an 'interactive' stream. Returns False if it can't be determined.readlines(self, hint=-1, /) β Return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (bytes/characters) of all lines so far exceeds hint.truncate(...) β Truncate file to size bytes. File pointer is left unchanged. size defaults to the current IO position as reported by tell(). Returns the new size.writelines(self, lines, /) β Write a list of lines to stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.__new__(*args, **kwargs) β Create and return a new object (static).__dict__compress(data, format=1, check=-1, preset=None, filters=None)
Compress a block of data.
Refer to LZMACompressor's docstring for a description of the optional arguments format, check, preset and filters.
For incremental compression, use an LZMACompressor instead.
decompress(data, format=0, memlimit=None, filters=None)
Decompress a block of data.
Refer to LZMADecompressor's docstring for a description of the optional arguments format, check and filters.
For incremental decompression, use an LZMADecompressor instead.
is_check_supported(check_id, /)
Test whether the given integrity check is supported.
Always returns True for CHECK_NONE and CHECK_CRC32.
open(filename, mode='rb', *, format=None, check=-1, preset=None, filters=None, encoding=None, errors=None, newline=None)
Open an LZMAβcompressed file in binary or text mode.
filename can be either an actual file name (str, bytes, or PathLike object) or an existing file object.
The mode argument can be "r", "rb" (default), "w", "wb", "x", "xb", "a", or "ab" for binary mode, or "rt", "wt", "xt", or "at" for text mode.
The format, check, preset and filters arguments specify the compression settings, as for LZMACompressor, LZMADecompressor and LZMAFile.
For binary mode, this function is equivalent to the LZMAFile constructor: LZMAFile(filename, mode, ...). In this case, the encoding, errors and newline arguments must not be provided.
For text mode, an LZMAFile object is created, and wrapped in an io.TextIOWrapper instance with the specified encoding, error handling behavior, and line ending(s).
CHECK_CRC32 = 1
CHECK_CRC64 = 4
CHECK_ID_MAX = 15
CHECK_NONE = 0
CHECK_SHA256 = 10
CHECK_UNKNOWN = 16
FILTER_ARM = 7
FILTER_ARMTHUMB = 8
FILTER_DELTA = 3
FILTER_IA64 = 6
FILTER_LZMA1 = 4611686018427387905
FILTER_LZMA2 = 33
FILTER_POWERPC = 5
FILTER_SPARC = 9
FILTER_X86 = 4
FORMAT_ALONE = 2
FORMAT_AUTO = 0
FORMAT_RAW = 3
FORMAT_XZ = 1
MF_BT2 = 18
MF_BT3 = 19
MF_BT4 = 20
MF_HC3 = 3
MF_HC4 = 4
MODE_FAST = 1
MODE_NORMAL = 2
PRESET_DEFAULT = 6
PRESET_EXTREME = 2147483648
__all__ = ['CHECK_NONE', 'CHECK_CRC32', 'CHECK_CRC64', 'CHECK_SHA256', ...]
/usr/lib/python3.10/lzma.py
Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 11:14 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format