Skip to content

Implement LZMA2 Compression Algorithm #12967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions compression_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import LZMA

def lzma2_compress(data: bytes, preset: int = 9) -> bytes:
"""
Compress data using the LZMA2 algorithm with the specified preset.

:param data: The data to compress.
:param preset: The preset level for compression. Default is 9.
:return: The compressed data as bytes.
"""
lzma_filter = LZMA.LZMACompressor(preset=preset, format=LZMA.FORMAT_ALONE)
compressed_data = lzma_filter.compress(data)
return compressed_data + lzma_filter.flush()


def lzma2_decompress(compressed_data: bytes) -> bytes:
"""
Decompress data compressed with LZMA2.

:param compressed_data: The compressed data as bytes.
:return: The decompressed data as bytes.
"""
lzma_decompressor = LZMA.LZMADecompressor()
decompressed_data = bytearray()
decompressed_data.extend(lzma_decompressor.decompress(compressed_data))
return bytes(decompressed_data)
27 changes: 27 additions & 0 deletions lzma2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import LZMA

def compress_lzma2(data: bytes) -> bytes:
"""
Compress data using the LZMA2 algorithm.

:param data: The data to compress as bytes.
:return: The compressed data as bytes.
"""
compressed_data = bytearray()
with LZMA.LZMACompressor() as compressor:
compressed_data.extend(compressor.compress(data))
compressed_data.extend(compressor.flush())
return bytes(compressed_data)


def decompress_lzma2(compressed_data: bytes) -> bytes:
"""
Decompress data compressed with the LZMA2 algorithm.

:param compressed_data: The compressed data as bytes.
:return: The decompressed data as bytes.
"""
decompressed_data = bytearray()
with LZMA.LZMADecompressor() as decompressor:
decompressed_data.extend(decompressor.decompress(compressed_data))
return bytes(decompressed_data)
50 changes: 50 additions & 0 deletions test_lzma2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import py7zr

def compress_lzma2(data: bytes, compression_level: int = 9) -> bytes:
"""
Compress data using LZMA2 algorithm.

:param data: The data to compress.
:param compression_level: The compression level (0-9). Default is 9.
:return: The compressed data.
"""
with py7zr.SevenZipFile(mode="w", compression_type=py7zr.LZMA2) as z:
z.write(data, arcname="data.bin", compress_level=compression_level)
return z.read("data.bin")

def decompress_lzma2(data: bytes) -> bytes:
"""
Decompress data using LZMA2 algorithm.

:param data: The data to decompress.
:return: The decompressed data.
"""
with py7zr.SevenZipFile(data, mode="r") as z:
return z.read("data.bin")

def test_lzma2_compression():
input_data = b"This is some test data for LZMA2 compression."
compressed_data = compress_lzma2(input_data)
decompressed_data = decompress_lzma2(compressed_data)

assert decompressed_data == input_data

def test_lzma2_compression_performance():
input_data = bytearray(1000000)
for i in range(1000000):
input_data[i] = i % 256

start_time = time.time()
compressed_data = compress_lzma2(input_data)
compression_time = time.time() - start_time

start_time = time.time()
decompressed_data = decompress_lzma2(compressed_data)
decompression_time = time.time() - start_time

print(f"LZMA2 compression time: {compression_time:.4f} seconds")
print(f"LZMA2 decompression time: {decompression_time:.4f} seconds")

if __name__ == "__main__":
test_lzma2_compression()
test_lzma2_compression_performance()