Image, Sound and Media

How Compression Shrinks Files

Accordion file folders holding organized documents in compartments
Photo: Anete Lusina via Pexels. Image credits

A file is a sequence of bits, and most sequences that people care about are wasteful. English text repeats letters and words. A photograph has long stretches where neighboring pixels are nearly the same shade. A spreadsheet holds columns of similar numbers. Compression exploits that predictability: it finds a shorter description of the same content, then reverses the process on demand. The reason it works at all, and the reason it sometimes does not, comes from information theory.

In 1948, Claude Shannon showed that the average information in a message is limited by its unpredictability, a quantity called entropy. A stream in which every symbol is equally likely and independent of the others cannot be shortened on average. A stream full of patterns has low entropy, and the gap between its actual size and its entropy is redundancy, which is exactly what a compressor removes.

Two Families: Lossless and Lossy

Lossless compression restores every bit exactly. It is essential for text, program code, documents, and any data where a single changed digit would matter. ZIP archives, PNG images, and FLAC audio are lossless formats.

Lossy compression accepts that the restored data will differ from the original, and uses knowledge about human perception to decide what can go. JPEG images, MP3 and AAC audio, and most video formats are lossy. The result is much smaller, but the discarded detail cannot be recovered, and repeated saving can compound the damage.

How Lossless Methods Work

Two ideas dominate, and the DEFLATE format combines both. It is used in ZIP files, gzip, and PNG images. It was specified by Phil Katz and documented in RFC 1951 in 1996.

The first idea is to replace repetition with a reference. When a sequence has appeared recently, the encoder writes a short pointer of the form "go back this far and copy this many bytes." This family of methods traces back to a 1977 paper by Abraham Lempel and Jacob Ziv and is called LZ77. In DEFLATE, the pointer can reach back up to 32,768 bytes, the sliding window, and copy up to 258 bytes. A long repeated phrase in a document becomes a couple of numbers. The decoder needs no dictionary: it rebuilds the text by copying from what it has already produced.

The second idea is to give common symbols short codes and rare symbols long codes. David Huffman published a method in 1952 that builds the best possible set of such codes for a given set of frequencies. Imagine assigning the shortest Morse-like sequence to the letter you use most. The codes are constructed so that no code is the beginning of another, which lets the decoder split the stream without separators. In DEFLATE, the literal bytes and the pointer lengths and distances are both coded this way, with the code tables stored in the file.

Preparing Data to Compress Better

Cleverness often goes into preparation. PNG, for example, applies a filter to each row of pixels before compressing. The filter replaces each pixel with its difference from a neighbor, so a smooth gradient turns into a run of small, repeated values that DEFLATE handles well. The specification explains that filtering can improve compressibility, and the process remains fully lossless: the decoder applies the reverse filter after decompressing.

How Lossy Methods Work

JPEG, standardized as ISO/IEC 10918 in the early 1990s, shows the typical pipeline. The image is first converted from red, green, and blue into a brightness channel and two color channels, because the eye is far more sensitive to brightness detail than to color detail. The color channels are usually stored at reduced resolution. Each channel is divided into blocks of 8 by 8 pixels, and a mathematical operation called the discrete cosine transform converts each block from pixel values into a set of spatial frequencies, from smooth gradients to fine texture.

Then comes the step where the loss happens. Quantization divides each frequency value by a number and rounds the result, and the divisors are larger for high frequencies. Many fine-texture values round to zero. The result is passed through Huffman coding, which handles long runs of zeros very compactly. Audio codecs follow a similar philosophy, using models of hearing to discard sounds that would be masked by louder ones nearby, a use of human perception that has an interesting counterpart in noise-cancelling headphones, which attack the problem from the physics side instead.

A useful way to picture the trade-off is a set of moving instructions. A lossless method is like packing a house into boxes that can be unpacked into exactly the same rooms. A lossy method is like a description of the house written for a stranger: it conveys the layout and feel, but the exact position of every book is gone. Which one is appropriate depends entirely on what the receiver needs to do with the result.

Why Some Files Refuse to Shrink

There is a hard limit that no cleverness overcomes. Counting shows that there are fewer short bit strings than long ones: only 256 distinct files fit in one byte, but far more in two. If a compressor made every input smaller, two different inputs would have to map to the same output, and decompression could not tell them apart. So any lossless compressor that shortens some files must leave others the same size or slightly larger.

In practice, data that is already random or already compressed has no redundancy left to remove. Compressing a ZIP file or a JPEG a second time achieves little, and the same is true of well-encrypted data, which is designed to look random, a point relevant to how encryption protects information. This is why the order matters: compress first, then encrypt, never the reverse.

Everyday Effects

Compression is quietly responsible for much of modern media. A digital camera sensor produces a large amount of raw data for every frame; the file format is what makes storing and sharing photos practical. Web pages are sent compressed. The same is true of much of the material held in cloud storage, where smaller files also mean cheaper transfers.

A common misconception is that a higher compression setting always means a better result. With lossy formats it means a trade: smaller files, more visible blocking around sharp edges, and a smeared texture in flat areas. Another is that "zipping" a file protects it. Compression hides nothing; anyone with a standard tool can open the archive.

In Short

Compression shortens files by removing redundancy: pointers replace repeats, and shorter codes replace common symbols. Lossless methods such as DEFLATE reproduce every bit, while lossy methods such as JPEG discard detail that people are least likely to miss. Because short descriptions are scarce, no method can shrink everything, and data that has already been squeezed, or is genuinely random, has nothing left to give.

Test what you learned

Three quick questions on this article. For the full experience, play the quiz on this topic.

1. What do lossless compression methods guarantee?

2. Which pair of techniques does the DEFLATE format combine?

3. Why is a JPEG of a photograph usually much smaller than a PNG of the same photograph?

Ready for more?

Play the quiz on this topic and see the explanation behind every answer.

Play the 6-question quiz

Sources

How we choose and check sources: Sources and methodology.

Keep exploring

Hand holding a QR code up to a reader in front of a wall of colorful posters
Image, Sound and Media

How a QR Code Stores Information

A QR code is a tiny map of black and white squares that carries text, a layout for finding itself, and enough redundancy to survive scratches and smudges.

5 min read 6 quiz questions
Hand holding a camera body without a lens, exposing the image sensor
Image, Sound and Media

How Digital Camera Sensors Capture Images

A camera sensor is a grid of tiny light meters. Each one counts photons through a color filter, and software rebuilds a full-color picture from those counts.

5 min read 6 quiz questions
Dark over-ear headphones resting on a light wooden surface
Image, Sound and Media

How Noise-Cancelling Headphones Reduce Sound

Active noise cancellation measures unwanted sound, plays its mirror image through a tiny speaker, and lets the two waves cancel each other near your ear.

5 min read 6 quiz questions
Rows of servers lit in blue inside a data center
Internet and Connectivity

What Storing Files in the Cloud Really Means

The cloud is not a place in the sky. It is rented capacity in data centers, with copies, syncing, and access controls. Here is what actually happens to your files.

5 min read 7 quiz questions