Skip to content

Instantly share code, notes, and snippets.

@dpanda
Created November 18, 2016 21:49
Show Gist options
  • Save dpanda/ed010e4c7fd69dbbbff04485f3ecc4b8 to your computer and use it in GitHub Desktop.
Save dpanda/ed010e4c7fd69dbbbff04485f3ecc4b8 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)
@sameeramritkar
Copy link

when i tried to use this to uncompress .lzf file getting below error
usage: uncompress.py [-h]
C:/Results/lzf.app
uncompress.py: error: the following arguments are required: C:/Results/lzf.app

I am passing input file path at below line
parser.add_argument('inputfile', metavar='C:/Results/lzf.app', type=str, help='the LZF file to decompress')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment