Hướng dẫn dùng python lzma python
Làm thế nào để sử dụng lzma2 trong mã python? (How to use lzma2 in python code?) I know there is a module called pylzma. But it only support lzma, not lzma2. My current solution is using Is there a better way? You can use backports.lzma, see for more info: Python 2.7: Compressing data with the XZ format using the "lzma" module Then it's simply a matter of doing e.g.: Or simpler (XZ format is default): See http://docs.python.org/dev/library/lzma.html for usage details. (by
Huo、Dagh Bunnstad) #compression #7zip #lzma #Python New in version 3.3. 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 supporting the The interface provided by this module is very similar to that of the lzma. LZMAError ¶This exception is raised when an error occurs during compression or decompression, or while initializing the compressor/decompressor state. Reading and writing compressed files¶lzma. 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, returning a file object. The filename argument can be either an actual file name (given as a The mode argument can be any of When opening a file for reading, the
format and filters arguments have the same meanings as for When opening a file for writing, the format, check, preset and filters arguments have the same meanings as for
For binary mode, this function is equivalent to the For text mode, a
Changed in version 3.4: Added support for the lzma. LZMAFile (filename=None, mode='r', *, format=None, check=- 1, preset=None,
filters=None)¶Open an LZMA-compressed file in binary mode. An The mode argument can be either If filename is a file object (rather than an actual file name), a mode of 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. When opening a file for reading, the
format and filters arguments have the same meanings as for When opening a file for writing, the format, check, preset and filters arguments have the same meanings as for
The following method is also provided: peek (size=- 1)¶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 size argument is ignored). Note While calling Changed in version 3.4: Added support for the Changed in version 3.5: The
Compressing and decompressing data in memory¶classlzma. LZMACompressor (format=FORMAT_XZ, check=- 1, preset=None, filters=None)¶Create a compressor object, which can be used to compress data incrementally. For a more convenient way of compressing a single chunk of data, see The format argument specifies what container format should be used. Possible values are:
The check 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:
If the specified check is not supported, an The compression settings 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). The preset argument (if provided) should be an integer between Note 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 The filters argument (if provided) should be a filter chain specifier. See Specifying custom filter chains for details. compress (data)¶Compress data (a
flush ()¶Finish the compression process, returning a The compressor cannot be used after this method has been called. lzma. LZMADecompressor (format=FORMAT_AUTO, memlimit=None, filters=None)¶Create a decompressor object, which can be used to decompress data incrementally. For a more convenient way of decompressing an entire compressed stream at once, see The format argument specifies the container format that should be used. The default is The memlimit
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 The filters argument specifies the filter chain that was used to create the stream being decompressed. This argument is required if
format is Note This class does not transparently handle inputs containing multiple compressed streams, unlike decompress (data,
max_length=- 1)¶Decompress data (a bytes-like object), returning uncompressed data as bytes. Some of data may be buffered internally, for use in later calls to
If max_length is nonnegative, returns at most max_length bytes of decompressed data. If this limit is reached and further output can be produced,
the If all of the input data was
decompressed and returned (either because this was less than max_length bytes, or because max_length was negative), the 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 Changed in version 3.5: Added the max_length parameter. check ¶The ID of the integrity
check used by the input stream. This may be eof ¶
unused_data ¶Data found after the end of the compressed stream. Before the end of the stream is reached, this will be needs_input ¶
New in version 3.5. lzma. compress (data, format=FORMAT_XZ, check=- 1, preset=None, filters=None)¶Compress
data (a See lzma. decompress (data, format=FORMAT_AUTO, memlimit=None, filters=None)¶Decompress
data (a If data is the concatenation of multiple distinct compressed streams, decompress all of these streams, and return the concatenation of the results. See
Miscellaneous¶lzma. is_check_supported (check)¶Return
Specifying custom filter chains¶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
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. Compression filters support the following options (specified as additional entries in the dictionary representing the filter):
The delta filter stores the differences between bytes, producing more repetitive input for the compressor in certain circumstances. It supports one option, 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, Examples¶Reading in a compressed file: import lzma with lzma.open("file.xz") as f: file_content = f.read() Creating a compressed file: import lzma data = b"Insert Data Here" with lzma.open("file.xz", "w") as f: f.write(data) Compressing data in memory: import lzma data_in = b"Insert Data Here" data_out = lzma.compress(data_in) Incremental compression: 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]) Writing compressed data to an already-open file: 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") Creating a compressed file using a custom filter chain: 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") |