What file compression is
Compression means writing the same information with fewer bytes. It comes in two families: lossless, like ZIP and RAR, which gives the original file back byte for byte, and lossy, which is what happens to images, audio and video, and which throws part of the detail away forever to get much smaller.
- Lesson 7 of 12
- Beginner
- Free, no signup
A lot goes in, a little passes through, the same comes out
What you put in
- A folder of documents
- Spreadsheets and code
- Text, full of repetition
What comes back out
- The same documents
- The same spreadsheets and code
- The same text, not a character missing
This shape describes lossless compression only. In lossy compression, what comes out at the bottom is no longer byte for byte what went in at the top.
Last checked: Facts and tool names in this lesson are re-checked against their sources on this date.
What compression actually does
Compression means writing the same content with fewer bytes by finding repetition. The idea is simpler than it sounds. If a text says the word "hello" thirty times, instead of writing it thirty times you write it once and point at it twenty nine times. The program that opens the file rebuilds the original text from those pointers.
So the more repetition a file has, the more it collapses. Text is full of repetition and gains the most.
An example from this very site: the stylesheet of the rgb.ir theme is 129 KB as it stands. The same file through gzip becomes 32 KB. About a quarter, without a single character being lost.

Lossless or lossy: the difference that decides everything
This is the only division in compression that genuinely matters; everything else is detail underneath it.
Lossless compression promises that the file coming out is byte for byte the file that went in. ZIP, RAR, 7z and gzip all belong to this family. For documents, code, spreadsheets and anything where a single byte counts, this family is the only usable one.
Lossy compression makes a different promise: I will make this much smaller, provided you let me throw away things the human eye and ear usually do not notice. A JPEG photo, an MP3 track and an MP4 video are all products of that bargain. What was thrown away does not come back. Opening the file again does not revive the lost detail, because it is no longer inside the file.
There is a simple way to see the difference. We compressed a file with gzip and opened it again, and the digital fingerprint of both copies was exactly the same; the thing we call a checksum in the safe software install lesson. No lossy file passes that test, and none is meant to.
Two families, two different jobs
Lossless: ZIP, RAR, gzip
- The file comes back byte for byte.
- For documents, code and spreadsheets.
- Gains the most on text.
- Almost nothing on an already compressed file.
Lossy: JPEG, MP3, MP4
- The discarded detail never comes back.
- For images, audio and video.
- Several times smaller than lossless.
- Every re-save adds fresh damage.
Neither is better than the other. The wrong choice is saving a document lossy and a web page image lossless.
Why zipping a folder of photos saves you nothing
Everyone has asked this once and the answer is one sentence: because those files are already compressed. A JPEG or WebP photo, an MP3 track and an MP4 video are all the product of heavy compression, and no repetition is left in them for ZIP to find.
We tried it on this server. One WebP image from the site media library, 549 KB. We compressed it with gzip at the highest setting and the result came out 115 bytes bigger than the original. Bigger, not smaller. The reason is reasonable enough: there was nothing left to collapse, and every compressed file carries a few bytes of header that get added on top.
So zipping a folder of photos is not useless, its use is simply a different one. It turns dozens of files into one file that is easier to send and does not lose pieces on the way. If your goal is less weight, you have to go at the photos themselves rather than at their packaging. That is the subject of the image formats lesson.
How much smaller does lossy actually get
Quoting a number here is meaningless except on a specific image, so we took a real one from this site media library, 2544 by 1904 pixels, and saved it several times with different settings.
As a PNG, which is lossless, it came to 7021 KB; about seven megabytes for one image. The same image as a JPEG at quality 90 came to 1388 KB, at quality 60 to 554 KB, and at quality 30 to 319 KB.
Two things in that row of numbers are worth seeing. First, the big jump happens between lossless and lossy, not between quality 90 and quality 30. Second, dropping from 60 to 30 takes only about two hundred kilobytes off and in exchange makes the damage visible along the edges. That is why in practice we usually stop somewhere between sixty and eighty and go no lower.
And one point tutorials rarely make: lower the quality once only. Every time you open a lossy file and save it again, fresh damage rides on top of the old damage. Keep the original lossless version somewhere and always export from that.
A password on an archive: a tool and a trick at once
Almost every compression program can put a password on an archive. It is a useful thing. If you have to email a few sensitive files, a password protected archive beats sending them plain, provided the password arrives by another route, a phone call for instance.
Now the other side of the coin. That same feature has been a malware delivery route for years, and the reason is not very technical. A system that inspects email attachments cannot see inside a password protected archive. Microsoft own documentation for Defender for Office 365 says exactly that: an encrypted attachment cannot be fully scanned or detonated unless the password is available, for example extracted from the email body. Which is why their recommended policy is to quarantine such messages.
So whoever sends the malware writes the password in the body of the email. It costs you nothing and it is a wall to the scanner.
The rule that follows is simple and better kept without exception: do not open a password protected archive you were not expecting, even when the sender looks familiar. A sender address can be forged; a phone call cannot.
The compression you never see
Most of the compression you benefit from day to day is not in your hands and you never hear about it. Almost every page you open in a browser arrives compressed from the server, and the browser unpacks it before showing it to you.
On this site you can measure it. At the time of writing this lesson, the rgb.ir cache layer holds 3111 ready pages on disk. Together they are 466 MB of HTML and they occupy 101 MB on disk, about 22 percent. The more interesting part is that there is no uncompressed copy in that folder at all; only the compressed version is stored, and that is what goes straight to the visitor.
And this is exactly what matters in a conversation about site speed: the same content, a fifth of the traffic. If that side of it interests you, the site speed track in this same learning section carries on from here, and if you have a site that is slow, the site speed service page shows how this is done on a real one.
The fast path, with AI
Before you zip a heavy folder and hope it shrinks, spend thirty seconds finding out where the weight actually is. Almost always a handful of files are large and the rest are noise, and almost always those few belong to the family ZIP cannot help. One command gives you the list, and a fast cheap language model of the Flash class turns the list into a decision; our current pick is listed in this site AI section.
- List the biggest files in the folder with their sizes. The top thirty rows are almost always the whole story.
- Hand the list to the model and ask which rows are already compressed and which ones zipping will genuinely shrink.
- If the answer is that the weight sits in images and video, drop the zip and go at those files themselves. Zipping them is packaging only.
Copy-ready recipe
Pull the list from the folder:
find . -type f -printf '%s\t%p\n' | sort -rn | head -30
The prompt, with that list attached:
These are the thirty biggest files in a folder, first column is size in bytes.
For each row say whether it is already compressed, and why.
Then build three groups:
1) Zipping these genuinely saves space.
2) Already compressed; the zip is packaging only.
3) The real weight is here and these need re-saving themselves.
My goal is {destination: emailing / long term archive / uploading to a site}.
Invent no numbers; speak only from this list.
Before you trust the output: The model sees only the names and sizes, never the content. A file with a zip extension can be full of photos and the model has no idea. Before deleting any original, open the archive and check it is sound; and if something is meant to last, keep two copies in two different places. A corrupt archive announces itself exactly when you need it.
AI in this kind of work
Compression is not where a language model performs miracles; ordinary programs do the actual work and they are free. What a model is genuinely good at here is this: reading a list you pulled from the folder yourself and telling you where the weight is and which effort is wasted. And there is one job never to hand it, described below.
Tools that actually help
- Gemini For the mechanical pass over a file list, on its fast cheap model. Google own page says the Gemini web app runs in over 230 countries and territories, and Iran is not on that list.
- Claude For when you want to understand the command rather than only run it, because it explains and the explanation can be argued with. Iran is on neither of Anthropic two supported-countries lists and there is no official payment route from Iran.
- 7-Zip Not an AI tool, and deliberately here: the actual work of this lesson is done by one free program and needs no model at all. The model decides, the program compresses.
- RGB The access and payment layer for Iran, kept separate from the tools themselves.
Where it backfires
The risk on this topic sits somewhere unexpected: free online compression tools. You search the address, upload the file, download the smaller copy. But your file has gone to a server you do not know, under a retention policy you have not read. For a holiday photo that does not matter; for a contract, a payslip or a scanned ID, compression happens on your own computer with a free program and there is no reason at all to upload it. The same goes for handing a document to a chatbot: whether the content feeds model training depends on the plan and settings of that service, and the companies write it down, for instance on Google privacy page for the Gemini apps. The second risk is more technical: a model guesses at file sizes and the guess is usually wrong. Any number about how small your file will get has to come from actually running the compression, not from a model.
Sources: Google: Gemini Apps privacy Where you can use the Gemini web app Anthropic supported countries
Where this advice stops
The numbers in this lesson were measured on this server on the exact files named here and do not carry over to your file. A text with heavy repetition collapses further than a quarter; a database file collapses almost not at all. Second, compression is not a substitute for a backup: a ZIP archive is one file, and if it corrupts it can take everything inside it with it, whereas an ordinary folder usually loses only the one damaged file. And a password on an archive is not organisation grade encryption; it is good against a casual look and no more.
From our own work
Every number you read in this lesson was taken on this server on 2026-09-06 and can be reproduced. The site theme stylesheet is 129 KB and becomes 32 KB through gzip at its highest setting. A WebP image from the media library, 549 KB, came out 115 bytes bigger through that same gzip rather than smaller; and after a gzip round trip that file had exactly the same sha256 fingerprint, which is what lossless means. The most interesting part, though, is the site own cache layer: at that moment there were 3111 ready page files on disk, and not one uncompressed copy in that folder. No page is stored twice; only the gzip version is written and that is what the web server sends straight out. Those 3111 pages are 466 MB of HTML between them and take 101 MB on disk, which is 21.77 percent.
Real follow-up questions
Is ZIP better than RAR or 7z?
All three are lossless and for everyday work the difference is small. ZIP has the advantage that Windows, Mac and Linux open it with nothing installed, so it is the safer choice for sending to other people. 7z usually squeezes a little further, and in exchange the other person needs its program.
If I zip a file twice, does it get smaller?
No, and it usually gets slightly bigger. The first pass took the repetition out and there is nothing left to take, while each round of packing adds a few bytes of header. That is exactly what happened to the WebP image in this lesson, which gained 115 bytes.
My zip file is corrupt; can anything be done?
Sometimes yes. Some compression programs have a repair option and can pull the sound part out, especially in RAR, which has optional recovery data. But the honest answer is that in most cases the damaged part is gone, and that is the main reason an archive is not a substitute for a backup.