Create and verify tar archives with gzip, xz, and zstd compression on Ubuntu 24.04
This guide walks through creating a tar archive of a directory and compressing it using three algorithms — gzip, xz, and zstd — on Ubuntu 24.04. Each compressed archive is tested for integrity, and a final extraction round-trip confirms the original data is fully preserved. All steps were verified end-to-end on a fresh Ubuntu 24.04 instance (incus 6.22) with xz-utils 5.4.5, zstd 1.5.5, and the system-provided gzip.
Create sample data and a plain (uncompressed) tar archive
Create a sample directory with two files — a short text note and a larger sequential number file — then produce an uncompressed tar archive from it. The plain archive serves as a baseline for later size comparisons.
$ mkdir -p /tmp/demo$ echo 'hello hostekhive' > /tmp/demo/notes.txt$ seq 1 100000 > /tmp/demo/numbers.txt$ tar -cf /tmp/archive.tar -C /tmp demo$ ls -l /tmp/archive.tar
captured output
-rw-r--r-- 1 root root 593920 Jun 16 15:15 /tmp/archive.tar
Install the xz and zstd compressors (gzip is already present)
Update the package lists and install xz-utils and zstd. The system's gzip is already present on Ubuntu 24.04, so only the other two compressors need to be added.
$ DEBIAN_FRONTEND=noninteractive apt-get update -qq$ DEBIAN_FRONTEND=noninteractive apt-get install -y -qq xz-utils zstd$ echo "installed: $(xz --version | head -n1), zstd $(zstd --version | grep -o 'v[0-9.]*' | head -n1)"
captured output
Selecting previously unselected package xz-utils. (Reading database ... (Reading database ... 5% (Reading database ... 10% (Reading database ... 15% (Reading database ... 20% (Reading database ... 25% (Reading database ... 30% (Reading database ... 35% (Reading database ... 40% (Reading database ... 45% (Reading database ... 50% (Reading database ... 55% (Reading database ... 60% (Reading database ... 65% (Reading database ... 70% (Reading database ... 75% (Reading database ... 80% (Reading database ... 85% (Reading database ... 90% (Reading database ... 95% (Reading database ... 100% (Reading database ... 16341 files and directories currently installed.) Preparing to unpack .../xz-utils_5.6.1+really5.4.5-1ubuntu0.3_amd64.deb ... Unpacking xz-utils (5.6.1+really5.4.5-1ubuntu0.3) ... Selecting previously unselected package zstd. Preparing to unpack .../zstd_1.5.5+dfsg2-2build1.1_amd64.deb ... Unpacking zstd (1.5.5+dfsg2-2build1.1) ... Setting up xz-utils (5.6.1+really5.4.5-1ubuntu0.3) ... update-alternatives: using /usr/bin/xz to provide /usr/bin/lzma (lzma) in auto mode Setting up zstd (1.5.5+dfsg2-2build1.1) ... installed: xz (XZ Utils) 5.4.5, zstd v1.5.5
Compress with gzip and verify archive integrity
Build a gzip-compressed tar archive using the -z flag, then verify its integrity with gzip -t. The test command passes without error and confirms the archive is valid.
$ tar -czf /tmp/archive.tar.gz -C /tmp demo$ gzip -t /tmp/archive.tar.gz && echo 'gzip integrity OK'
captured output
gzip integrity OK
Compress with xz and verify archive integrity
Build an xz-compressed tar archive using the -J flag, then verify its integrity with xz -t. The test passes and confirms no corruption.
$ tar -cJf /tmp/archive.tar.xz -C /tmp demo$ xz -t /tmp/archive.tar.xz && echo 'xz integrity OK'
captured output
xz integrity OK
Compress with zstd and verify archive integrity
Build a zstd-compressed tar archive using the --zstd flag (passed directly to tar), then verify its integrity with zstd -t. The test passes and reports the uncompressed equivalent size in stderr.
$ tar --zstd -cf /tmp/archive.tar.zst -C /tmp demo$ zstd -t /tmp/archive.tar.zst && echo 'zstd integrity OK'
captured output
zstd integrity OK /tmp/archive.tar.zst: 593920 bytes
Compare the compressed sizes and confirm extraction round-trips the original file
List all four archives sorted by size — showing the relative compression ratios — then extract the zstd archive into a fresh directory and read back the original notes file. The output matches the original content, confirming a successful round-trip.
$ ls -lS /tmp/archive.tar /tmp/archive.tar.gz /tmp/archive.tar.xz /tmp/archive.tar.zst$ mkdir -p /tmp/restore$ tar --zstd -xf /tmp/archive.tar.zst -C /tmp/restore$ cat /tmp/restore/demo/notes.txt
captured output
-rw-r--r-- 1 root root 593920 Jun 16 15:15 /tmp/archive.tar -rw-r--r-- 1 root root 215405 Jun 16 15:15 /tmp/archive.tar.gz -rw-r--r-- 1 root root 74559 Jun 16 15:15 /tmp/archive.tar.zst -rw-r--r-- 1 root root 18784 Jun 16 15:15 /tmp/archive.tar.xz hello hostekhive
You now have a plain tar archive plus three compressed variants (gzip, xz, zstd), each verified intact. The extracted contents match the original exactly, confirming that all three compression methods preserve data faithfully. A natural next step is to benchmark compression speed and ratio trade-offs for your own datasets, or automate this pattern in a backup script.