r/AskProgramming • u/AdPsychological7065 • Jul 10 '26
Algorithms LZ77 compression algorithm is actually making the file size bigger
I was recently toying around with some compression, just trying to understand it from the basics. I came to know about LZ77, LZ78, and LZW algorithms in the last couple of days, understood the algorithms, and when implementing LZ77, every time I try to compress a txt file, it is making the output file bigger. The compression is done right coz when I decompress the compressed file, I get the exact text.
Is there something I am missing to understand? Can anyone help me out and figure out what I am doing wrong?
0
Upvotes
13
u/Lumpy-Notice8945 Jul 10 '26
For smal file sizes and specific algos this can happen, thats ehy there is multiple and tools like zip will usualy pick one that works either faster or has stronger compression.
Try generating a big file with repetitive text and see if the filesize changes. Not sure if these are container formats but if they are you can try compressibg multiple smal files too.
9
u/Felicia_Svilling Jul 10 '26
Fundamentally, there is no way to make lossless compression that always compresses the data. As proof, just think if that was the case, then you could compress the output from the compression algorithm until you reached zero bits.
In practice compression works by removing order from the file. If you compress something that is just random letters for example, you will not get any compression, or if you compress something that is allready compressed. Also as have been noted the compression adds some meta data.
This means that on average, if you randomly generate data and compresses it, the result will be larger. This effect will be larger the smaller files you use.
3
u/DSudz Jul 10 '26
It compresses the content and adds metadata. Sounds like the metadata ends up larger than the reduction in the content.
How big is the file? You'll never compress HelloWorld.txt ifb it is 11 bytes long.
2
u/MagicWolfEye Jul 10 '26 edited Jul 10 '26
What types of files did you try? e.g. .png files already use the same type of compression
Edit: Wow reddit; why do I get downvoted; png uses a micture of LZ77 and Huffman
4
u/mtimmermans Jul 10 '26
If your txt file is actually text, it means you got the output format right, but you're doing the compression wrong.
4
u/high_throughput Jul 10 '26
When this happens for a 62k txt file, you're either finding poor matches (e.g. returning the first substring instead of the longest, or having a tiny window), or you're encoding the data inefficiently (e.g. storing position and length as 32bit ints).
1
u/naemorhaedus Jul 10 '26
how big is the file? Every compression algorithm has overhead.
3
u/Graumm Jul 10 '26
It’s not what you asked about specifically but it’s fun to know that you cannot compress an encryption certificate without it getting larger. They are very intentionally designed to be high entropy and thus are pretty much guaranteed to be larger after compression.
Most text compresses pretty well, but generic compression can only really compress data that has repeating patterns in it. They make bitwise dictionaries of repeated sequences and then deduplicate them.
There are web specific compression algos like Brotli which includes a dictionary of very common patterns in websites, so that the compressed data does not have to re-include them with every single compressed file. Anything that uses it can just assume that the standard dictionary exists on both sides.
Similarly if you compress singular files you may not get as much compression as you would if you compressed many different files that are similar. Their repeating patterns get stored in a single dictionary instead of creating independent dictionaries.
2
u/Individual-Flow9158 Jul 11 '26
If the number of bytes the compression saved, is less than the header size of the .zip format. Or if the data needs to be padded before being encrypted with a password. Then yep. That can happen in any compression algorithm.
2
u/Dazzling_Music_2411 Jul 11 '26
Note that this does not logically follow.
You could be "compressing" it all wrong, and just reversing this wrong process.