Compression: Making Files Smaller Without Losing Their Usefulness
📑 On this page
- Redundancy creates opportunities
- Lossless compression
- Dictionaries and references
- A concrete lossless example
- Lossy compression
- Why photographs compress well with JPEG
- Audio and video compression
- Compression ratio and bitrate
- Why compression takes processing work
- Why compressing a ZIP or JPEG again rarely helps
- Repeated lossy saves
- Compression and security
- Common misunderstandings
- "Lossless means uncompressed"
- "A smaller file always has visibly lower quality"
- "Every file can be compressed dramatically"
- "Changing a file extension changes its compression"
- Knowledge check
- The one idea to remember
Digital files can contain enormous amounts of raw data. A photograph may contain millions of pixel values, and one minute of uncompressed stereo audio can contain more than ten million bytes.
Yet familiar JPEG photographs, streaming songs, videos, and ZIP archives are often much smaller.
Compression works by finding a more efficient representation.
Lossless compression preserves enough information to reconstruct the original exactly. Lossy compression deliberately removes or simplifies information to achieve greater size reduction.
The correct approach depends on whether exact recovery matters and which changes users can tolerate.
Redundancy creates opportunities
Many files contain repeated or predictable patterns.
Consider this text:
AAAAAAAAAABBBBBBBBBBInstead of storing twenty individual letters, a simple system could record:
10 × A, 10 × BThis idea is called run-length encoding. It works well when values repeat in long runs. It can perform badly when every value differs because the count information adds overhead.
Real compression algorithms use more sophisticated forms of redundancy:
- Repeated byte sequences
- Frequently occurring symbols
- Predictable differences between neighboring values
- Repeated structures elsewhere in the file
Compression does not merely "squeeze" bytes physically. It describes the same or similar information using fewer symbols.
Lossless compression
Lossless compression guarantees that decompression reproduces the original data exactly.
That requirement matters for:
- Program files
- Documents
- Database records
- Source code
- Financial data
- Archives
- Medical or scientific data when every stored value must remain unchanged
A ZIP archive can contain several files, organize metadata, and apply lossless compression. PNG commonly uses lossless techniques for images. FLAC applies lossless compression to audio.
The decompressed result has the same bytes as the source, provided nothing was corrupted.
Dictionaries and references
Many lossless methods build or imply a dictionary of repeated sequences.
Suppose a document repeatedly contains:
Tech Explained SimplyThe compressor can store the phrase once and use shorter references for later occurrences. Decompression replaces each reference with the original phrase.
Algorithms in the LZ family use variations of this idea by referring to sequences that appeared earlier. Other techniques assign shorter codes to common symbols and longer codes to rare ones.
The exact mathematics can become advanced, but the intuition is straightforward:
Repetition should not always require repeating the full stored representation.
A concrete lossless example
Imagine an image row with these pixel labels:
blue blue blue blue blue white white red red redRun-length encoding might store:
5 blue, 2 white, 3 redFor a simple diagram with large flat areas, this can save space. For a photograph where neighboring pixels vary constantly, the same method may provide little benefit.
This explains why a PNG screenshot of a clean interface may be compact while a PNG photograph is large. The interface often contains repeated colors and sharp structures that lossless compression handles well.
Lossy compression
Lossy compression permits the decoded result to differ from the original.
It is common for photographs, audio, and video because these media contain far more raw detail than is practical to transmit and because human perception does not treat every detail equally.
A lossy encoder may:
- Reduce fine color detail
- Simplify subtle high-frequency changes
- Approximate groups of similar values
- Remove audio information masked by louder sounds
- Predict video frames from previous and future frames
The encoder tries to spend data where differences are most noticeable.
Lossy does not mean random destruction. Good codecs use models of signals and perception to make controlled trade-offs.
Why photographs compress well with JPEG
JPEG typically transforms small image blocks from direct pixel values into components describing spatial patterns. It then represents less visually important detail with reduced precision.
Human vision is often more sensitive to brightness structure than to very fine color variation. JPEG can take advantage of this characteristic.
At moderate settings, the differences may be difficult to notice at normal viewing size. At aggressive settings, artifacts become visible:
- Block boundaries
- Ringing around sharp edges
- Smearing of fine texture
- Color degradation
Text and line art can look especially poor because sharp edges expose the approximations. A lossless format is often a better choice for those images.
Audio and video compression
Lossy audio codecs analyze sound and attempt to reduce information that is less audible. They may use frequency-domain representations and psychoacoustic models.
Video compression has an additional opportunity: neighboring frames are often similar. Instead of storing every frame as an independent complete image, a codec can store:
- Occasional reference frames
- Changes from earlier or later frames
- Motion estimates for blocks or regions
A static background then needs far less repeated data.
Fast motion, film grain, confetti, water, and rapidly changing detail are harder to compress because prediction becomes less effective.
Compression ratio and bitrate
A compression ratio compares original size with compressed size.
If a 100 MB file becomes 20 MB:
100 ÷ 20 = 5The ratio is 5:1.
For continuous media, bitrate is often more useful. It describes the number of bits used per second.
A video at 5 Mbps uses approximately:
5 megabits/second ÷ 8 = 0.625 megabytes/secondFor ten minutes:
0.625 × 600 = 375 MBActual container size includes audio, metadata, and overhead, and bitrate may vary through the file.
Variable bitrate lets an encoder spend more data on complex sections and less on simple ones. Constant bitrate aims for a steadier data rate, which can be useful for some transmission constraints.
Why compression takes processing work
The compressor must analyze data, discover patterns, choose representations, and write encoded output. Decompression reverses the process.
Different formats balance:
- Compression speed
- Decompression speed
- Memory use
- Resulting size
- Quality
- Ability to seek or stream
- Hardware support
A backup archive might tolerate slow compression if it runs once and saves storage. A live video call needs low delay, so the codec must operate quickly enough for real-time conversation.
Compression trades storage and network capacity for computation and complexity.
Why compressing a ZIP or JPEG again rarely helps
Compressed data is designed to remove easy-to-find redundancy. Its byte patterns often look less predictable.
Putting a JPEG inside a ZIP archive may add organization but usually produces little additional size reduction. Compressing the ZIP again typically helps even less and may increase size slightly because another layer adds headers and metadata.
Encryption has a similar effect: good encrypted data appears statistically patternless, so general compression should normally happen before encryption.
Repeated lossy saves
Opening a JPEG and saving it again with lossy compression can introduce additional changes. Repeating this process may accumulate artifacts.
Professional workflows often preserve a high-quality original or lossless editing master, then export compressed delivery versions.
The same principle applies to audio and video. Transcoding from one lossy format or bitrate to another does not restore information removed earlier. It can remove more.
When possible, begin a new export from the best available source.
Compression and security
Compressed files require careful handling.
A small archive can expand into an enormous amount of data. Attackers can create decompression bombs intended to exhaust storage, memory, or processing resources.
Software should enforce limits on:
- Expanded size
- File count
- Nested archive depth
- Processing time
- Output paths
Archive extraction must also prevent files from escaping the intended directory through malicious path names.
Compression is a data representation feature, but its resource effects become a security concern.
Common misunderstandings
"Lossless means uncompressed"
Lossless data can be compressed. The defining property is exact recovery, not large size.
"A smaller file always has visibly lower quality"
Lossless compression can reduce size without changing the content. Good lossy compression can also remove information that is difficult to perceive.
"Every file can be compressed dramatically"
Compression depends on patterns and acceptable loss. Already-compressed, encrypted, or highly unpredictable data may not shrink meaningfully.
"Changing a file extension changes its compression"
Renaming .png to .jpg changes only the name. Actual conversion requires decoding the source and encoding a new file.
Knowledge check
1. What guarantee does lossless compression provide?
Decompression can reconstruct the original data exactly.
2. Why can lossy compression achieve greater reduction?
It is allowed to remove or approximate information instead of preserving every original value.
3. Why does a JPEG often shrink very little inside a ZIP archive?
JPEG compression has already removed much of the useful redundancy, leaving fewer predictable patterns for ZIP to exploit.
4. Why should important editing workflows preserve a high-quality source?
Repeated lossy encoding can accumulate damage, and information removed by an earlier export cannot be recovered by later transcoding.
The one idea to remember
Compression stores information more efficiently by exploiting patterns or accepting controlled loss.
Lossless methods preserve the exact original. Lossy methods trade some information for much smaller results. The right choice follows from the purpose and the consequences of change.
Next, we will connect data size to storage capacity and transfer speed so you can estimate how long files take to move.