HomeHow ToHow file compression works

How file compression works

File compression is a fundamental part of how the web works. It allows us to transfer files that would otherwise take an inordinate amount of data and time. Every time you link to ZIP files or view JPEG images, you benefit from file compression.

How file compression works

So, you may be wondering: how does file compression work? In this article, let's take a basic look at what file compression means and how it works.

What is compression?
Simply put, file compression (or data compression ) is the act of reducing the size of a file while preserving the original data. This way, the file takes up less space on a storage device and can also be transferred more easily over the internet or otherwise.

It is important to note that compression is not infinite. Although it can be done incrementally, there is always a limit. While compressing a file to a ZIP compression format reduces its original size, you cannot continue to compress the file to reduce it further.

Generally, file compression is divided into two main types: lossy and lossless. Let's take a look at how both of them work.

How file compression works: Lossy Compression

Lossy compression (or lossy compression) reduces the size of a file by removing unnecessary pieces of information. It is most common in image, video, and audio formats, where a perfect representation of the original source is not necessary. There are many formats, or if you like, compression protocols for lossy compression for each type of file (music, image, or video). MP3 and JPEG are two popular examples.

An MP3 does not contain all the audio information from the original recording. It essentially discards and deletes sounds that humans do not hear. It deletes frequencies below 20Hz and above 16000Hz as well as various acoustic harmonics.

You won't notice they're missing anyway, and so removing this information results in a smaller file size with virtually no downside.

How file compression works

Similarly, JPEG compression removes non-vital parts of images. For example, in a photo containing a blue sky, JPEG compression might change all the sky pixels to one or two shades of blue, rather than using dozens of different shades.

However, for every type of file, there is a threshold where the quality reduction due to lossy compression is not noticeable. Below that threshold, the more you compress a file, the more noticeable the quality drop. You may have heard MP3 files with a lot of lossy compression from YouTube. For example, compare this high-quality music track:

With this very compressed version of the same song:

Lossy compression is appropriate when a file contains more information than you need for your purposes. For example, let's say you have a huge RAW image file. If you want to print this image on a large poster then you need to maintain that quality so that it prints vividly, with clear colors and no pixelation. It's pointless to upload the RAW file to Facebook.

How file compression works

The image contains so much data that it is not noticeable when viewed on social media sites. Compressing the image to a high-quality JPEG removes some information, but the image looks almost the same to the naked eye.

See our comparison of popular image formats for a deeper analysis on the topic: When do I use JPEG, GIF, or PNG?

Lossy Compression in General Use
As we’ve mentioned, lossy compression is largely suitable for most forms of social media. Because of this, it’s vital for companies like Spotify and Netflix that are constantly streaming massive amounts of information. Reducing the file size as much as possible while maintaining quality makes their operation more efficient. Can you imagine the storage space that would be required if every YouTube video was stored and streamed in its original uncompressed form?

Large  companies rely heavily on file compression to be able to offer their services extensively in limited storage spaces.

But lossy compression doesn't work so well for files where all the information is critical. For example, using lossy compression on a text file or spreadsheet would result in corrupted output. You can't really throw anything away without seriously damaging the final product.

When saving a file in a lossy format, you can often specify the quality level. For example, many use a slider to select the quality of a JPEG file from 0-100.

Saving at something like 90 or 80 percent reduces the file size quite a bit, with little difference to the eye. But saving at a low quality, like 10%, or repeatedly saving the same file with a small loss, will eventually degrade it.

How File Compression Works: Lossless Compression
Lossless compression is a way of reducing the size of a file so that you can perfectly reconstruct the original file. Unlike lossy compression, it doesn't throw away any information. Instead, lossless compression essentially works by removing redundant elements.

Let's take a basic example to show what this means. Below is a stack of 10 bricks: two blue, five yellow, and three red. This stack is a simple way to visualize these bricks, but there is another way to do this.

How file compression works

Instead of showing all 10 bricks, we can remove all but one of each color. Then, if we use numbers to show how many bricks of each color there were, we have represented exactly the same piece of information using far fewer bricks. Instead of 10 bricks, we now only need three.
How file compression works

This is a simple illustration of how lossless compression is possible. It stores the same information in a more efficient way by removing redundancy. Consider a real file, where the following string:

mmmmmuuuuuuuuuuuuuuuuuuuuu

It can be “compressed” into the following, much smaller form:

m5u7o12

This allows us to use seven characters instead of 24 to represent the same data, which is a significant savings.

Compression Lossless in daily use

How file compression works

As mentioned above, lossless compression is important in situations where you can't remove any of the original files. If you're curious about how ZIP files work, read on:

When you create a ZIP file with a program in Windows, you are using lossless compression. Compressing ZIP files is an efficient way to store the file, and when you decompress the file, all of the original information is there. If you used lossy compression to compress, for example, an executable file, the decompressed version would be corrupted and unusable.

Common lossless compression formats are PNG for images, FLAC for audio, and ZIP for all files in general. Lossless compressions for video are rare because the results will take up huge amounts of space.

When to use lossy compression vs. lossless compression

How file compression works

Now that we’ve looked at both file compression formats, you may be wondering when you should use one and when the other. As it turns out, there’s no “best” compression format, it all depends on how you want to use your files.

In general, you should use lossless compression when you want a perfect copy of the source material, and lossy compression when an imperfect copy is good enough. Let's look at another example to understand how to harmoniously choose which way is best.

Say you've just ripped your old CD collection and want to digitize it so you can have all your music on your computer. When ripping your CDs, it makes sense to use a format like FLAC, which is lossless. This allows you to have an original copy on your computer that's as good as the original CD.

Later, you might want to put some music on your phone or an old MP3 player so you can enjoy it on the go. You probably don't care about your music being in perfect quality for that, so you can convert your FLAC files to MP3. This gives you an audio file that is still perfectly intelligible, but doesn't take up much space on your mobile device. The quality of the MP3 converted from FLAC will be just as good as if you were creating a compressed MP3 audio file directly from the original CD.

Concerns When Compressing Files
As we have seen, converting a lossless compressed file to a lossy compression format is not a problem. However, you should never convert a lossy compressed file to a lossless compression format. In general, you should be careful when converting one compression format to another.

Converting a lossy compressed file to a second lossless compressed format is simply a waste of space. Remember that lossy compressions delete data that is then impossible to recover.
How file compression works

Say you have an MP3 file that's 3MB in size. Converting it to FLAC might result in a 30MB file, but that 30MB contains the same sounds that the much smaller MP3 files had. Converting back to a lossless format doesn't "recover" the information that the MP3 compression erased.

Finally, as mentioned above, converting from one lossy compression format to another (or repeatedly saving to the same format) will further degrade the quality. Each time you apply lossy compression, you lose more detail. This becomes increasingly noticeable until the file is essentially destroyed.

How does compression work? Now you know
We've broken down lossy and lossless compression and seen how they work. Now you know how to save a file in a smaller size than its original format and how to choose the best method for your needs.

Of course, the algorithms that decide what data to delete in lossy methods and how best to store redundant data in lossless compression are much more complex than what we've explained here. There's a lot more that you can further explore online if the topic interests you.

📧
Subscribe to the SecNews Newsletter

The most important Security & Technology news in your Inbox.

SecNews
SecNewshttps://www.secnews.gr
In a world without fences and walls, who needs Gates and Windows

SEARCH

FOLLOW US

📧
Newsletter SecNews
The most important Security & Technology news in your inbox.

LIVE NEWS