35Technology

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.

On the table

Packing a suitcase

A file is a long row of bits, which are zeros and ones. To shrink it without losing anything, you find a way to write the same row in fewer bits, so that the original can be built back exactly. This is lossless compression, and it is what a zip file does.

Three ideas do most of the work. Write a long run of the same thing once, with a count. Write a piece that has appeared before as a pointer back to it. Give common letters short codes. Try each on a small example. Then see why nothing can shrink everything. The sizes are counted by small toy versions, not by a real zip program. Without a script each part shows one table.

Pack one

Runs of the same

A row of black and white dots can be written as a count of each run: 8 black, 8 white, and so on. A row with long runs gets short. Click the dots to change the row, or pick a row.

In this toy a plain row costs 1 bit a dot, and the run version costs 1 bit for the first colour plus 6 bits for each run. Try the checker row. Counting runs makes it much longer than the plain row.

The row has 4 runs: 8 black, 8 white, 8 black, 8 white. Plain it takes 32 bits. As runs it takes 25 bits.

RowPlainAs runs
stripes32 bits25 bits
block32 bits19 bits
checker32 bits193 bits
noise32 bits97 bits

Pack two

Say it once, point back

Text repeats itself. When a piece has appeared before, the compressor writes where it was and how long it is, instead of writing it again. Two numbers stand in for the whole piece. This idea is called LZ77, and it is one half of what a zip file does.

Pick a text or type one. Letters in the output are kept as they are. A pointer is shown as how far back to go and how many letters to copy. A letter costs 9 bits in this toy and a pointer costs 13.

the_cat_s<4,3>on_<15,4>m<15,3>and<12,5>dog<27,12>log

The text has 49 letters, which is 392 bits. Written with pointers it has 27 pieces and takes 263 bits. A pointer shows how far back to go and how many letters to copy, and an underscore is a space.

Pack three

Short codes for common letters

A plain letter takes 8 bits. But some letters are far more common than others. Give the common ones short codes and the rare ones long codes, and the text gets shorter. David Huffman found the best way to do this in 1952. The two rarest letters are joined first, and joined again and again until one tree is left.

The codes are made so that no code is the start of another one, so a row of bits can be read without gaps. Type a text and see the table. The table must be stored too, so a tiny text does not shrink.

LetterCountCode
space1201
t8111
a5001
e41100
h41101
o4000
n31010
d210110
g210111
s21000
c1100110
l1100111
m110010

Plain the text takes 392 bits. With these codes the letters take 164 bits, and the table takes 156 bits more, so 320 bits in all.

  1. Join c (1) and l (1) into one of 2.
  2. Join m (1) and cl (2) into one of 3.
  3. Join d (2) and g (2) into one of 4.
  4. Join s (2) and mcl (3) into one of 5.

Pack four

What cannot shrink

No method shrinks everything. There are only so many short messages. If you could shrink every message of 10 bits, you would need 1,024 different shorter messages, but there are only 1,023 of them. Two messages would have to share one, and then you could not tell them apart.

So compressors work by finding patterns. Pick a text and compare the three ideas with the plain size. Text with no pattern, like random letters, does not shrink, and the table can even make it bigger.

Size in bits of the chosen text with each method
MethodBitsAgainst plain
Plain letters392100 per cent
Run lengths784200 per cent
Repeats26367 per cent
Short codes, with the table32082 per cent

A sentence: the plain text takes 392 bits. Counting messages: there are 1,024 messages of exactly 10 bits but only 1,023 shorter ones, so they cannot all shrink.

Key takeaways

  1. Lossless compression writes the same data in fewer bits, so that the original can be built back exactly.
  2. A run of the same thing can be written once with a count. This helps only when there are long runs.
  3. A piece of text that has appeared before can be replaced by a pointer back to it.
  4. Common letters can get short codes and rare letters long ones. The codes must not start with each other.
  5. 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.