Andrew Mercer
on this page

Compression and Archiving

This section covers the tools used to shrink and bundle files on Linux, from the ones you'll reach for today to the formats you'll still find in old backups and FTP mirrors. Each tool has its own page; this page explains the concepts, compares the options, and links to everything.

Core concepts

Archiving vs. compression

Unix traditionally splits two jobs between two kinds of tool:

Job What it does Tools
Archiving Bundles many files plus metadata (paths, permissions, owners, timestamps, links) into one stream tar, cpio, ar
Compression Shrinks a single byte stream gzip, bzip2, xz, zstd, lz4, lzip, brotli, lzop, compress
Both Self-contained archive format with built-in compression zip, 7-Zip, RAR
Mountable Compressed, read-only filesystem image SquashFS

The single-stream compressors compress one file. To compress a directory, archive it with tar first and compress the tar stream. That's what .tar.gz, .tar.zst and friends are.

Solid vs. per-file compression

  • Solid (tarballs, solid 7z): the whole archive is compressed as one stream. Better ratio, because redundancy across files is exploited. Downside: extracting the last file means decompressing everything before it, and corruption can damage everything after the bad byte.
  • Per-file (zip): each member is compressed independently. Fast random access and better corruption isolation, at the cost of ratio on many small similar files.

The trade-off triangle

Every compressor trades ratio, compression speed, and decompression speed/memory. Level flags (-1 … -9) move you along one algorithm's curve; choosing a different algorithm moves the whole curve. Today zstd dominates most of that curve, which is why it has become the default in so many places.

Choosing a tool

Situation Use
General default for new work (backups, logs, transfers) zstd — default level 3; -19 or --ultra -22 for archival
Must open anywhere, on any Unix, forever gzip
Smallest file, compressed once, downloaded many times xz -9e or zstd --ultra -22 --long
Maximum speed (real-time, in-memory, fast pipes) lz4
Sending files to Windows/macOS users zip, or 7-Zip if they have it
Password-protected archive for humans 7-Zip with AES-256 and -mhe=on
Encrypted backups for yourself tar \| zstd \| age — see encryption
Read-only image you want to mount, not extract SquashFS
Long-term archival with recovery tooling lzip
Pre-compressed static web assets brotli (and gzip via zopfli)
Mystery or legacy file Universal extractors: bsdtar, 7z, unar
bzip2 for new data Rarely; zstd and xz beat it on both axes

Relative characteristics (the shape of the curve, not exact numbers — benchmark on your own data):

Tool Compress speed Decompress speed Ratio Multithreaded
lz4 ★★★★★ ★★★★★ ★ Limited
zstd -1…-3 ★★★★ ★★★★★ ★★★ Yes (-T0)
gzip -6 ★★ ★★★ ★★ Via pigz
bzip2 -9 ★ ★ ★★★ Via pbzip2/lbzip2
zstd -19 ★ ★★★★★ ★★★★ Yes
xz -6…-9 ★ ★★ ★★★★★ Yes (-T0)

The headline: zstd decompresses fast at every level, while xz and bzip2 are slow to decompress no matter how they were compressed.

Modern tools

Single-stream compressors

Tool Ext. Algorithm First released Page
zstd .zst LZ77 + FSE/Huffman 2015 (1.0 in 2016) zstd
lz4 .lz4 LZ77 (byte-oriented) 2011 lz4
xz .xz LZMA2 2009 (LZMA ~1998) xz
gzip .gz DEFLATE 1992 gzip
bzip2 .bz2 Burrows–Wheeler + Huffman 1996 bzip2
lzip .lz LZMA 2008 lzip
brotli .br LZ77 + Huffman + static dictionary 2013–2015 brotli

Archivers and archive formats

Tool Ext. Type First released Page
tar .tar Archiver (pair with a compressor) 1979 tar
zip .zip Archive + per-file DEFLATE 1989 zip
7-Zip .7z Archive + LZMA/LZMA2, AES-256 1999 7-Zip
RAR .rar Proprietary archive 1993 RAR
cpio .cpio Archiver (initramfs, RPM payloads) 1977 cpio
ar .a, .deb Simple archiver (static libs, Debian packages) early 1970s ar
SquashFS .sqsh, .squashfs Compressed read-only filesystem 2002 SquashFS

Historical tools

Formats you'll mostly meet when digging through old archives, BBS-era software collections, or legacy systems.

