Archive and compression formats compared: ZIP, TAR, GZ, BZ2, XZ, Zstandard, 7Z and RAR

Understand the architectural difference between random-access archives (ZIP), sequential tape archives (TAR), solid compressors (7Z, RAR), and stream compressors (Gzip, Bzip2, XZ, Zstandard).

1. Archiving vs. compression: why Unix separates TAR from GZ, BZ2, XZ and ZST

In the Unix philosophy, combining multiple files and directories into a single byte stream (archiving) is separate from shrinking that byte stream (compression):

  • TAR (.tar) concatenates files sequentially in 512-byte blocks—writing a 512-byte POSIX ustar header (containing the filename, octal permissions, uid/gid, mtime, and byte size) before each file's raw, uncompressed bytes. A pure .tar file is always a multiple of 512 bytes and performs zero compression.
  • Stream compressors (.gz, .bz2, .xz, .zst) compress a single stream of bytes. When wrapped around a .tar archive, they create a compressed tarball (.tar.gz / .tgz, .tar.bz2, .tar.xz, .tar.zst) that achieves natural solid compression across all files in the archive.
  • ZIP (.zip) combines archiving and per-file DEFLATE compression in one format, placing a random-access Central Directory at the end of the file so you can extract a single 4 KB file from a 2 GB ZIP archive without decompressing the preceding 1.99 GB.
FormatMagic bytes (hex)AlgorithmRandom single-file accessSample download
ZIP (.zip)50 4B 03 04 ('PK\x03\x04')DEFLATE (per file)Yes (via End Central Directory)ZIP samples
TAR (.tar)75 73 74 61 72 ('ustar' at offset 257)None (uncompressed 512B blocks)Sequential header hopsTAR samples
Gzip (.gz / .tgz)1F 8B 08DEFLATE (RFC 1952 + CRC32)No (sequential stream)GZ samples
Bzip2 (.bz2)42 5A 68 ('BZh')Burrows-Wheeler block sortBy 100–900 KB blocksample.bz2
XZ (.xz)FD 37 7A 58 5A 00LZMA2 + CRC64By XZ block indexsample.xz
Zstandard (.zst)28 B5 2F FD (RFC 8878)LZ77 + Finite State EntropyWith seekable frame tablesample.zst
7-Zip (.7z)37 7A BC AF 27 1CLZMA / LZMA2 solid blocksBy solid blocksample-archive-7z.7z
RAR (.rar)52 61 72 21 1A 07 ('Rar!')Proprietary RARlabs LZ/PPMBy solid/non-solid blockDocumented (proprietary encoder)

2. Critical security checks when extracting uploaded archives

  1. Zip Slip (path traversal via ../): An archive entry name is just an arbitrary string inside the header. If an archive contains an entry named ../../../../etc/cron.d/pwn or ..\startup.bat and your extractor joins that name directly to the output directory without verifying resolvedPath.startsWith(targetDir), the extractor writes outside the target directory.
  2. Zip Bombs and decompression ratio limits: Never decompress an untrusted archive to completion without tracking total uncompressed bytes written so far (not just the `uncompressed_size` field in the ZIP header, which can be spoofed).
  3. Symlink attacks in TAR and ZIP: POSIX tar archives (`typeflag = '2'`) and Unix-created ZIP files can contain symbolic links pointing to `/etc/passwd` or application secrets. Reject or strip symlink and device-node entries when unpacking user uploads.

Related guides and test tools

Frequently asked questions

Why do TAR files put their magic bytes at offset 257 instead of offset 0?
The first 100 bytes of every 512-byte TAR header block hold the null-terminated filename of the first archived file. The POSIX ustar signature (75 73 74 61 72, ASCII 'ustar') sits at byte offset 257.
Why is Zstandard (.zst) replacing Gzip in modern backup and package systems?
Zstandard (RFC 8878) achieves compression ratios comparable to XZ/Bzip2 while decompressing at over 1,000 MB/s per CPU core—roughly 3× to 4× faster than Gzip.