Compression formats
Quick answer
Linux offers gzip, bzip2, xz and Zstandard (zstd) for compressed streams, plus ZIP for an archive with independently stored or compressed entries. Choose the format based on recipient support, resource constraints and the task, not a universal ranking.
Prepare the practice environment
Run python3 lab-fixtures/archives/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example and stop if a command fails unexpectedly. All archive and output paths must be new. Outputs below are not commands.
How it works
Common suffixes are .gz, .bz2, .xz, .zst, .zip. A tar bundle combined with a compressor often uses .tar.gz, .tar.bz2, .tar.xz or .tar.zst. Compression ratio, throughput and memory depend on data, settings and versions; xz may trade time or memory for compactness while zstd is often chosen for speed, but measure on representative data. ZIP is useful for file exchange with systems that support ZIP; metadata support differs. For the stream compressors, -c writes stdout, -d decodes and -t tests integrity in the tools used here. Info-ZIP zip creates or updates an archive; unzip -l lists, -t tests and -d selects a destination. Test mode checks format integrity, not origin or absence of malicious content. No benchmark or universal ratio is claimed.
For a fair size comparison, feed the same original bytes to each compressor and keep each output separate. A smaller output does not establish a faster decoder or lower memory usage. Test each compressed file with its own decoder; a truncated stream may return failure even if it emits some decoded bytes. Do not treat partial stdout as recovered data. Record tool versions and input size if reporting measurements, and check the receiver supports the selected format.
Try the examples
bzip2 -c notes.txt > notes.bz2xz -c notes.txt > notes.xzbzip2 -dc notes.bz2 | cmp - notes.txt && xz -dc notes.xz | cmp - notes.txt && printf "both match\n"Both methods return the same original bytes. Tiny text is not a useful performance benchmark.
Expected stdout:
both matchzstd -q -c notes.txt > notes.zstzstd -q -t notes.zst && zstd -q -dc notes.zstQuiet mode suppresses informational messages; the test must succeed before decoding.
Expected stdout:
hello archivezip -q bundle.zip notes.txt report.txtunzip -Z -1 bundle.zipunzip -tq bundle.zip >/dev/null && mkdir zip-out && unzip -q bundle.zip -d zip-out && cmp notes.txt zip-out/notes.txt && cmp report.txt zip-out/report.txt && printf "ZIP restored\n"Info-ZIP -Z -1 lists member names. The trusted fixture is tested and extracted into a fresh destination before byte comparison.
Expected stdout:
notes.txtreport.txtZIP restoredCommon mistakes
Do not assume ZIP and gzip are interchangeable or that a renamed suffix changes the format. Identify the actual format and choose the matching decoder. Do not infer security from compression or encryption labels.
Practice
Create bzip2, xz and zstd versions of notes.txt. Check all three with their test modes and verify decoded bytes. Record actual sizes with wc -c, but do not generalize a tiny fixture result.
Related references
Study: bzip2, xz, zstd, zip, unzip. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compression formats — Knowledge Check
Apply the lesson concepts to the controlled archive fixture.
Which format directly bundles multiple named entries?
What does -dc do in xz?
Which input determines the best compression choice?
What does unzip -t test?