Tool Ext. Era Notes Read it today with
pack .z (lowercase) 1970s Unix Static Huffman coding gzip -d (unpack on some systems)
compact .C early-1980s BSD Adaptive Huffman; slow, quickly replaced Rarely needed; uncompact if available
compress .Z 1984–85 LZW; the Unix standard until the Unisys patent fight pushed everyone to gzip gzip -d, uncompress — see compress
ARC .arc 1985 (SEA) DOS-era archiver; its legal fight with PKWARE led to ZIP unar, 7z, nomarch
LHA / LHarc .lzh, .lha 1988 Popular on DOS, Amiga, and in Japan lha, unar, 7z
ZOO .zoo 1986 Common on Usenet and Amiga unar, zoo
ARJ .arj 1991 DOS favourite for multi-volume floppy archives arj, unar, 7z
shar .shar, .sh 1980s "Shell archive": a script that recreates files when run. Never run an untrusted one — read it or use unshar unshar
LZO / lzop .lzo 1996–97 Speed-first ancestor of lz4 lzop — see lzop
LZMA-alone .lzma 2000s LZMA Utils format that predates .xz xz -d, unlzma — see xz
StuffIt .sit, .sitx 1987 Classic Mac OS unar
bzip .bz 1996 bzip2's predecessor; withdrawn over arithmetic-coding patents Effectively extinct

A short history

  • 1970s: Unix gets ar for libraries, then tar (Version 7, 1979) for tapes and cpio for PWB/UNIX. Compression is pack, simple Huffman coding.
  • 1984–85: Terry Welch publishes LZW; compress adopts it and becomes the Unix standard .Z format.
  • Late 1980s: The DOS world fragments into ARC, ZIP (Phil Katz, 1989), LHA, ZOO, then ARJ and RAR in the early '90s.
  • 1992: Unisys starts enforcing its LZW patent. The GNU project releases gzip, built on the patent-free DEFLATE algorithm, and .tar.gz becomes the lingua franca of free software.
  • 1996: bzip2 trades speed for a better ratio and gradually takes over source tarballs.
  • 1999–2009: Igor Pavlov's 7-Zip introduces LZMA; LZMA Utils and then xz (2009) bring it to Unix. Kernel and distro tarballs move to .tar.xz.
  • 2011–2016: Speed matters as much as ratio: lz4 (2011), brotli for the web, then zstd (2016), which covers nearly the whole speed/ratio range.
  • 2018 onward: zstd becomes the default across distro packages, kernels, initramfs, filesystems, journald, and container image layers.
  • 2024: The xz-utils backdoor (CVE-2024-3094) is a high-profile reminder that compression libraries sit deep in the supply chain — see xz.

File extensions at a glance

Extension Format
.gz, .tgz / .tar.gz gzip
.bz2, .tbz2 / .tbz / .tar.bz2 bzip2
.xz, .txz / .tar.xz, .lzma xz
.zst, .tzst / .tar.zst zstd
.lz4 lz4
.lz lzip
.br brotli
.lzo lzop
.Z compress
.zip (also .jar, .docx, .whl, …) zip
.7z 7-Zip
.rar RAR
.cpio, initramfs cpio
.a, .deb ar
.sqsh, .squashfs, .snap SquashFS

Cross-cutting topics

Page Covers
Encryption age, GnuPG, OpenSSL, and archive-native encryption; compress-then-encrypt
Parallel compression pigz, pbzip2/lbzip2, xz -T0, zstd -T0, thread counts in containers
Pipelines Streaming over SSH, progress bars, splitting, database dumps, Kubernetes pods
Testing and benchmarking Magic bytes, integrity checks, ratios, benchmarking on your own data
Compression in the stack logrotate, journald, HTTP, container images, databases, kernel, zram, filesystems
Universal extractors bsdtar, 7z, unar, ouch, atool, dtrx
macOS and Windows bsdtar, ditto, PowerShell, Explorer, cross-platform advice
Rust crates flate2, zstd, xz2, tar, zip and friends, with a .tar.zst example
Gotchas and safety Tarbombs, zip slip, decompression bombs, source-deletion defaults
Cheat sheet Every common command on one page
in this section
* 7-zip
* ar
* brotli
* bzip2
* compress (.z)
* compression cheat sheet
* compression gotchas and safety
* compression in rust
* compression in the stack
* compression on macos and windows
* compression pipelines
* cpio
* encrypting archives
* gzip
* lz4
* lzip
* lzop
* parallel compression
* rar
* squashfs and compressed filesystems
* tar
* testing and benchmarking compression
* universal extractors
* xz
* zip and unzip
* zstd