Python decompression module for .Z files compressed using Unix compress utility
This is a purely Python adaptation of Mark Adler's 'unlzw' C function. Which can be found here on Stackoverflow. If you don't have any specific requirement to execute this as Python code, then I recommend using subprocess to call uncompress, gzip, or something similar to decompress your file. Python will be much slower than using any compiled utility for the same purpose. Note: there are currently no Python modules which provide this functionality (the gzip system utility does - but the gzip Python module does not).
I adapted this function so that it could be used in AWS Lambda scripts to avoid convoluted virtualenv setups. Though it could probably also be done by wrapping the C code using something like Cython. Currently my application use case is not incredibly time critical, and I expect submission of .Z files will slowly decrease. Though out of personal interest, I would like to minimaize the execution time.
This function takes compressed data as any type which can be converted to a bytearray (generally a string), and returns a UTF-8 decoded string containing the decompressed data.
from unlzw import unlzw
with open('file.Z') as fh:
compressed_data = fh.read()
uncompressed_data = unlzw(compressed_data)