Open a compressed file in a text viewer and it looks like noise, yet the original comes back intact. How does a program make a file smaller without losing anything? It finds what is said more than once and says it only once.

This guide covers the three kinds of repetition a compressor hunts for: runs, repeated strings and neighbors that are close in value. Each comes with a small example you can check by hand, so you can see where the savings come from and why the same trick can backfire.

Redundancy: the predictable part

Information theory has a name for the repeated, predictable part of a message: redundancy. In his 1948 paper, Claude Shannon estimated that ordinary English, looking at structure up to about eight letters, is roughly 50% redundant. He put it this way: half of what is written is determined by the structure of the language, and half is chosen freely.

A lossless compressor aims at the first half. It rewrites the predictable part more briefly and keeps a recipe for rebuilding it, so nothing is lost. The free half cannot be shortened this way, so a file with little predictable structure has little to give.

Four patterns a compressor hunts for

Pattern What it looks like Method that uses it
Runs One value repeated many times in a row Run-length coding
Uneven frequencies Some symbols common, others rare Huffman coding
Repeated strings A phrase that appeared earlier appears again Back-references (LZ77-style)
Close neighbors Each value is near the one before it Filtering, then one of the methods above

Real formats can combine these. The next sections take runs, repeated strings and neighbors one at a time.

Runs, and the case where counting backfires

Run-length coding stores a value and how many times it repeats. A university algorithms course shows a line of 40 bits made of 15 zeros, 7 ones, 7 zeros and 11 ones. Stored as four 4-bit counts, it takes 16 bits instead of 40.

Worked example. Example scheme: one byte for the value and two bytes for the run length, so every run costs 3 bytes. A block of 1,000 identical bytes becomes one run: 3 bytes. Ratio: 1,000 ÷ 3 = 333.3:1. Percent saved: (1 − 3 ÷ 1,000) × 100 = 99.7%. Now take 1,000 bytes that alternate A, B, A, B. That is 1,000 runs of length 1, so the output is 3 × 1,000 = 3,000 bytes. Ratio: 1,000 ÷ 3,000 = 0.33:1. The file tripled.

The same method wins big on one file and loses on another. Paste the two pairs of sizes into the compression ratio calculator to see both results. The lesson is that a method must match the pattern in the data. DEFLATE, for example, combines two ideas instead of relying on one.

Repeated strings: point back instead of copying

Text repeats whole phrases. The DEFLATE format, specified in RFC 1951 in 1996, handles this with two kinds of items: literal bytes, and pointers written as a pair of length and backward distance. A pointer means "copy this many bytes from this far back in the output." The format limits lengths to 3 through 258 bytes and distances to 1 through 32,768 bytes.

Take "to be or not to be", 18 characters. The first 13 characters, "to be or not ", have no earlier copy, so they stay as 13 literals. The last five, "to be", match the start. One pointer with length 5 and distance 13 covers them. The 18 characters become 14 items. That counts items, not bits, because the bit cost depends on how items are encoded.

A pointer may even overlap its own output. RFC 1951 gives the example of the last two decoded bytes being X and Y, where a pointer with length 5 and distance 2 adds X, Y, X, Y, X. So "ab" followed by one pointer with length 10 and distance 2 produces "abababababab": 12 characters from 3 items.

Because lengths top out at 258, a long repeat needs several pointers. Example: "ab" repeated 1,000 times is 2,000 characters. After the two literals, 1,998 characters remain. That takes 7 pointers of 258 (1,806 characters) and one of 192, so 10 items in total. For how these items are then packed into bits, see how DEFLATE works.

Neighbors that are close, not equal

Some data, like a smooth gradient in an image, never repeats exactly but changes slowly. PNG handles this with filtering before compression. The PNG specification says filtering can improve compressibility and that the operation is deterministic, reversible and lossless.

Its Sub filter replaces each byte with the difference from the corresponding byte in the pixel to the left. For the first pixel in a row, the left neighbor counts as zero. Example: a row of single-byte pixels with values 100, 101, 102 and on up to 109 becomes 100, 1, 1, 1, 1, 1, 1, 1, 1, 1. Ten different numbers turned into one number followed by a run of nine 1s, which the earlier methods handle well.

Common mistakes

  • Thinking compression needs exact duplicates. Runs, repeated strings and near-equal neighbors are three different kinds of pattern. Filtering is one way to make the third kind easier for the other methods to use.
  • Assuming one method fits all data. The 1,000-byte alternating example tripled in size. A method that suits long runs can hurt data without them.
  • Expecting short inputs to shrink a lot. A pointer needs earlier text to point at. In "to be or not to be," the first 13 characters had nothing to match.
  • Counting items as bytes. Pointers and literals still have to be written in bits. The saving is only real after the encoding step.
  • Believing any file can be squeezed by repeating the process. The second pass finds little left to remove. See why some files barely shrink.

For the overall picture, start with what data compression is.

FAQ

How does file compression work in simple terms?

A compressor looks for repeated or predictable parts of the data and writes each one more briefly. A decompressor follows the same rules backward to rebuild the original exactly.

Why does text compress well?

Text repeats letters, words and phrases. Shannon estimated that ordinary English is roughly 50% redundant, which leaves a lot of room to shorten it.

Can compression make a file bigger?

Yes. The alternating-byte example above grew from 1,000 to 3,000 bytes. Well-designed formats limit the damage, but no lossless method can shrink every input.

Is the saving always the same for the same file type?

No. It depends on how much repetition the specific file contains, so two files of the same type can shrink by very different amounts.

Sources

  1. Bell System Technical Journal (reprint hosted by Harvard University), A Mathematical Theory of Communication, C. E. Shannon, 1948. Accessed November 6, 2026.
  2. Princeton University, Sedgewick and Wayne, 5.5 Data Compression (Algorithms lecture slides). Accessed November 6, 2026.
  3. IETF, RFC 1951: DEFLATE Compressed Data Format Specification version 1.3. Accessed November 6, 2026.
  4. W3C, Portable Network Graphics (PNG) Specification (Third Edition). Accessed November 6, 2026.