2.3.2 Data compression
Why Compress Data?
Data compression reduces the size of a file by encoding its contents more efficiently. There are two main reasons to compress data:
- Storage: compressed files occupy less disk space, allowing more files to be stored on a device.
- Transmission: smaller files take less time to upload or download and consume less bandwidth — important for streaming, email attachments and web content.
Lossless Compression
In lossless compression, no data is permanently removed. The original file can be perfectly reconstructed from the compressed version — decompression restores the exact original content, bit for bit.
How it works (simplified): repeated patterns or sequences in the data are encoded more efficiently. For example, a sequence of 50 identical white pixels stored as 50 × 24-bit values can be encoded as "50 × white" — a far more compact representation. This technique is called run-length encoding (RLE).
Common formats: PNG (images), ZIP/DEFLATE (general files), FLAC (audio), GIF (for simple graphics).
When to use: any situation where the original data must be recovered exactly — text files, program executables, databases, medical images, legal documents.
Example: run-length encoding
Original: WWWWWWWWWWWWBBBBBWWWWWWWWWWWWBBB
RLE encoded: 12W 5B 12W 3B
32 characters reduced to 8 characters — a 75% reduction (in this simplified representation).
Lossy Compression
In lossy compression, some data is permanently discarded. The decompressed file is an approximation of the original — the missing data cannot be recovered. This allows much higher compression ratios than lossless, but at the cost of quality.
How it works (simplified): information that humans are least sensitive to is discarded. For images, subtle colour gradations invisible to the eye may be simplified. For audio, sounds outside the range of human hearing or masked by louder sounds are removed (perceptual coding). The level of compression can often be controlled — more aggressive compression reduces file size more but reduces quality more.
Common formats: JPEG (photographs), MP3/AAC (audio), MPEG/H.264 (video).
When to use: media files where minor quality loss is acceptable — web photographs, music streaming, video streaming. Not suitable for text, programs or databases.
| Lossless | Lossy | |
|---|---|---|
| Data recovered? | Exactly — perfect reconstruction | No — some data permanently lost |
| File size reduction | Moderate (2:1 to 5:1 typical) | High (10:1 to 100:1 for media) |
| Quality after decompress | Identical to original | Approximation — visible/audible artefacts at high compression |
| Formats | PNG, ZIP, FLAC | JPEG, MP3, MPEG |
| Use for | Text, programs, medical images | Photos, music, video for distribution |
Key Takeaways
- Compression reduces file size for storage efficiency and faster transmission.
- Lossless: no data lost — original perfectly recoverable. Formats: PNG, ZIP, FLAC.
- Lossy: data permanently discarded — smaller files, lower quality. Formats: JPEG, MP3, MPEG.
- Use lossless for text, programs, databases; lossy for photos, music and video where small quality loss is acceptable.