13 Archiving and Extraction

Learning objectives

  • Package a directory.
  • Compress and extract common .tar.gz, .gz, and .zip files.
  • Distinguish packaging from compression.

Command overview

CommandPurposeMeaningBasic usageExample
tarArchive and extractTape archive; commonly handles .tar and .tar.gztar [options] archive pathtar -czf archive.tar.gz folder/
gzipgzip compressionCompresses one file to .gzgzip filegzip app.log
gunzipgzip extractionExtracts a .gz filegunzip file.gzgunzip app.log.gz
zipzip compressionCreates a .zip archivezip [options] archive pathzip -r archive.zip folder/
unzipzip extractionExtracts a .zip archiveunzip archiveunzip archive.zip

Common scenarios

Package a directory as .tar.gz:

tar -czf project.tar.gz -- project/

Extract .tar.gz:

mkdir -- extract-target
tar -xzf project.tar.gz -C extract-target

List archive contents without extracting:

tar -tzf project.tar.gz

Compress and extract an individual gzip file:

gzip app.log
gunzip app.log.gz

Create a zip archive in a unique lab directory and extract it into a new empty directory:

zip_lab=$(mktemp -d "${TMPDIR:-/tmp}/zip-lab.XXXXXX") || exit 1
if [ ! -O "$zip_lab" ] || [ -L "$zip_lab" ]; then
  printf 'zip lab ownership or symlink check failed\n' >&2
  exit 1
fi
mkdir -- "$zip_lab/project"
printf 'fixture\n' > "$zip_lab/project/README.txt"
(cd -- "$zip_lab" && zip -rq project.zip project) || exit 1
unzip -l "$zip_lab/project.zip"
zip_dest=$(mktemp -d "$zip_lab/extract.XXXXXX") || exit 1
unzip -q "$zip_lab/project.zip" -d "$zip_dest"
find "$zip_dest" -maxdepth 2 -print

Notes for beginners

  • Common tar options: c creates, x extracts, t lists, z uses gzip, and f names the file.
  • .tar is mainly packaging, .gz is mainly compression, and .tar.gz packages first then compresses.
  • gzip file normally replaces the original with file.gz; the original file is not retained.
  • Before extraction, use tar -tzf or unzip -l to see what is inside.
  • Extract an unknown archive into an empty directory first to avoid scattering files or overwriting existing ones.

Additional risks from untrusted archives

“List first + use a new empty directory” is only a minimum starting point. A malicious archive can contain absolute paths, .. path traversal, symbolic/hard links, special files, unusual permissions, or ownership information. Do not extract an unknown archive as root, directly into a project, home directory, or system path.

A controlled GNU tar check flow:

archive=$(realpath -e -- project.tar.gz) || exit 1
tar -tzf "$archive"
dest=$(mktemp -d "${TMPDIR:-/tmp}/archive-lab.XXXXXX") || exit 1
dest_real=$(realpath -e -- "$dest") || exit 1
printf 'archive=%s\ndestination=%s\n' "$archive" "$dest_real"
tar --extract --gzip --file="$archive" \
  --directory="$dest_real" \
  --no-same-owner \
  --no-same-permissions
find "$dest_real" -mindepth 1 -maxdepth 3 -print

You must still inspect absolute paths, .., and intended links in the listing. GNU tar and the common macOS bsdtar/libarchive differ in options and default protections; read the manual for the actual implementation.

Exercise and mastery check

In a lab directory, create two small text files, archive them, list the contents, extract them into a brand-new directory, and compare with cmp. Completion requires explaining packaging vs. compression, whether the original remains, the extraction destination, and the untrusted-archive boundary.

References