This article is free to read. Creating a lesson uses your daily or stored credits; review the language and settings before starting.
How does file compression work?
Quick answer
File compression shrinks data by spotting repeated patterns and encoding them with shorter symbols. Lossless methods, such as ZIP, rebuild the original exactly and suit text and code. Lossy methods, such as JPEG and MP3, discard imperceptible detail for much smaller files. A decoder reverses the encoding when the file is opened.
File compression works by finding repeated patterns in data and replacing them with shorter codes, so the same information takes up fewer bytes. A decoder later reverses this mapping to rebuild the original file exactly, or close enough for images and sound.
Step by step
- The compressor scans the file and builds a map of which byte values or sequences appear most often.
- Frequent symbols are reassigned short binary codes, while rare symbols receive longer codes, shrinking the total bit count.
- Repeated strings are replaced by references pointing to an earlier copy of the same string, avoiding duplicate storage.
- The compressed stream is written with a header describing the code table or dictionary needed for decoding.
- A decoder reads the header, rebuilds the code table, and expands each short code back into its original symbol.
- For lossy formats, a quantisation step discards fine detail before the remaining data is encoded.
Key things to understand
- Compression removes redundancy by representing repeated patterns with shorter symbols instead of storing every byte literally.
- Lossless compression preserves every bit, so the decompressed file is byte-for-byte identical to the original.
- Lossy compression discards detail the human eye or ear is unlikely to notice, achieving far smaller files.
- Algorithms like Huffman coding assign shorter codes to frequent symbols and longer codes to rare ones.
- LZ77 and LZ78 find repeated sequences and replace them with references pointing back to earlier occurrences.
Labelled diagram
- Original file
Raw bytes containing repeated words, colours or sound samples.
- Pattern scan
Algorithm counts how often each symbol or sequence appears.
- Encode
Frequent items get short codes; repeats become back-references.
- Compressed file
Smaller stream plus a header describing the code table.
- Decode
Decoder reverses the mapping to rebuild the original data.
Real-world examples
- Sending a large spreadsheet by email often means zipping it first, since text and numbers compress well.
- Streaming services compress video with lossy codecs so a two-hour film fits through a home internet connection.
- PNG images use lossless compression, which is why screenshots and logos stay sharp but file sizes remain moderate.
- MP3 players shrink music by removing frequencies most listeners cannot hear, cutting file size to roughly a tenth.
Common mistake
✗ Myth: Compressing a file always makes it smaller, and you can keep compressing to shrink it further.
✓ Fact: Compression only works when the data contains redundancy. Already-compressed or encrypted files have little redundancy left, so a second pass usually saves nothing and can add overhead.
People also ask
How does zip compression work?
Why can't you compress an already compressed file?
What is the difference between lossy and lossless compression?
What is the difference between lossless and lossy compression?
Why do some files not shrink much when compressed?
What is a ZIP file?
Does compressing a file reduce its quality?
Part of the guide: How the Internet Works →

