How files shrink
A zip file holds the same data in fewer bits. Pack a row of dots into runs, replace repeated text with pointers, give common letters short codes, and see why no method can shrink every file.
Key takeaways
- Lossless compression writes the same data in fewer bits, so that the original can be built back exactly.
- A run of the same thing can be written once with a count. This helps only when there are long runs.
- A piece of text that has appeared before can be replaced by a pointer back to it.
- Common letters can get short codes and rare letters long ones. The codes must not start with each other.
- Nothing shrinks every file. Data with no pattern does not shrink, and the sizes on this page come from toy versions.
Sources
Most of these sites could not be opened while this page was written, so the facts were checked through search results that quote them. Huffman coding as a lossless method that gives common symbols shorter codes, the prefix property, the tree built by joining the two rarest symbols, David Huffman’s 1952 paper from his time at MIT, DEFLATE as a mix of LZ77 repeats and Huffman codes in ZIP and PNG, and the difference between lossless (ZIP, PNG) and lossy (JPEG) compression each appeared in at least two places. Sources differ on whether Huffman was a doctoral student working for an Sc.D. or a Ph.D., so the page only says MIT. The bit counts (1 bit and 6 bits for runs, 9 and 13 bits for repeats, 12 bits for each entry of the code table) and the example texts are made up for the toy, and the page counts what its own toy gives. Real programs differ.
- Wikipedia: Huffman codingThe method, the tree and the 1952 paper.
- Huffman: a method for minimum redundancy codes (1952)The original paper.
- Moffat: Huffman coding (University of Melbourne)A survey of the method.
- PlanetCalc: Huffman codingThe prefix property and the tree.
- Essential books: Huffman codingBuilding the codes.
- DEV: lossless and lossy compressionDEFLATE and the two kinds of compression.
- GIS Geography: lossless and lossy compressionZIP and PNG against JPEG.
- University of Utah: compression (slides)Repeats and sliding windows.