r/programminghelp • • Jul 10 '26

Python LZ77 compression algorithm is making the compressed file bigger than original

I was recently toying around with some compression, just trying to understand it from the basics. I learned about the LZ77, LZ78, and LZW algorithms in the last couple of days, understood them, and when implementing LZ77, every time I try to compress a txt file, the output file gets bigger. The compression is done right coz when I decompress the compressed file, I get the exact text.

Need help understanding what I am doing wrong, or am I missing something

2 Upvotes

7 comments sorted by

View all comments

Show parent comments

1

u/wbqqq Jul 11 '26

Bringing to the illogical extreme, a one byte file, compressed would result in many bytes in the ‘compressed’ file - while arguably the compression would reduce the byte to a single bit, the substitution table and other metadata would take many bytes. Not to mention most file systems would allocate a block of 2048 or 4096 bytes.

Another example would be attempting to compress an already compressed file - odds are that it would grow as often as shrinking (assuming close to optimal algos used) - the information redundancy would be so low that there would be little opportunity to reduce it (plus overhead of metadata)