1

I have a program that is supposed to compress some data with LZMA, but it returns an empty string.

# this is as much as I can isolate it.  For full code, visit https://replit.com/@inyourface3445/Text-Viewer-from-Bsace-64-URL#test.py
import lzma

zlib = lzma.LZMACompressor(preset = 9)
zlib_dc = lzma.LZMADecompressor()

txt = 'hello'

compressed_text = zlib.compress(txt.encode())
b64 = compressed_text

print(b64.decode('utf-16'))

s2 = zlib_dc.decompress(b64).decode()
print(len(s2))

The output looks like this:

㟽塺ZЀ훦䚴ġ켐챘
0

The first line is the raw compressed data, and the second line is the length of the output of the algorithem.

  • 2
    You never called `.flush()` to finish the compression process. It would be more convenient to use `lzma.compress()`, rather than explicitly creating a `LZMACompressor` object, as that function does everything instead of requiring you to handle all the details yourself. – jasonharper May 11 '22 at 17:47

1 Answers1

0

This was my mistake! I forgot to call zlib.flush() and therefore, LZMA never finished the compression. The solution to this is either to call zlib.flush() or just use lzma.compress() to avoid using zlib.flush() altogether.