Stewdent.eu
▾

Compression

Compression reduces the size of files by encoding the data more efficiently. There are two main types: lossless compression, which preserves every bit of data, and lossy compression, which permanently removes some data to achieve smaller sizes.

Last updated: 13 February 2026

★★☆☆☆ Level 2computingDatacompressionlosslesslossyZIPJPEG4 min read

Report an error

A single high-resolution photograph can be dozens of megabytes. A feature-length film in raw format can be hundreds of gigabytes. Without compression, streaming video would be impossible, email attachments would fill up inboxes in seconds, and hard drives would be exhausted in days. Compression is one of the most practical and widely used ideas in computing.

Why Compress?

Compression reduces file size by finding and eliminating redundancy in the data. Smaller files:

  • Take less storage space
  • Transfer faster over a network
  • Use less bandwidth when streaming
  • Are cheaper to store in the cloud

The trade-off is that compression and decompression take time and processing power — though modern hardware handles this almost instantly for most everyday tasks.

Lossless Compression

Lossless compression reduces file size while preserving every single bit of the original data. When you decompress the file, you get back an exact copy of the original.

Run-Length Encoding (RLE)

One of the simplest lossless techniques is run-length encoding. Instead of repeating a value many times, you store how many times it appears.

Example: the string AAAABBBCCCCCDDAA can be encoded as 4A3B5C2D2A — storing the count and the value rather than each individual character. The original is 16 characters; the encoded version is 10.

ZIP Compression

The ZIP format uses the DEFLATE algorithm, which combines two techniques:

  1. LZ77 — finds repeated sequences of bytes and replaces later occurrences with a reference to the first occurrence.
  2. Huffman coding — assigns shorter bit patterns to the most common characters, and longer ones to rare characters (similar to Morse code).
Note

Text files, program code, and CSV files compress very well with lossless algorithms — often to 10–30% of their original size. Already-compressed files (like JPEG or MP3) show little further reduction.

Lossy Compression

Lossy compression achieves much smaller file sizes by permanently discarding data that is considered less important — usually information the human senses struggle to detect.

JPEG Image Compression

JPEG compression works by:

  1. Converting the image from RGB colour space to a different space that separates brightness from colour.
  2. Reducing the precision of colour information (humans are more sensitive to brightness than colour).
  3. Applying a mathematical transform (DCT — Discrete Cosine Transform) to blocks of 8x8 pixels.
  4. Discarding high-frequency detail (fine textures) that the eye barely notices.
  5. Applying lossless compression to the remaining data.

At high quality settings, a JPEG looks almost identical to the original. At low quality, you see blocky artefacts around edges.

Warning

Every time you save a JPEG, more data is lost. If you need to edit an image repeatedly, always keep the original in a lossless format (PNG or RAW) and only export to JPEG as a final step.

MP3 Audio Compression

MP3 uses perceptual coding — it exploits the limits of human hearing to discard sounds we cannot easily perceive:

  • Very quiet sounds that occur at the same time as loud sounds (masking)
  • Frequencies above the range of human hearing
  • Subtle nuances in less perceptually important frequency bands

A typical MP3 file is roughly one-tenth the size of the equivalent uncompressed WAV, with only slight quality loss at standard bitrates.

Lossless vs Lossy: Choosing the Right Type

LosslessLossy
Data preserved?100% — exact originalNo — some data permanently removed
File size reductionModerate (good for text)Large (good for media)
Common formatsZIP, PNG, FLACJPEG, MP3, MP4, AAC
Best forDocuments, code, archivesPhotos, audio, video for distribution
Tip

As a rule of thumb: use lossless compression when accuracy matters (contracts, medical images, software), and lossy compression when a good-enough quality is acceptable and file size is important (photos shared online, streaming music).

Practice Problems

A student uses run-length encoding on the string 'BBBBBAAAACCCC'. What is the encoded result?

Show answer
5B4A4C

Explain why JPEG compression is described as lossy.

Show answer
JPEG permanently removes some image data — particularly fine colour detail and high-frequency texture information — that cannot be recovered later. When you decompress a JPEG, you get an approximation of the original, not an exact copy.