Objective: Pick the right command line compression tools for the job on any operating system, run them with the flags that matter, verify what you built, and avoid the traps that bite people in production (2 GB limits, AppleDouble junk, path traversal, a backdoored xz).
Target audience: Network and security engineers, sysadmins and anyone who moves logs, configs, packet captures, backups or release bundles between Linux, macOS, Windows and BSD boxes from a terminal.
Every OS ships some mix of archivers and compressors, and most people learned exactly one incantation (tar -czvf) twenty years ago and never looked back. That habit is costing you time. In my lab, zstd at its default level beat gzip on both speed and size by a wide margin, Windows has shipped a real tar for years, and macOS has two archivers most Mac users have never typed. This guide walks the command line compression tools that matter on each platform, with real benchmark numbers, copy-paste commands and the gotchas I have hit in the field.
Prerequisites and Lab Setup
Assumed knowledge: Basic shell navigation (bash or zsh), PowerShell or cmd basics on Windows, and file permissions.
Lab used for this post:
- Ubuntu 24.04 LTS VM, 2 vCPU: GNU tar 1.35, gzip 1.12, bzip2 1.0.8, xz 5.4.5, zstd 1.5.5, pigz 2.8, lz4 1.9.4, brotli 1.1.0, 7-Zip 23.01, bsdtar 3.7.2
- macOS (Apple Silicon) with Homebrew for the tools Apple does not ship
- Windows 11 24H2 with Windows Terminal, PowerShell and cmd
- FreeBSD for the BSD notes
| Tool | Type | What it does | Typical extension |
|---|---|---|---|
| tar (GNU tar, bsdtar) | Archiver | Bundles files, dirs, perms and links into one stream | .tar |
| gzip / pigz | Compressor | DEFLATE, universal, pigz is the multi-core version | .gz, .tgz |
| bzip2 / pbzip2 / lbzip2 | Compressor | Burrows-Wheeler, better ratio than gzip, slow | .bz2 |
| xz | Compressor | LZMA2, high ratio, slow to compress | .xz, .txz |
| zstd | Compressor | Zstandard, fast at every level, tunable 1 to 22 | .zst, .tzst |
| lz4 | Compressor | Extremely fast, lower ratio | .lz4 |
| zip / unzip | Archiver plus compressor | Per-file DEFLATE with a central index | .zip |
| 7-Zip (7zz, 7z) | Archiver plus compressor | LZMA/LZMA2, AES-256, reads almost everything | .7z |
| ditto, aa | Archiver (macOS) | Mac metadata aware zip and Apple Archive | .zip, .aar |
| Compress-Archive | Archiver (Windows) | PowerShell zip cmdlets | .zip |
Archiving vs. Compression: Two Different Jobs
The single most useful mental model: archiving turns many files into one stream (keeping names, permissions, owners, timestamps and links), and compression makes one stream smaller. Unix keeps the jobs separate (tar then gzip, xz or zstd). Zip and 7z do both in one tool.
Figure 1: Archiving vs. compression: the Unix tar plus compressor model compared with the zip and 7z model

