lzma — 压缩使用 LZMA 算法

Added in version 3.3.

源代码: 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 supporting the .xz and legacy .lzma file formats used by the xz utility, as well as raw compressed streams.

The interface provided by this module is very similar to that of the bz2 module. Note that LZMAFile and bz2.BZ2File are not thread-safe, so if you need to use a single LZMAFile instance from multiple threads, it is necessary to protect it with a lock.

exception lzma. LZMAError

This exception is raised when an error occurs during compression or decompression, or while initializing the compressor/decompressor state.

读写压缩文件

lzma. open ( filename , mode = 'rb' , * , format = None , check = -1 , preset = None , 过滤 = None , encoding = None , errors = None , newline = None )

Open an LZMA-compressed file in binary or text mode, returning a 文件对象 .

The filename argument can be either an actual file name (given as a str , bytes or 像路径 object), in which case the named file is opened, or it can be an existing file object to read from or write to.

The mode argument can be any of "r" , "rb" , "w" , "wb" , "x" , "xb" , "a" or "ab" 对于二进制模式,或 "rt" , "wt" , "xt" ,或 "at" for text mode. The default is "rb" .

When opening a file for reading, the format and 过滤 arguments have the same meanings as for LZMADecompressor 。在此情况下, check and preset arguments should not be used.

When opening a file for writing, the format , check , preset and 过滤 arguments have the same meanings as for LZMACompressor .

For binary mode, this function is equivalent to the LZMAFile 构造函数: LZMAFile(filename, mode, ...) 。在此情况下, encoding , errors and newline arguments must not be provided.

对于文本模式, LZMAFile object is created, and wrapped in an io.TextIOWrapper instance with the specified encoding, error handling behavior, and line ending(s).

3.4 版改变: 添加支持 "x" , "xb" and "xt" 模式。

3.6 版改变: 接受 像路径对象 .