Linux commands
Browse topics

Packing and extracting ZIP archives with zip and unzip

Version
unversioned
Review
Draft

Summary#

zip creates a ZIP archive and unzip lists or extracts one. The format is common for exchanging files with Windows and macOS.

Syntax#

sh
zip -r site.zip site/
unzip -l site.zip
unzip -t site.zip
unzip site.zip -d restored/
unzip -p site.zip site/index.html

-r includes directories. -l lists entries, -t tests them, -d chooses the destination and -p writes a member to standard output.

Example#

sh
(
  set -eu
  dir=$(mktemp -d)
  trap 'rm -rf -- "$dir"' EXIT
  cd "$dir"
  mkdir -p site/css
  echo '<h1>Hi</h1>' > site/index.html
  echo 'h1 {}' > site/css/app.css

  zip -q -r site.zip site
  unzip -Z1 site.zip | LC_ALL=C sort
  unzip -q site.zip -d restored
  cat restored/site/index.html
)

Expected output#

Text
site/
site/css/
site/css/app.css
site/index.html
<h1>Hi</h1>

Common mistakes#

Running zip -r again on an existing archive updates it and keeps entries for files you have since removed; delete the archive first for a fresh copy. List an unfamiliar archive with unzip -l and extract it into an empty directory, so it cannot overwrite your files. End-of-central-directory signature not found means the file is not a complete ZIP archive, often a partial download or an HTML error page saved with a .zip name. cannot find or open means the path is wrong. ZIP does not reliably keep Unix ownership; use tar for system backups.

Version applicability#

This entry describes Info-ZIP Zip 3.0 and UnZip 6.0.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0