Algorithms, visualizedExhibit 10
How does ZIP compression work?
A ZIP file finds a shorter way to say exactly the same thing. Its Deflate method stacks two ideas: LZ77 replaces repeated text with tiny “go back, copy” notes, and Huffman coding gives common symbols shorter codes. Nothing is lost, and ordinary text often shrinks to about a third. Type below and watch it happen.
Try it: watch text shrink with Huffman coding
Type anything, or start with the word from the video. The page counts each symbol, builds a Huffman tree from the bottom up, and reads off the codes. Nothing is sent anywhere; it all runs in your browser.
1. Count the symbols
2. Build the tree from the bottom up
Each step joins the two rarest groups into one, until everything is a single tree.
3. Read off the codes
Left branches are 0, right branches are 1. Common symbols sit near the top, so their codes are short.
| Symbol | Count | Code | Bits used |
|---|
4. The result
Encoded bits (each symbol’s code in turn, alternately shaded):
- Plain text (8 bits per character)
- …
- Huffman codes
- …
- Size compared with plain text
- …
Simplified model. Like the video, the baseline counts 8 bits per character. That is true for plain English letters, but emoji and most non-English characters take more than 8 bits in a real file, so for them the baseline here is an understatement. The page works on Unicode code points, so an emoji built from several code points (a skin tone, a ❤️ with a style marker) counts as several symbols. Ties are broken the same way every time: the group containing the earliest character code goes first. A real compressed file must also store the code table, which isn’t counted here, so a tiny example barely wins, if at all. Input is capped at 500 characters.
Bonus: LZ77’s “go back, copy”
Before Huffman coding, Deflate hunts for repeats. Press the button to find the one in this phrase.
Watch: how a ZIP file makes files smaller
Two ideas, one from 1977 and one from a 1951 term paper, in under 5 minutes.
The video loads from YouTube only when you press play. Watch on YouTube
Why are files bigger than they need to be?
Start with how plain text is stored. Each character usually takes 8 bits. The letter “e” gets 8 bits, and so does “z”, even though “e” turns up far more often in English. Every symbol pays the same price, whether it appears on every line or once a chapter.
Real text also repeats itself constantly: the same words, the same phrases, the same chunks of code, the same tags in a web page. A plain file spells every repeat out in full, every time.
That waste is the opportunity. Compression doesn’t make information disappear. It finds a shorter way to say exactly the same thing, so the original can be rebuilt perfectly, bit for bit. That’s called lossless compression, and it’s what ZIP does. (Photos and music often use lossy compression instead, which throws away detail you’re unlikely to notice. A ZIP file never does that: you’d be horrified if a spreadsheet came back with slightly different numbers.)
ZIP attacks the two kinds of waste with two different ideas. One deals with repetition; the other deals with the fact that some symbols are much more common than others.
How does LZ77 work?
The first idea is simple: if you’ve already said it, just point back. Take the phrase “to be or not to be”. When the compressor reaches the second “to be”, it notices that the same five characters appeared thirteen characters earlier. So instead of storing them again, it writes a tiny note: go back 13, copy 5. (Count it yourself: “to be or not ” with its trailing space is exactly 13 characters, and “to be” is 5.)
To decompress, you just follow the notes. Copy the plain letters as they come, and whenever you meet a note, look back that many characters in what you’ve already written and copy that many again. The output is identical to the input, every time.
Jacob Ziv and Abraham Lempel published this idea in 1977, which is why it’s called LZ77. ZIP’s version looks for matches in the previous 32 kilobytes of data, a sliding window that moves along with the text. The Deflate specification allows a single note to copy anywhere from 3 to 258 characters, from up to 32,768 bytes back. In one short phrase that saves a little. In a long document, full of repeated words, names and phrases, those little notes replace a huge amount of text.
What is Huffman coding?
The second idea is older, and it began with a choice. In 1951, MIT professor Robert Fano offered his information theory students a deal: take the final exam, or write a term paper on finding the most efficient binary code. A student named David Huffman chose the paper. He worked on it for months and was about to give up and study for the exam when the answer clicked.
Fano had worked on the problem with Claude Shannon, and their method, now called Shannon–Fano coding, built codes from the top down by splitting the symbols into groups. It was good, but not always the best possible. Huffman’s trick was to turn the problem upside down and build the code from the bottom up, starting with the rarest symbols. He published it in 1952, and proved that no code which replaces one symbol at a time with a fixed pattern of bits can do better. In that sense, Huffman codes are optimal.
How do you build a Huffman tree?
The whole method fits in four steps, and the demo above runs exactly these:
- List every symbol with how often it appears.
- Take the two rarest and join them into a pair. The pair’s count is the two counts added together.
- Treat that pair as a single symbol and repeat: join the two rarest again, whether they’re letters or pairs, until everything is joined into one tree.
- Walk from the top of the tree down to each symbol, writing 0 for a left turn and 1 for a right turn. That path is the symbol’s code.
Because rare symbols are joined first, they end up at the bottom of the tree, far from the top, with long codes. Common symbols are joined last, so they sit near the top with short codes.
Worked example: “abracadabra”
This is the example from the video. Eleven letters at 8 bits each is 88 bits. The letter “a” appears five times, “b” and “r” twice each, and “c” and “d” once. First, the two rarest, “c” and “d”, join into a pair worth 2. Now there are three groups worth 2 (“b”, “r” and the c-d pair); one way to break the tie joins “b” with the c-d pair, making 4. Then “r” joins that, making 6, and finally “a” (5) joins the 6 to finish the tree at 11, one for every letter.
| Letter | Count | Code | Bits per letter | Bits in total |
|---|---|---|---|---|
| a | 5 | 0 | 1 | 5 |
| r | 2 | 10 | 2 | 4 |
| b | 2 | 110 | 3 | 6 |
| c | 1 | 1110 | 4 | 4 |
| d | 1 | 1111 | 4 | 4 |
| Total | 11 | 23 |
So “a” gets a one-bit code, and the rarest letters get four bits. Add it all up: 5 + 4 + 6 + 4 + 4 = 23 bits instead of 88, about 26% of the original. A different tie-break gives different codes, but never a different total: every Huffman tree for the same counts uses the same number of bits.
How does the decoder know where one code ends?
If codes have different lengths, you might expect to need a separator between them. You don’t, thanks to a neat guarantee:no code is the beginning of any other code. That’s called a prefix code (or prefix-free code), and every Huffman tree produces one automatically, because symbols only ever sit at the ends of branches, never part of the way down another symbol’s path.
So a decoder just reads bits one at a time and walks down the tree. The moment it reaches a symbol, it writes it out and jumps back to the top. Try it with the codes above on the start of the encoded word, 0110100: 0 is “a”, 110 is “b”, 10 is “r”, 0 is “a”. There’s never a moment of doubt, and no gaps are needed.
What is Deflate, and how does ZIP use it?
In 1989, a programmer named Phil Katz released the ZIP format with his program PKZIP. A few years later he designed the method ZIP files still use by default today, called Deflate. The ZIP format can hold files compressed in several different ways, but Deflate (listed in the official ZIP specification as method 8) is the common default.
Deflate does both ideas, in order:
- LZ77 replaces repeats with “go back, copy” notes.
- Huffman coding then squeezes those notes and the remaining letters into short codes, using code tables that suit the data.
The specification lets each block of data be stored with no compression, compressed with a fixed, built-in set of Huffman codes, or compressed with its own custom codes. For custom codes, the file has to carry the code table, but it does this cheaply: the code is completely defined by the length of each symbol’s code, so only those lengths need to be stored.
Deflate was published as an open specification, RFC 1951, in 1996, and it’s everywhere: in ZIP files, in gzip, in PNG images, and in a lot of the web pages your browser downloads. Run a classic test file, the text of Alice’s Adventures in Wonderland, through gzip at its strongest setting, and 152,089 bytes becomes 54,191 bytes: about 35.6% of the original, roughly a third.
The two ideas side by side
| Idea | Waste it removes | How |
|---|---|---|
| LZ77 (Ziv and Lempel, 1977) | Repeated words, phrases and chunks of code | Replaces a repeat with “go back, copy” (distance, length), searching the last 32 KB |
| Huffman coding (1951, published 1952) | Some symbols being far more common than others | Gives common symbols short codes and rare ones long codes, with no code the start of another |
| Deflate (Phil Katz; RFC 1951, 1996) | Both at once | LZ77 first, then Huffman codes for the letters and the notes; the default method in ZIP files |
Why don’t JPEG photos and MP4 videos shrink when you zip them?
So why doesn’t zipping your holiday photos save anything? Because JPEG photos and MP4 videos are already compressed. Their own formats have already squeezed out the repetition, so there are few repeats left for LZ77 to point back to and little unevenness for Huffman coding to exploit.
The same goes for files that are already zipped, and for most audio and video formats. Compressing twice doesn’t help. Text, spreadsheets, source code and many document formats are a different story: they’re full of repetition, so they shrink well.
Can any compressor shrink every file?
No, and it’s not about clever engineering: it’s counting. There are fewer short files than long ones. For example, there are 256 different files that are exactly 8 bits long, but only 255 files shorter than that, even counting the empty one (1 + 2 + 4 + 8 + 16 + 32 + 64 + 128). A lossless compressor has to give every input its own output, or it couldn’t rebuild the right one. So if some files get shorter, others must get longer.
You can see this in the demo: type a string where every character is different and the Huffman total barely beats, or even loses to, the plain 8 bits per character, before the code table is even counted. Real compressors limit the damage (Deflate can fall back to storing a block uncompressed), but they can’t avoid it. On average, truly random data can’t be compressed at all. Compression works on real files because real files aren’t random.
Frequently asked questions
How does ZIP compression work?
Most ZIP files use a method called Deflate, which stacks two ideas. First, LZ77 replaces repeated text with short “go back, copy” notes. Then Huffman coding gives common symbols short codes and rare ones longer codes. Nothing is thrown away, so the original is rebuilt bit for bit. On ordinary English text, gzip (which uses the same Deflate method) shrinks Alice’s Adventures in Wonderland from about 152 KB to about 54 KB, roughly a third.
What is Huffman coding and who invented it?
Huffman coding builds a variable-length binary code from how often each symbol appears: you repeatedly join the two rarest symbols into a pair until everything forms one tree, then read each symbol’s path as zeros and ones. David Huffman found the method in 1951 as an MIT student, in a term paper he chose instead of Robert Fano’s final exam, and published it in 1952. No symbol-by-symbol code can do better.
What is LZ77?
LZ77 is the “go back, copy” idea published by Jacob Ziv and Abraham Lempel in 1977. When the compressor meets text it has already seen, it writes a tiny note instead: how far back the earlier copy starts and how many characters to copy. In “to be or not to be”, the second “to be” becomes “go back 13, copy 5”. Deflate searches the previous 32 KB for matches.
Why don’t JPEG photos or MP4 videos get smaller when I zip them?
Because they are already compressed. JPEG and MP4 files have had their repetition squeezed out, so there is almost nothing left for LZ77 to point back to and no lopsided symbol counts for Huffman coding to exploit. Zipping them saves next to nothing, and can even add a few bytes.
Can a compressor make every file smaller?
No. There are simply fewer short files than long ones, so a lossless compressor that made some files shorter must make others longer, otherwise two different files would have to share the same compressed version. On average, truly random data can’t be compressed at all.
Sources
- RFC 1951: DEFLATE Compressed Data Format Specification (May 1996)
- PKWARE: ZIP File Format Specification (APPNOTE 6.3.9), section 4.4.5
- Wikipedia: ZIP (file format)
- Ziv and Lempel: A Universal Algorithm for Sequential Data Compression (IEEE Transactions on Information Theory, 1977)
- Huffman: A Method for the Construction of Minimum-Redundancy Codes (Proceedings of the IRE, 1952)
- Wikipedia: Huffman coding (history, including Gary Stix, Scientific American, September 1991)
- Wikipedia: Prefix code
- Wikipedia: Lossless compression, limitations
- arXiv 2602.19626, Table 2 (alice29.txt from the Canterbury Corpus with gzip -9)
Last updated
Keep exploring

How does a JPEG make photos so small?
Drag a quality slider on a real photo and watch the blocks appear.
How does autocorrect know what you meant?
Type a misspelling and watch the candidate words line up by how common they are.
How does Google search so fast?
Search real books two ways and watch the timer.See all the exhibitsSubscribe on YouTube (opens in a new tab)