The tradeoff that follows from this design:
- tar plus a compressor compresses the whole stream at once, so redundancy across files gets squeezed out (better ratio). The cost: no random access. Listing an archive or pulling one file means decompressing the stream up to that point.
- zip compresses each member on its own and writes a central directory at the end, so listing is instant and you can extract a single file without touching the rest. The cost: worse ratio on many small similar files, and weaker support for Unix ownership and special files.
- 7z can work either way, and by default uses a “solid” block (closer to the tar model) for better ratio.
Which Command Line Compression Tools Ship With Each OS
| Tool | Linux (typical distro) | macOS | Windows 10/11 | FreeBSD |
|---|---|---|---|---|
| tar | GNU tar | bsdtar (libarchive) | tar.exe (bsdtar) | bsdtar |
| gzip / gunzip | Yes | Yes | No (tar.exe reads .gz) | Yes |
| bzip2 | Yes | Yes | No (tar.exe reads .bz2) | Yes |
| xz | Yes | Homebrew (tar reads .xz) | No (tar.exe reads .xz) | Yes |
| zstd | Most modern distros | Homebrew | No | Yes |
| zip / unzip | Often, check minimal images | Yes | Compress-Archive, Explorer | unzip only |
| 7-Zip | Package 7zip (7zz) | Homebrew sevenzip | Installer from 7-zip.org | Package 7-zip |
| Platform specials | cpio, pax | ditto, aa | Compress-Archive, Expand-Archive | pax |
Two details in that table surprise people. First, Windows has had tar.exe since 2018: Microsoft added it in Windows 10 Insider build 17063, built on libarchive’s bsdtar. Second, macOS tar is also bsdtar, not GNU tar, so a few GNU-only flags you copy from a Linux blog will fail on a Mac.
Step 1: tar Fundamentals That Work Everywhere
Goal: Create, list and extract archives with one set of flags that works on GNU tar, bsdtar on macOS and BSD, and tar.exe on Windows.
Action: Learn the five letters that matter: c create, x extract, t list, f file name follows, v verbose. Add a compressor letter or let tar pick one from the file name.
# Create, list, extract (plain tar, no compression)
tar -cvf project.tar project/
tar -tvf project.tar
tar -xvf project.tar -C /tmp/restore/
# Compressor flags: z=gzip j=bzip2 J=xz (--zstd on GNU tar and bsdtar)
tar -czf project.tar.gz project/
tar -cJf project.tar.xz project/
tar --zstd -cf project.tar.zst project/
# Let tar pick the compressor from the suffix (-a / --auto-compress)
tar -caf project.tar.zst project/
# Extract: modern GNU tar and bsdtar detect the compression on their own
tar -xf project.tar.zst
GNU tar can pick the compressor from the suffix with -a (--auto-compress), per the GNU tar compression section. On extraction you do not need z, J or --zstd at all when reading from a file; tar sniffs the format.
Flags worth memorizing:
tar -czf logs.tgz --exclude='*.pcap' --exclude='.git' /var/log/myapp
tar -xpf backup.tar.gz # -p keep permissions (default for root)
tar -xf backup.tar.gz --no-same-owner # extract as you, not as stored UID
tar -cf - project | ssh lab 'tar -xf - -C /opt' # stream over SSH
tar -I 'zstd -19 -T0' -cf release.tar.zst build/ # custom compressor + flags
tar -xf big.tar.zst project/config/app.yaml # pull one member
Verification: tar -tvf project.tar.zst should print member names with permissions and owners:
drwxr-xr-x root/root 0 2026-12-03 18:28 project/
drwxr-xr-x root/root 0 2026-12-03 18:28 project/src/
-rw-r--r-- root/root 3 2026-12-03 18:28 project/src/app.py
-rw-r--r-- root/root 288894 2026-12-03 18:28 project/logs/app.log
Step 2: The Stream Compressors (gzip, bzip2, xz, zstd, lz4)
Goal: Compress single files (logs, pcaps, disk images, database dumps) with the right algorithm and know the shared flags.
Action: They all follow the same gzip-style interface, which makes them easy to swap.
| Flag | gzip | bzip2 | xz | zstd | lz4 | Meaning |
|---|---|---|---|---|---|---|
| -d | Yes | Yes | Yes | Yes | Yes | Decompress |
| -k | Yes (1.6+) | Yes | Yes | Default | Default | Keep the original file |
| -c | Yes | Yes | Yes | Yes | Yes | Write to stdout |
| -t | Yes | Yes | Yes | Yes | No | Test integrity |
| -l | Yes | No | Yes | Yes | No | List sizes and ratio |
| -1 to -9 | Yes | Yes | Yes | 1 to 19 (22 with –ultra) | 1 to 12 | Speed vs. ratio |
| -T0 | pigz -p | pbzip2 | Yes | Yes | No | Use all cores |
gzip -k -9 capture.pcap # capture.pcap.gz, original kept
gunzip -c capture.pcap.gz | tcpdump -r - -nn | head
xz -k -T0 -9 firmware.bin # best ratio, all cores
zstd -T0 -10 db-dump.sql # db-dump.sql.zst, original kept
zstd -d db-dump.sql.zst -o restored.sql
lz4 vm-disk.raw vm-disk.raw.lz4 # when speed is all that matters
zcat access.log.gz | grep ' 500 ' # z-tools read without extracting
zstdcat app.log.zst | grep ERROR
xzgrep -i 'denied' auth.log.xz
Note the defaults: gzip, bzip2 and xz delete the source after compressing unless you pass -k. zstd and lz4 keep it unless you pass --rm. That one difference has eaten more than one evidence file.
Need multi-core gzip without changing the format? pigz writes normal .gz files that any gunzip can read. In my lab it compressed 3.4x faster than gzip on 2 vCPU with an identical ratio, and it scales further on bigger boxes.
Step 3: zstd Deep Dive (the One to Default To)
Goal: Use zstd’s levels, threads and long-range mode to replace gzip for most Unix work.
Action: Start with the defaults, then tune.
zstd file # level 3, fast, decent ratio
zstd -19 -T0 file # high ratio, all cores
zstd --ultra -22 --long=31 file # max ratio, huge window (needs RAM)
zstd -d --long=31 file.zst # decompress side must match --long=31
zstd --adapt big.img # auto-tune level to I/O speed (pipes)
zstd -T0 --rm file # delete source like gzip does
zstd --patch-from=v1.iso v2.iso -o v2.patch.zst # binary delta
zstd -lv archive.tar.zst # frame info, window size, checksum
Recent releases keep improving the CLI: zstd v1.5.7 made the CLI multi-threaded by default, scaling with core count up to 4 threads. On older builds (like the 1.5.5 in Ubuntu 24.04) pass -T0 yourself.
Decompression speed is the hidden win. Every zstd level in my test decompressed the 80 MB corpus in about 0.1 seconds, while xz took about a second and bzip2 took 2.5 seconds. Compress once at a high level, decompress fast forever. That is why so many package managers and the Linux kernel adopted it.
Small file trick (dictionaries): compressing thousands of tiny JSON or log records? Train a dictionary and the ratio jumps.
zstd --train samples/*.json -o json.dict
zstd -D json.dict record.json
zstd -D json.dict -d record.json.zst
Step 4: zip, unzip and 7-Zip
Goal: Build archives that a non-technical user can open on any OS, and archives with real encryption.
Action (zip / unzip):
zip -r -9 site-backup.zip site/ -x 'site/.git/*'
zip -r -y links.zip project/ # store symlinks as links
unzip -l site-backup.zip # list before extracting
unzip -t site-backup.zip # integrity test
unzip -q site-backup.zip -d /tmp/site
unzip -p site-backup.zip site/config.php | less # read one file
Do not trust zip -e. Classic zip encryption (ZipCrypto) is weak and broken; treat it as an envelope, not a lock. If you need a password, use AES via 7-Zip.
Action (7-Zip): On Linux and macOS the modern binary is 7zz (official build) or 7z from the 7zip package. The old p7zip fork is unmaintained; avoid it.
7zz a -mx=9 evidence.7z case-1234/ # max LZMA2 ratio
7zz a -p -mhe=on secrets.7z keys/ # AES-256, encrypt file names too
7zz a -tzip -p -mem=AES256 share.zip report.pdf # AES zip for Windows users
7zz l evidence.7z # list
7zz t evidence.7z # test
7zz x evidence.7z -o/tmp/case # extract with paths (no space after -o)
7zz a -v100m big.7z disk.img # split into 100 MB volumes
-mhe=on matters: without it, a 7z archive encrypts the contents but leaves the file names readable to anyone.
Patch your archivers. Archive tools parse attacker-controlled input all day. CVE-2025-0411 let crafted archives bypass the Windows Mark-of-the-Web when extracted with 7-Zip, it landed in the CISA KEV catalog, and Kaspersky’s write-up notes version 24.09 fixed it. 7-Zip does not auto-update, so check what is deployed; the 7-Zip site lists the current 26.x releases.
Step 5: Windows (tar.exe, PowerShell and 7-Zip)
Goal: Archive and extract from cmd or PowerShell without installing anything, and know when to reach for 7-Zip.
Action (tar.exe, works in cmd and PowerShell):
tar --version
tar -czf C:\Temp\logs.tgz -C C:\ProgramData\MyApp Logs
tar -tf C:\Temp\logs.tgz
tar -xf release.tar.gz -C C:\Tools
tar -a -cf C:\Temp\Logs.zip -C C:\ProgramData\MyApp Logs # makes a real .zip
That last line is the hidden gem: because Windows tar is bsdtar, -a with a .zip suffix writes a standard zip archive, with no 2 GB cmdlet limit.
Action (PowerShell cmdlets):
Compress-Archive -Path C:\Reports\* -DestinationPath C:\Temp\reports.zip
Compress-Archive C:\Reports\new.csv -Update -DestinationPath C:\Temp\reports.zip
Expand-Archive -Path C:\Temp\reports.zip -DestinationPath C:\Restore -Force
Get-FileHash C:\Temp\reports.zip -Algorithm SHA256
Know the ceiling: Compress-Archive sits on System.IO.Compression.ZipArchive, and Microsoft documents that the API limits the maximum file size to 2GB. Big VM exports, memory dumps and pcaps fail with “Stream was too long”. Use tar.exe, 7-Zip or the .NET class directly:
Add-Type -AssemblyName System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('D:\Exports', 'D:\exports.zip')
File Explorer is not the command line, but it matters: Windows 11 reads .rar, .7z, .tar.gz, .tar.zst and friends through libarchive, and 24H2 added creating 7z and TAR archives from the right-click menu. Useful when you send a .tar.zst to a Windows user.
Action (7-Zip on Windows): 7z.exe lives in C:\Program Files\7-Zip\. Same switches as above: 7z a -mx=9 out.7z folder, 7z x in.7z -oC:\Out.
Step 6: macOS (bsdtar, ditto and aa)
Goal: Archive on a Mac without leaking metadata junk, and preserve Mac metadata when you actually want it.
Action (keep junk out of archives for Linux and Windows users):
COPYFILE_DISABLE=1 tar -czf clean.tgz project/ # no ._AppleDouble files
tar --no-mac-metadata -czf clean.tgz project/ # same idea, bsdtar flag
zip -r -X clean.zip project/ -x '*.DS_Store' # no extra attrs, no .DS_Store
Without that, a Linux colleague extracting your tarball gets a ._file beside every file. That is macOS storing extended attributes and resource forks as AppleDouble entries.
Action (ditto, the Finder-compatible zipper):
ditto -c -k --sequesterRsrc --keepParent MyApp.app MyApp.zip # like Finder
ditto -x -k MyApp.zip ~/Desktop/ # extract
ditto -c -k --norsrc --noextattr --noacl project project.zip # strip Mac data
ditto is what you want for app bundles, code-signed binaries and anything where extended attributes matter; see the ditto man page for the full option list.
Action (aa, Apple Archive): Apple Archive is Apple’s own format with LZFSE, LZMA and zlib options. Per the aa man page:
aa archive -d project -o project.aar # default LZFSE
aa archive -d project -o project.aar -a lzma -b 8m
aa extract -i project.aar -d restored/
aa list -i project.aar
Use it for Mac-to-Mac transfers and to unpack Apple installer payloads. Everyone else wants zip or tar.
Homebrew fills the gaps: brew install zstd xz pigz sevenzip. Note that brew install gnu-tar gives you GNU tar as gtar if a script depends on GNU-only flags.
Step 7: Linux and BSD Power Moves
Goal: Move big data fast, in pipelines, with progress and integrity.
# Backup over SSH, compressed in transit, nothing written locally
tar -cf - /etc | zstd -T0 -10 | ssh backup 'cat > etc-$(date +%F).tar.zst'
# Progress bar with pv
tar -cf - bigdir | pv -s $(du -sb bigdir | cut -f1) | zstd -T0 > bigdir.tar.zst
# Split for upload limits, then rejoin
tar -caf - case/ | split -b 2000M - case.tar.zst.part-
cat case.tar.zst.part-* | tar --zstd -xf -
# Recompress gzip to zstd without touching disk twice
gzip -dc old.tar.gz | zstd -T0 -19 -o new.tar.zst
# cpio and pax still matter: initramfs images are cpio archives
lsinitramfs /boot/initrd.img-$(uname -r) | head
On FreeBSD, tar is bsdtar from base and zstd is in base too, so tar --zstd and zstd -T0 work on a fresh install.
Benchmark: What the Numbers Actually Look Like
I compressed an 80 MB tarball of mixed text and binaries (a Python standard library plus /usr/share/doc) on the 2 vCPU Ubuntu lab VM. Wall clock seconds, single run, page cache warm.
| Command | Compress (s) | Size (MB) | Ratio | Decompress (s) |
|---|---|---|---|---|
| lz4 -1 | 0.18 | 33.8 | 2.37x | 0.07 |
| gzip -6 | 2.58 | 25.7 | 3.13x | 0.39 |
| pigz -6 | 0.76 | 25.8 | 3.11x | 0.21 |
| gzip -9 | 13.59 | 25.4 | 3.16x | 0.46 |
| zip -6 | 2.19 | 25.7 | 3.13x | 0.51 |
| bzip2 -9 | 5.74 | 21.6 | 3.71x | 2.52 |
| zstd -3 -T0 | 0.23 | 23.2 | 3.46x | 0.09 |
| zstd -10 -T0 | 1.15 | 20.0 | 4.02x | 0.09 |
| zstd -19 -T0 | 19.05 | 17.9 | 4.47x | 0.10 |
| brotli -q 9 | 16.76 | 17.7 | 4.54x | 0.23 |
| xz -6 | 34.02 | 17.1 | 4.70x | 1.22 |
| xz -9e -T0 | 47.19 | 15.7 | 5.10x | 0.94 |
| 7z -mx=9 | 21.98 | 15.7 | 5.10x | 0.94 |
Figure 2: Scatter chart of compression ratio versus compression time for lz4, gzip, pigz, bzip2, zstd, brotli, xz and 7-Zip

What I take from it:
- zstd -3 beat gzip -6 on every axis: about 11x faster to compress, 10 percent smaller, 4x faster to decompress.
- gzip -9 is a trap: five times slower than -6 for a 1 percent smaller file.
- For maximum ratio, 7z -mx=9 matched xz -9e in less than half the time on this box.
- Use gzip or zip when the other side might not have zstd. Compatibility is still a feature.
Run your own with zstd -b1 -e19 yourfile (zstd has a built-in benchmark) or 7zz b for 7-Zip.
Verification and Validation
Never ship or delete the original until the archive tests clean. Every tool has a test mode:
gzip -t backup.tar.gz && echo gzip OK
bzip2 -t backup.tar.bz2 && echo bzip2 OK
xz -t backup.tar.xz && echo xz OK
zstd -t backup.tar.zst
unzip -t backup.zip | tail -1
7zz t backup.7z | tail -2
tar -tf backup.tar.zst > /dev/null && echo tar index OK
sha256sum backup.tar.zst > backup.tar.zst.sha256
sha256sum -c backup.tar.zst.sha256
Expected success output:
gzip OK
bzip2 OK
xz OK
backup.tar.zst : 296960 bytes
No errors detected in compressed data of backup.zip.
Everything is Ok
tar index OK
backup.tar.zst: OK
Want to see ratios without extracting? gzip -l, xz -l and zstd -l read the headers:
$ xz -l app.log.xz
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 9832 B 282.1 KiB 0.034 CRC64 app.log.xz
Security Notes: Archives Are Attacker Input
- List before you extract.
tar -tvf,unzip -land7zz lshow absolute paths,../entries and symlinks before they land on disk. GNU tar strips leading/and../by default (you will see “Removing leading” warnings) and bsdtar refuses..paths; do not override either with-Pon anything you did not build. - Zip bombs: check the uncompressed size first (
unzip -l,zstd -l,xz -l) and extract into a quota-limited directory or container. - Extract untrusted archives as a normal user with
--no-same-ownerand--no-same-permissions, never as root into/. - The archiver itself is supply chain. CISA’s alert on CVE-2024-3094 covers the malicious code in XZ Utils 5.6.0 and 5.6.1. Check with
xz --versionand your package manager; any 5.6.0 or 5.6.1 build from that period should be treated as compromised. - Compression is not encryption. For confidentiality, pipe the archive through a real tool:
tar -cf - case | zstd -T0 | age -r <recipient> > case.tar.zst.age, or use7zz a -p -mhe=on.
Troubleshooting and Gotchas
1. “tar: Archive is compressed. Use -z option”
Cause: you piped a compressed archive into tar via stdin, so tar cannot seek to sniff the format. The GNU tar manual shows exactly this error.
cat archive.tar.gz | tar -tzf - # add the compressor flag on pipes
curl -sL https://example.org/x.tar.zst | tar --zstd -xf -
2. Compress-Archive fails with “Stream was too long”
Cause: the 2 GB ZipArchive limit. Fix: use tar -a -cf out.zip folder, 7-Zip, or [IO.Compression.ZipFile]::CreateFromDirectory().
tar -a -cf D:\export.zip -C D:\ VMExport
7z a -tzip D:\export.zip D:\VMExport
3. xz uses one thread or runs out of memory
Cause: xz caps memory and will quietly reduce threads. My benchmark printed “Reduced the number of threads from 2 to 1 to not exceed the memory usage limit”. Fix: raise or lower the limit explicitly, or switch to zstd.
xz -T0 --memlimit-compress=75% -9 big.img
xz --info-memory
4. `._file` clutter or “Ignoring unknown extended header keyword” on Linux
Cause: macOS bsdtar adds AppleDouble and xattr headers. Fix on the Mac: COPYFILE_DISABLE=1 tar ... or --no-mac-metadata. Fix on Linux after the fact: find . -name '._*' -delete.
5. zstd “Frame requires too much memory for decoding”
Cause: the archive was built with --long=31 or --ultra -22. Fix: pass the same --long=31 (or --memory=2048MB) when decompressing, and avoid --long for files you hand to other people.
Quick Reference Cheat Sheet
| Task | Linux / BSD / macOS | Windows |
|---|---|---|
| Fast everyday archive | tar -caf out.tar.zst dir/ |
tar -czf out.tgz dir |
| Archive for anyone | zip -r out.zip dir/ |
tar -a -cf out.zip dir |
| Max ratio | 7zz a -mx=9 out.7z dir/ |
7z a -mx=9 out.7z dir |
| Encrypted | 7zz a -p -mhe=on out.7z dir/ |
7z a -p -mhe=on out.7z dir |
| List | tar -tvf f / unzip -l f |
tar -tf f |
| Extract | tar -xf f -C dest/ |
tar -xf f -C dest |
| Test | zstd -t f / unzip -t f |
7z t f |
| Read without extracting | zstdcat f, zcat f, unzip -p f path |
tar -xOf f path |
| Mac-safe zip | ditto -c -k --keepParent dir out.zip |
n/a |
Sources
-
- GNU tar manual: Creating and Reading Compressed Archives
- Zstandard releases on GitHub (v1.5.7 default multithreading)
- Microsoft Tech Community: Tar and Curl Come to Windows
- Microsoft Learn: Compress-Archive cmdlet reference
- BleepingComputer: Windows 11 native support for 7-Zip, RAR and GZ archives
- Pureinfotech: Create ZIP, TAR and 7z archives on Windows 11
- aa(1) Apple Archive man page
- ditto(1) man page
- 7-Zip official site and release list
- NVD: CVE-2025-0411 7-Zip Mark-of-the-Web bypass
- Kaspersky: CVE-2025-0411 Mark-of-the-Web bypass in 7-Zip
- CISA alert: Supply chain compromise affecting XZ Utils, CVE-2024-3094
- InfoSecMonkey: macOS File System CLI Command Cheat Sheet
No fluff. Just the config that works.