Python Lzma Module
The lzma module provides classes and convenience functions for compression and decompression using the LZMA compression algorithm. It includes support for the .xz and .lzma file formats.
Importing the Module
import lzma
Reading Compressed Files
To open an LZMA-compressed file for reading, use lzma.open().
import lzma
with lzma.open('file.txt.xz', 'rt') as f:
file_content = f.read()
print(file_content)
Writing Compressed Files
To open an LZMA-compressed file for writing, use lzma.open() with mode 'wt' (for text) or 'wb' (for binary).
import lzma
content = "Hello, world!"
with lzma.open('file.txt.xz', 'wt') as f:
f.write(content)
Compressing Data
You can compress data directly using lzma.compress().
import lzma
data = b"Hello, world!"
compressed_data = lzma.compress(data)
print(compressed_data)
Decompressing Data
You can decompress data using lzma.decompress().
decompressed_data = lzma.decompress(compressed_data)
print(decompressed_data)