How does file compression/ZIP files work?

346 viewsEngineeringOther

I understand it’s to save file space, but like…how?

Follow-up: If you have to compress a photo to save on file size (when emailing, for example) couldn’t it just send essentially a text file on how to uncompress it back to its original size and quality? (ex. “Set the resolution to XxY and add X color at Y pixels”)

Thank you!

In: Engineering

9 Answers

Anonymous 0 Comments

digital files are just a bunch of 1s and 0s (at the very basic level), and those are repeating, in essence: they have patterns.

compression tool or algorithm look for these patterns, then make a new (now compressed) file with “pointers” like Pattern A Goes Here, Pattern B Goes There, etc.

these pointers are often smaller than the actual patterns, and some files have a lot of similar patterns, the resulting compressed file can be way smaller than it’s actual file.

of course there are files that can’t be compressed very well and you see basically no noticeable change in their size.

you can see this firsthand with a little experiment: make a text file in notepad, put in repeating words or letters like aaaaaaaaaaa or bbbbbbbbbbbbb or ababababababab, save it as a `.txt` file and then compress it with whatever compression program you use (WinRAR, 7-Zip, NanaZip, etc.)

you’ll see that the text file may have like ~200kb size but the compressed file is just around ~1kb (the program found the pattern of letters and make pointers on how to find them, like “Repeat AB 20x here”, “Repeat AAAA 30x there”).

texts are among the easiest files to compress.

> couldn’t it just send essentially a text file on how to uncompress it back to its original size and quality?

in a simple way, no. To uncompress a compressed file, the program have to understand how it was compressed in the first place, and that information is available in the file, you can’t simply send a text file containing the compression information, it’s kind of inefficient–and as stated in above example, a single text file can be 200kb, but the compressed version is 1kb, why send the bigger text file?

[more answers](https://www.reddit.com/r/explainlikeimfive/comments/g9266e/eli5_how_does_file_compression_work/)

You are viewing 1 out of 9 answers, click here to view all answers.