Skip to content

Instantly share code, notes, and snippets.

@musteresel
Forked from dpanda/decompress_lzf_file.py
Created March 15, 2021 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musteresel/61e7dfa29e7ecf3ac4e641970daf9736 to your computer and use it in GitHub Desktop.
Save musteresel/61e7dfa29e7ecf3ac4e641970daf9736 to your computer and use it in GitHub Desktop.
Using python-lzf module, decompress a whole LZF file
import argparse
import lzf
import os
import struct
def decompress(lzf_file, out_file):
chunk_number=1
while True:
prefix = lzf_file.read(2)
if len(prefix) == 0:
# end of file
print('end of file')
break
elif prefix!=b'ZV':
# not a valid lzf chunk
print("Chunk #{} is invalid, does not begin with 'ZV' ('{}' found instead)".format(chunk_number, prefix.decode('utf8')))
break
# it's a lzf file
typ = lzf_file.read(1)
chunk_len = struct.unpack('>H', lzf_file.read(2))[0]
if typ == b'\x00':
print('Chunk #{} is uncompressed, {} bytes'.format(chunk_number, chunk_len))
chunk = lzf_file.read(chunk_len)
elif typ == b'\x01':
uncompressed_len = struct.unpack(">H", lzf_file.read(2))[0]
print('Chunk #{} is compressed, {} bytes (original size: {} bytes)'.format(chunk_number, chunk_len, uncompressed_len))
compressed_data = lzf_file.read(chunk_len)
chunk = lzf.decompress(compressed_data, uncompressed_len)
else:
print('3rd bytes of chunk #{} has an invalid value: "{}". Exiting'.format(chunk_number, typ))
return
out_file.write(chunk)
chunk_number += 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Decompress an LZF file')
parser.add_argument('inputfile', metavar='input_file', type=str, help='the LZF file to decompress')
args = parser.parse_args()
path = args.inputfile
path_out = path+'.unc'
with open(path,'rb') as in_file, open(path_out, 'wb') as out_file:
decompress(in_file, out_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment