← Back to list

File Compression — Martin Stellinga

Ever wonder how you compress files? What zip file compression is, or why jpg files are smaller than bmp? Let’s dig in.

Martin Stellinga · 2026-02-13 09:12 · 0 claps · 3.3 min read
#file-compression #science
Open on Medium ↗
Wiki topics: 🔬 · Science · General 📰 · Journalism & News

File Compression — Martin Stellinga

Ever wonder how you compress files? What zip file compression is, or why jpg files are smaller than bmp? Let’s dig in.

Encoding and frequency

Compression works by replacing sequences of data occurring multiple times by a code that is shorter than the replaced data. Well, one form of compression. I’ll talk about another later.

So, that was a mouthful. How does this replacing-by-a-code work? Let’s look at an example.

Take the following quote:

one small step for man, one giant leap for mankind.

- Neil Armstrong

So, there are some words in there that occur multiple times: ‘one’ and ‘for’.

Alright, let’s replace those by a code (1=one and 2=for).

1 small step 2 man, 1 giant leap 2 mankind.

Cool. From 51 to 43 characters. Oh, wait. Whoever decompresses the sentence will need to know the codes we used. Normally, you add those to the compressed data. We call that a dictionary. Like this:

1one|2for|1 small step 2 man, 1 giant leap 2 mankind.

Oh… By adding that dictionary we end up with 53 characters instead of 51. That’s actually bigger than the original.

Now what?

Well, our replacement was a bit naive, because we used word boundaries. We can do better. Check this out:

1one |2p for man|1small ste2, 1giant lea2kind.

For the first code, we added the space after ‘one’. And the second code is the interesting one: ‘p for man’. Now that is starting to save some space. Now we’re at a 51 to 46 characters compression, which is not great, but it is down to 90% of the original size.

From this example, I hope you can see that frequency of repeating data matters. Longer texts usually have a lot of repetition (like the word ‘the’), so that compresses quite well, even with a dictionary.

Files, Images, and Music

When you compress files regardless of content, you usually end up with one of the general purpose compression methods like ‘ zip’ or ‘ rar ‘. Those use the techniques described above on the byte level to compress a set of files. Depending on how much repeated patterns there are in the files, the compression ratio is better.

For images, encoding works similarly. A plain bitmap (bmp) file simply encodes each pixel in an image as three bytes indicating the strength of red, green, and blue in the image. By using an encoding scheme as described above called RLE (Run-Length-Encoding), this can be shortened. A more clever compression scheme yields even smaller files in the form of png files.

For music, the plain encoding format is the wav file, or Waveform Audio Format. This encodes two audio channels waveform in bytes 44100 times per second. The format is the basis of the CD (Compact Disc, remember those?). More clever encoding has resulted in the FLAC (Free Losless Audio Codec) format.

But, there is a way to get more mileage out of your bytes. If you accept a downside.

Lossless vs Lossy

As you can imagine, images, video, and audio files will all compress better with repeating patterns. So far, so good.

One way to improve compression further is by using so-called lossy compression. We’ve looked at compression where the original could be reconstructed exactly. By allowing a slightly distorted original bit, you can improve compression.

Look at this (pretty weird) example:

1one |2ep for man|1small st2, 1giant l2kind.

We reduced it to 44 characters! How did we… wait… When you decode it, it now has a typo! It’s not a ‘giant lep’, but a ‘giant le a p’.

That is lossy compression. You deliberately allow errors to slip in to allow for more compression.

In text that’s a pretty stupid thing to do. However in images (jpg), music files (mo3), and video (mp4), it’s far more acceptable. The human eye can’t see subtle errors, and our ears don’t hear minute distortions in high or low frequencies.

Of course, we’ve all seen over-compressed blurry jpegs, and heard washed out songs, so you won’t be surprised there’s a limit to this: the more you compress, the more errors.

In the end, there is only so much information you can store in a byte: quite literally only 256 different values. So lossy means just that. You lose data in favor of better compression. Of course, the opposite is also true, lossless costs more space. That’s why a bitmap (lossless) file is far bigger than a jpg (lossy), and a FLAC music file (lossless) is so much larger than an mp3 (lossy).

Summary

Compression is a combination of clever math, and the willingness to accept some loss of data. Like so many things in software, it’s a trade off. Clever math takes processing power, and lossy means lost data.

But in an effort to send data across the internet faster, or save storage space, it can definitely be worth it.

Originally published at https://martinstellinga.com on February 13, 2026.


메타데이터
post_id
ab030aa748e6
slug
file-compression-martin-stellinga-ab030aa748e6
url
https://medium.com/@martin_94956/file-compression-martin-stellinga-ab030aa748e6
canonical_url
https://medium.com/@martin_94956/file-compression-martin-stellinga-ab030aa748e6
author_url
https://medium.com/@martin_94956
status
ok
fetched_at
2026-07-21 10:43:05