Zip Archive
File Compression
Directory Management
Tutorial
Tech Tips

How to create a zip archive of a directory?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Creating a ZIP archive of a directory is a routine task for releases, backups, and handoff bundles. The command is short, but the result depends on details such as whether the top-level folder is included, which files are excluded, and how you verify the archive before sharing it.

Decide What the Extracted Layout Should Be

Before running any command, decide what the recipient should see after extraction.

If the source directory is project/, there are two common outcomes:

  • the archive contains project/...
  • the archive contains only the files inside project

Including the top-level folder is usually safer because extraction does not spill files into whatever directory the recipient happens to be in.

Use zip -r on Linux and macOS

On Unix-like systems, the standard tool is zip in recursive mode.

Include the top-level directory:

bash
zip -r project.zip project

Archive only the contents:

bash
cd project
zip -r ../project_contents.zip .

Those commands can contain the same files but produce different internal paths, which matters during extraction.

Exclude Files Deliberately

Most directories contain files that should not be distributed, such as Git metadata, local dependencies, caches, or system junk files.

bash
zip -r project.zip project -x "*/.git/*" "*/node_modules/*" "*.DS_Store"

Treat exclusions as part of the packaging definition rather than an afterthought. That avoids oversized archives and reduces the chance of leaking local development data.

Use Compress-Archive on Windows

PowerShell provides the equivalent operation on Windows.

Archive the directory itself:

powershell
Compress-Archive -Path "C:\work\project" -DestinationPath "C:\artifacts\project.zip" -Force

Archive only the contents:

powershell
Compress-Archive -Path "C:\work\project\*" -DestinationPath "C:\artifacts\project_contents.zip" -Force

The same rule applies as with zip: the path you pass determines whether the top folder is included.

Verify the Archive Before Sharing It

A successful command does not guarantee the archive contains the right files. Always inspect the result.

List the contents:

bash
unzip -l project.zip

Test integrity:

bash
unzip -t project.zip

On Windows, a practical equivalent is to extract into a temporary folder and inspect it.

powershell
Expand-Archive -Path "C:\artifacts\project.zip" -DestinationPath "C:\tmp\project_test" -Force
Get-ChildItem "C:\tmp\project_test" -Recurse | Select-Object -First 10

That catches wrong paths, missing files, and corrupt output before someone else finds the problem.

Make the Process Repeatable

If your team creates archives regularly, capture the logic instead of rebuilding it from memory each time.

bash
1create_release_zip() {
2  local src_dir="$1"
3  local out_zip="$2"
4
5  rm -f "$out_zip"
6  zip -r "$out_zip" "$src_dir" -x "*/.git/*" "*/node_modules/*" "*.pyc"
7  unzip -t "$out_zip"
8}
9
10create_release_zip project release-2026-03-11.zip

A small helper keeps the process easier to review and less dependent on shell history.

Compression and Protection Are Different Problems

Compression reduces size. Encryption and access control are separate concerns.

If you want a password prompt with zip, you can use:

bash
zip -r -e secure_project.zip project

That may be fine for some workflows, but teams with real security requirements should follow an approved archive-encryption process rather than relying on an improvised command.

Common Pitfalls

The biggest mistake is running the command from the wrong directory and ending up with unexpected paths inside the archive.

Another common issue is forgetting exclusions and packaging .git, dependency folders, or local caches that should never leave the machine. Developers also often skip verification and discover too late that the archive is incomplete or incorrectly structured.

Summary

  • Decide whether the archive should include the top-level folder or only its contents.
  • Use zip -r on Unix-like systems and Compress-Archive on Windows.
  • Exclude metadata, caches, and dependency directories explicitly.
  • Verify the archive contents and integrity before sharing it.
  • Make repeated archive creation reproducible instead of ad hoc.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions