Learning Center · Compression · Beginner

How file compression works

See how lossless compression shrinks files with run-length encoding, Huffman coding and LZ77 pointers, and why ZIP, gzip and PNG all rely on DEFLATE.

By listme.name Updated 5 min read 1:24 video Share

1:24 video with captions. The text below has the detail and the sources.
Read the video transcript

The puzzle

Open a zip file and every byte comes back exactly as it was. So where did the space go?

Idea one: describe the run

Files are full of repetition. The simplest trick writes a run as a count and a symbol. Seven A's, three B's, eight C's, six D's. Twenty-four characters become eight. But text with no runs doubles. No method helps every input.

Idea two: shorter codes for common symbols

Next idea: short codes for common symbols. Abracadabra takes eighty-eight bits, at eight per letter. Count the letters. Join the two rarest, again and again, until one tree remains. Left is zero, right is one. A gets one bit, and the word now takes twenty-three.

Idea three: point back

Third idea: never repeat text you already wrote. Write a pointer instead: go back this far, copy this many. Green pointers replace whole stretches. Thirty-five characters become twenty pieces.

How ZIP combines them

Real tools stack these ideas. ZIP's DEFLATE swaps repeats for pointers, then gives the results short Huffman codes. gzip and PNG use the same recipe.

What to remember

Compression is a bet on patterns. Without them, nothing shrinks. Read the full guide for details.

File compression makes data smaller by writing repeated or predictable parts of it more briefly. A zip file is a good example: open it and every byte of the original comes back exactly, even though the archive took less room. That works because real files contain far more repetition than they need to, and compression replaces the repetition with something shorter and a rule for expanding it again.

Three ideas do most of the work in everyday tools. The video above shows them in about 85 seconds; this page adds the detail and the limits.

What does compression actually remove?

Compression removes redundancy, the part of a file that says something the rest of the file already says. A blank stretch of an image repeats the same bytes. English text repeats words such as "the" and uses the letter e far more often than the letter z. A log file repeats the same line shapes with different numbers.

A compressor finds redundancy and encodes it more efficiently. A decompressor reverses the encoding. When the round trip gives back the original exactly, the method is lossless. When the method throws away detail a person is unlikely to notice, as JPEG does for photos and MP3 does for audio, it is lossy. This lesson covers lossless methods; lossless vs lossy compression covers the other kind.

Idea one: describe a run

The simplest scheme is run-length encoding. When the same symbol repeats, write the count and the symbol once. The 24 characters AAAAAAABBBCCCCCCCCDDDDDD become 7A 3B 8C 6D, which is 8 characters. Early fax machines and simple bitmap formats rely on this idea, because a page of mostly white pixels is mostly long runs.

Run-length encoding also shows the first limit of compression. Feed it ABCDEF, which has no runs, and the encoded form 1A1B1C1D1E1F is twice as long. Every method has inputs it cannot shrink, and a good compressor checks whether its output is smaller before it uses it.

Idea two: give common symbols shorter codes

Plain text normally spends 8 bits on every character, whether the character is common or rare. Huffman coding, published by David Huffman in 1952, gives frequent symbols short codes and rare symbols long ones, with no code being the start of another so the result can be read back without separators.

Take the word ABRACADABRA, 11 characters. At 8 bits each it needs 88 bits. The letter A appears five times, B and R twice each, C and D once each. Huffman's method repeatedly joins the two rarest items into one node until a single tree remains, then reads each letter's code from the path to it: left is 0, right is 1. A gets the one-bit code 0, and the other four letters get three bits each. The same word now takes 23 bits and decodes back to exactly the same letters.

The lesson Huffman coding explained builds the tree step by step.

Idea three: point back instead of repeating

Text repeats whole phrases, not just letters. LZ77, described by Abraham Lempel and Jacob Ziv in 1977, handles this by keeping a window of the data already written. When the next stretch matches something in the window, the compressor writes a pointer: go back this many characters and copy this many. In the sentence the cat sat on the mat. the cat sat, 35 characters, the encoder writes 16 literal characters and 4 pointers, 20 pieces in total. The last pointer alone replaces cat sat with "back 24, copy 7".

On long files the pointers cover a large share of the data, which is why this idea gives most of the savings on text and code. The lesson LZ77 and DEFLATE: how ZIP works walks through the matching.

How ZIP, gzip and PNG combine the ideas

Real tools stack these ideas. The DEFLATE format, specified in RFC 1951 in 1996, compresses with a combination of the LZ77 algorithm and Huffman coding: LZ77 first turns repeats into pointers, then Huffman codes make the resulting literals and pointers shorter. ZIP archives most often store files with DEFLATE, gzip uses it, and so does the compression inside PNG images. That shared recipe is why the same format shows up in so many places.

Newer algorithms such as xz, Zstandard and Brotli use the same building blocks with larger windows and better models, which buys smaller output or more speed. Choosing a compression algorithm compares them for common jobs.

Why do some files not shrink?

Compression needs patterns. Three kinds of files give it very little to work with:

  • Already compressed files. JPEG photos, MP3 and AAC audio, and H.264 video have had their redundancy removed. Putting them in a ZIP usually saves little, though the archive can still bundle many files into one.
  • Encrypted data. Good encryption makes data look random, and random data has no redundancy. Compress first and encrypt afterwards, never the other way round.
  • Very small or already random files. A short file has little to repeat, and the format's own headers can outweigh the savings.

There is also a counting argument behind this. No method can map every possible input to a shorter output, because there are fewer short strings than long ones. That is why DEFLATE, for one, has a mode that stores a block unchanged when compressing it would not help.

Which compression tool should you use?

For everyday files, the format matters more than the algorithm. ZIP opens everywhere, 7z usually gives smaller archives, and RAR needs specific software to create. The file archivers ranking compares tools for Windows, macOS and Linux by format support, platform reach, license and encryption, and the guide how to choose a file archiver explains the trade-offs in plain terms.

Sources