Youcha Youcha - 1 year ago 276
Python Question

Decompressing with PyLZMA

I compress my files using this script

import pylzma, struct
i = open(path+fileName,'rb')
o = open(path+zipName+'.zip','wb')
data =
c = pylzma.compressfile(data, eos=1)
result =
result += struct.pack('<Q', len(data))
o.write(result +

I use this method as shown in the PyLZMA documentation because it allows my files to be readable by 7zip or lzma.exe. Decompression using 7zip works fine but it does not work when I use PyLZMA. I use this:

i = open(path+name+'.zip', 'rb')
o = open(path+name, 'wb')
data =
u = pylzma.decompress(data)

It stops on pylzma.decompress and I receive the following error:

TypeError: Error while decompressing: 1

Answer Source

If I'm reading the documentation correctly (I'm having trouble installing PyLZMA so I am unable to verify), compress() outputs a string that decompress() can handle.

However, in order to make the compressed string compatible with other utilities, it is necessary to insert the 8-byte length in between the first 5 bytes and the rest of the compressed data.

Thus, if you want to decompress the data using PyLZMA, I suspect you will need to manually remove that 8-byte length field (quickest way would probably be to open the input file, read 5 bytes, skip 8, then read the remainder of the file).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download