Search code examples
python-3.xlzma

Python lmza.LMZADecompressor() returning null byte


i compressed the bytes and when i tried to decompress it gives a null byte.

import lzma
comp = lzma.LZMACompressor()
decomp = lzma.LZMADecompressor()
a= comp.compress(b'alpha')
print(a)
b = decomp.decompress(a)
print(b)

this the results.

b'\xfd7zXZ\x00\x00\x04\xe6\xd6\xb4F\x02\x00!\x01\x16\x00\x00\x00t/\xe5\xa3'
b''

why is this occuring and how to solve it.


Solution

  • That is because comp.compress doesn't return the complete compressed bytestring. You have to append the result of comp.flush() to a.

    import lzma
    comp = lzma.LZMACompressor()
    decomp = lzma.LZMADecompressor()
    a= comp.compress(b'alpha') + comp.flush()
    print(a)
    b = decomp.decompress(a)
    print(b)