Introduction
Packaging and compression solve related but different problems. An archive combines a directory tree into one data stream while preserving names and structure. Compression represents data more compactly. A .tar file is an archive; a .gz file is gzip-compressed data; a .tar.gz file is a tar archive compressed with gzip.
You will build a small project, create each format, inspect an archive before extraction, restore into a separate directory, and compare restored data with the source. This inspect–extract–verify workflow avoids overwriting originals blindly.
Build a Directory Tree to Preserve
In this step, you will create a small source tree and record what must survive packaging and extraction.
Create configuration, notes, and data directories:
cd /home/labex/project/archive-lab
mkdir -p source/{config,notes,data}
Write three files with distinct content:
printf 'mode=training\nport=8080\n' > source/config/app.conf
printf 'Archive the complete project tree.\n' > source/notes/README.txt
printf 'alpha,beta,gamma\n1,2,3\n' > source/data/sample.csv
Use find to inspect regular files relative to the source directory:
find source -type f -printf '%P\n' | sort
Create and Inspect a Tar Archive
In this step, you will package the source tree without compression and inspect its member names.
The main tar operation letters are c for create, t for list, x for extract, and f for the archive filename. Use -C to change tar's working directory before adding source; this avoids storing a long absolute path:
cd /home/labex/project/archive-lab
tar -cf project.tar -C /home/labex/project/archive-lab source
The source tree still exists because archive creation reads rather than moves it. Inspect both the archive and one original file:
ls -lh project.tar
ls -l source/config/app.conf
List members before extracting:
tar -tf project.tar > tar-members.txt
cat tar-members.txt
Every member should begin with source/, and there should be no leading /. Relative member names make restoration safer and portable.
Extract Safely and Compare the Result
In this step, you will extract into an empty destination instead of over the source, then compare both trees.
Create a restore directory and use -C as the extraction destination:
cd /home/labex/project/archive-lab
mkdir tar-restore
tar -xf project.tar -C tar-restore
Inspect the restored paths:
find tar-restore -type f -printf '%P\n' | sort
Compare recursively. diff -r prints nothing and exits successfully when the trees match:
diff -r source tar-restore/source
echo "tar restore matches source" > tar-restore-ok.txt
cat tar-restore-ok.txt
Compress and Decompress One File with Gzip
In this step, you will see how gzip differs from an archive by operating on one regular file.
Copy a file so the source tree remains untouched, then compress the copy:
cd /home/labex/project/archive-lab
cp source/data/sample.csv sample-copy.csv
gzip sample-copy.csv
By default, gzip replaces the input with sample-copy.csv.gz:
ls -lh sample-copy.csv.gz
file sample-copy.csv.gz
Decompress it and compare its content with the original:
gunzip sample-copy.csv.gz
cmp source/data/sample.csv sample-copy.csv
echo "gzip round trip matches" > gzip-round-trip-ok.txt
cat gzip-round-trip-ok.txt
Gzip does not package a directory tree by itself; tar provides that structure.
Create and Restore a Compressed Tar Archive
In this step, you will combine tar packaging with gzip compression. The z option tells tar to pass archive data through gzip.
Create project.tar.gz and inspect it with the matching -tzf options:
cd /home/labex/project/archive-lab
tar -czf project.tar.gz -C /home/labex/project/archive-lab source
tar -tzf project.tar.gz > targz-members.txt
cat targz-members.txt
Extract into a separate destination and verify the tree:
mkdir targz-restore
tar -xzf project.tar.gz -C targz-restore
diff -r source targz-restore/source
echo "tar.gz restore matches source" > targz-restore-ok.txt
cat targz-restore-ok.txt
Extensions communicate the format to people and tools: .tar.gz clearly indicates a gzip-compressed tar archive.
Exchange the Project as a Zip Archive
In this step, you will create and inspect a Zip archive, a common format on Linux, macOS, and Windows.
zip -r recursively adds a directory tree. Run it from the lab root so stored paths begin with source/:
cd /home/labex/project/archive-lab
zip -r project.zip source
List members without extracting:
unzip -l project.zip
Use -d to choose a separate destination, then compare the result:
unzip -q project.zip -d zip-restore
diff -r source zip-restore/source
echo "zip restore matches source" > zip-restore-ok.txt
cat zip-restore-ok.txt
Summary
You separated packaging from compression, created and listed tar archives, completed a gzip round trip, combined both operations in .tar.gz, and exchanged the same tree through Zip. Most importantly, you restored into separate directories and compared content rather than assuming that archive creation was enough.



