Skip to content

Why Does ZIP Compression Ratio Vary Greatly Depending on the File?

Category: File Format

Have you ever experienced 「the file barely became smaller even after ZIP compression」 or 「text files drastically reduced in size after compression」? ZIP compression rates vary greatly depending on file type. This article explains the reasons and characteristics of each file format.

Compressed size by file type (vs 100% original) 0% 100% TXT / CSV ~15% JSON / XML ~20% HTML / CSS / JS ~25% DOCX / XLSX ~90% PDF ~95% JPEG / PNG / MP3 ~99% MP4 / ZIP ~100%
Diagram: ZIP compression ratio by file type (smaller = better)

Compression Algorithm Used in ZIP: Deflate

The ZIP format (.zip) primarily uses the Deflate algorithm. Deflate is a combination of the following two techniques.

  • LZ77 (Lempel-Ziv 1977): Replace repeating patterns in data with references to previous occurrences
  • Huffman coding: Represent frequently occurring characters with shorter bit sequences

In other words, data with many repetitive patterns has a high compression ratio, while random data or already-compressed data can barely be compressed at all.

Compression Rate by File Format

File FormatCompression ratio (approximate)Reason
Text (.txt)60–80% ReductionMany repeated characters and words
CSV70–85% reductionDelimiter and same pattern repeats
HTML / XML / JSON65–85% ReductionFrequent repetition of tags and key names
Log file70–90% reductionFrequent repetition of timestamp format
BMP (Uncompressed Image)50–80% ReductionMany consecutive pixels of the same color
PDF5–20% ReductionIn many cases, it is already compressed with zlib internally
PNG0–5% reductionAlready compressed with Deflate
JPEG0–5% reductionAlready compressed with DCT + Huffman
MP3 / AAC0–3% reductionAlready compressed with lossy compression
MP4 / H.2640–3% reductionAlready highly compressed
ZIP / GZ / 7z0–2% reduction (may increase in some cases)Re-compression of already compressed data is largely ineffective

When compressed files become even larger

When compressing already-compressed files like JPEG or MP4 with ZIP, the file size may increase slightly due to ZIP headers (file metadata). This is because the ZIP format includes a local file header (30 bytes or more) for each file and a central directory for the entire archive.

JPEGファイル (1.00 MB)
 └── ZIP圧縮後: 1.00 MB + ヘッダー(約50B)= わずかに増加

Difference between 「Store」 mode

ZIP has a Store mode that stores files without compression. When combining multiple already-compressed files (such as JPEG, MP4, etc.), using Store mode eliminates the CPU load of compression processing while storing them at equivalent sizes.

# zip コマンドで圧縮レベルを指定
zip -0 archive.zip image.jpg video.mp4   # Store(圧縮なし)
zip -9 archive.zip data.csv report.txt   # 最大圧縮

# Python で圧縮レベルを指定
import zipfile
with zipfile.ZipFile('archive.zip', 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zf:
    zf.write('data.csv')

Characteristics of test ZIP files

DevLab's test ZIP files contain random data (pseudo-random byte sequences) to precisely control file size. Since random data has maximum entropy, Deflate compression is nearly ineffective. Therefore, a "10MB ZIP file" remains approximately "10MB after decompression."

If you need a ZIP file that reaches a specific size after extraction, you can create a test file using a method like the one below.

# 解凍後ちょうど 100MB になるZIPを作成(ゼロバイト埋め、高圧縮)
dd if=/dev/zero bs=1M count=100 | zip -9 zero-100mb.zip -

# 解凍後ちょうど 100MB になるZIPを作成(ランダムデータ、ほぼ無圧縮)
dd if=/dev/urandom bs=1M count=100 | zip -0 random-100mb.zip -

Summary

  • ZIP Compression Ratio Is Determined by the Prevalence of Repeating Data Patterns
  • Text, CSV, and XML can be reduced by 60–85%
  • JPEG, MP4, and Pre-compressed Files Cannot Be Compressed Much (May Even Increase Slightly)
  • When combining already-compressed files, save CPU with Store mode (-0)
  • DevLab's test ZIP files use random data, so the size remains nearly identical before and after decompression

Download test ZIP files here

Test files for this article

❓ Frequently Asked Questions

Why does ZIP barely shrink some files?
JPEG, PNG, MP3 and MP4 are already compressed, so ZIP's Deflate algorithm has almost nothing left to squeeze. Zipping an already-compressed file can even grow it slightly, thanks to the ZIP headers. Data with lots of repetition — text, CSV, HTML, XML — typically shrinks by 60-85%.
What is ZIP's Store mode and when should I use it?
Store mode puts files into the archive as-is, with no compression. It suits bundling files that are already compressed, such as JPEG or MP4: you get the same archive size without paying the CPU cost of compressing. Pass -0 to the zip command.
Which file types compress best in a ZIP?
Log files compress best, typically 70-90%, followed by CSV at 70-85%, HTML, XML and JSON at 65-85%, and plain text at 60-80%. All of them are full of repetition, which is exactly what Deflate's LZ77 back-references and Huffman coding exploit.