Create a ZIP Archive

Create a ZIP Archive

The teacher wants one downloadable package that colleagues can open on different systems. A ZIP archive can hold several named files, their directory paths and compressed member data. That makes it convenient for sharing a folder of reports as a single file.

A directory name alone does not necessarily include everything below it. We will request recursion so the student's list inside the term folder becomes a member too. Listing or extracting the finished archive is how you confirm that you packaged the right contents.

What you’ll learn

  • Create a ZIP archive containing a directory tree.
  • Use recursion and verify archive members before sharing the result.

A ZIP can contain many named members

  1. Directory

    The class list is nested beneath term.

    term/students.txt
  2. Recursive archive

    zip -r includes the directory and its descendants.

    term.zip
  3. Members

    The ZIP holds member names and compressed data, not merely a reference to the source.

    term/students.txt inside ZIP
ZIP combines named members into a convenient package, often with compression.

Create a ZIP containing the term directory

Prepare the example

Use a fresh folder with zip installed. The first two lines create the source tree. The archive name is outside that tree, and no previous ZIP of that name should exist, so the example is a new package rather than an update to an older one.

Bash · Linux terminal

mkdir term
printf 'Diya\n' > term/students.txt
zip -r term.zip term

Expected behavior; machine-dependent details

Progress lists added entries; sizes vary.

zip -r term.zip term requests a ZIP named term.zip. The -r option walks recursively through the source directory, so term/students.txt is included along with the folder entry. Depending on the input, some members may compress more than others.

The tool's progress text is not the member data itself. Inspect the archive with an appropriate listing command, such as unzip -l, before assuming a successful run means every intended report is inside. If you later update an existing archive, understand the tool's update behavior rather than treating that operation as identical to creating a new package.

Package files, include a tree and inspect members

The ZIP format combines archiving and compression in one widely supported file format. It is convenient when files need to be shared with Linux, Windows or macOS users.

Step 1: Install zip (if not already installed)

Most modern Linux distros come with zip pre-installed. But in case it's missing, here’s how to install it:

sudo apt update
sudo apt install zip

Step 2: Create Some Files

Let’s first create a few sample files to zip. You can skip this step if you already have files to compress.

mkdir myfiles
cd myfiles
echo "Hello World" > file1.txt
echo "Linux is awesome" > file2.txt
echo "zip command tutorial" > file3.txt

Step 3: Compress Files into a Zip Archive

Now let’s compress all those files into a single zip archive.

zip myarchive.zip file1.txt file2.txt file3.txt

This creates a new file called myarchive.zip which contains the three files.

  adding: file1.txt (deflated 10%)
  adding: file2.txt (deflated 20%)
  adding: file3.txt (deflated 30%)

Step 4: Zip an Entire Folder

To compress a whole folder (e.g., the current folder), use the -r (recursive) option:

zip -r myfolder.zip .
  adding: file1.txt (stored 0%)
  adding: file2.txt (stored 0%)
  adding: file3.txt (stored 0%)

Step 5: View the Contents of a Zip File

Want to peek inside your zip file without extracting it?

unzip -l myarchive.zip
Archive:  myarchive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       12  2025-07-02 10:01   file1.txt
       20  2025-07-02 10:01   file2.txt
       25  2025-07-02 10:01   file3.txt
---------                     -------
       57                     3 files

Step 6: Extract the Zip File

And finally, to unzip and get your files back:

unzip myarchive.zip
Archive:  myarchive.zip
  inflating: file1.txt
  inflating: file2.txt
  inflating: file3.txt

Build ZIP archives and verify their contents

  • Use zip myfile.zip files... to compress files.
  • Add -r for folders: zip -r archive.zip folder/
  • Use unzip -l to list contents.
  • Use unzip archive.zip to extract files.

Try it, then check the worked answer

Your task: Include a directory's contents in a ZIP archive.

Use a fresh folder with zip installed. The first two lines create the source tree. The archive name is outside that tree, and no previous ZIP of that name should exist, so the example is a new package rather than an update to an older one.

Bash · complete example for this question

mkdir term
printf 'Diya\n' > term/students.txt
zip -r term.zip term

Expected behavior; machine-dependent details

Progress lists added entries; sizes vary.

Worked answer

zip -r term.zip term

Use the recursive ZIP form for the directory's descendants. Gzip does not package this named directory tree in the same way. The key requirement is not just obtaining one output filename; it is making sure the files below the selected folder are included.

Check your decision

Include a directory's contents in a ZIP archive.
Choose one answer. You can try again.

Answer and explanation

zip -r term.zip term

Use the recursive ZIP form for the directory's descendants. Gzip does not package this named directory tree in the same way. The key requirement is not just obtaining one output filename; it is making sure the files below the selected folder are included.

Use recursion to include the whole directory tree

ZIP combines named members into a convenient package, often with compression. Add recursion when the source is a tree, then inspect the member list. Choose a format the receiver can use, and distinguish a newly created archive from an update to an existing one.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Extract a Compressed tar Archive →