Last active
April 5, 2023 10:30
-
-
Save a-sajjad72/98e3a105475e6b76a93d39a08b379064 to your computer and use it in GitHub Desktop.
As lz4 provides you the fastest (de)compression speed. Here is python script to create an archive and extract the archive using lz4 algorithm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import lz4.frame | |
def compression(files): | |
with open('archive.lz4', 'wb') as archive: | |
for file_name in files: | |
# Write file metadata as length-prefixed string to the archive | |
archive.write(len(file_name).to_bytes(2, byteorder='big')) | |
archive.write(file_name.encode("utf-8")) | |
# Compress the file and write it to the archive | |
with open(file_name, 'rb') as file: | |
compressed_data = lz4.frame.compress(file.read()) | |
archive.write(len(compressed_data).to_bytes(4, byteorder='big')) | |
archive.write(compressed_data) | |
# Function to extract files and read metadata | |
def decompression(archive_name): | |
with open(archive_name, 'rb') as archive: | |
while True: | |
# Read the next file metadata | |
file_name_length = int.from_bytes(archive.read(2), byteorder='big') | |
if not file_name_length: | |
break | |
file_name = archive.read(file_name_length).decode('utf-8') | |
file_size = int.from_bytes(archive.read(4), byteorder='big') | |
# Read the compressed file data and decompress it | |
compressed_data = archive.read(file_size) | |
decompressed_data = lz4.frame.decompress(compressed_data) | |
# Write the decompressed file to disk | |
with open(file_name, 'wb') as file: | |
file.write(decompressed_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment