CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Archiving & Compression / zip

zip

Create ZIP archives that open everywhere

Beginnerarchivecompressioncloud

zip packs and compresses files into a .zip archive in one step. It's the format Windows and macOS open natively, and the one AWS Lambda, browsers and Java (jar, war) expect. Each file is compressed separately, so you can update or delete members in place.

Syntaxzip [OPTIONS] ARCHIVE.zip FILE... (zip -r for directories)

Watch it run

A colleague on Windows needs the app source, without node_modules.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
zip -r shop-api.zip app -x 'app/node_modules/*'

One line per member. 'deflated 63%' means that file shrank by 63%; directories are 'stored' as empty entries. The exclude pattern is quoted so zip, not the shell, matches it.

Anatomy of a command

$
commandzip

Info-ZIP's zip: creates or updates a .zip archive.

How it works

A ZIP file stores each member as a local header (name, method, CRC32, sizes, DOS timestamp) followed by its data, compressed on its own with DEFLATE (or just stored if compression doesn't help). At the very end comes the central directory: a list of every member with the offset of its local header, plus an end-of-central-directory record. Readers start from the end, read the central directory and jump straight to the member they want: random access, which a .tar.gz can't do. Because members are compressed separately, zip can add, replace (-u, -f) or delete (-d) members by rewriting the archive without recompressing the others, but similar files can't share compression context, so the ratio is worse than tar.gz for many small similar files. Unix permissions are kept in the 'external attributes' field; owners are not. zip -e uses the old ZipCrypto scheme, which is easy to break.

Options you'll actually use

-rRecurse into directories
-qQuiet
-x PATTERNExclude matching files
-i PATTERNInclude only matching files
-jJunk paths: store files without their directory names
-u / -fUpdate changed and add new files / only refresh existing members
-dDelete members from an existing archive
-0 ... -9Store only ... best compression (default 6)
-yStore symlinks as links instead of the files they point to
-TTest the archive after writing it
-eEncrypt with a password (weak ZipCrypto)

Common mistakes

zip app.zip app

Without -r only the directory entry is added; the archive is essentially empty.

zip -r app.zip app
zip -r function.zip build/ for a Lambda package

Files end up under build/ inside the zip and Lambda can't find the handler.

cd build && zip -r ../function.zip .
Relying on zip -e to protect secrets

ZipCrypto is broken; known-plaintext attacks recover the key quickly.

gpg -c file, age, or 7z -p with AES-256
Using zip to back up a Linux server

zip doesn't store owners, and handles special files and hard links poorly.

tar (with --numeric-owner) or rsync

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create logs.zip from the ~/logs directory, including everything inside it.

  2. 2

    Create app.zip from ~/app but leave out every .log file.

  3. 3

    Remove one file from app.zip without recreating it, then test the archive.

Related commands

unzipList, test and extract ZIP archivestarBundle files into an archive and extract themgzipCompress and decompress single files with gzip
PreviousxzNextunzip
Linux / Archiving & Compression / zip

zip

Create ZIP archives that open everywhere

Beginnerarchivecompressioncloud

zip packs and compresses files into a .zip archive in one step. It's the format Windows and macOS open natively, and the one AWS Lambda, browsers and Java (jar, war) expect. Each file is compressed separately, so you can update or delete members in place.

Syntaxzip [OPTIONS] ARCHIVE.zip FILE... (zip -r for directories)

Watch it run

A colleague on Windows needs the app source, without node_modules.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
zip -r shop-api.zip app -x 'app/node_modules/*'

One line per member. 'deflated 63%' means that file shrank by 63%; directories are 'stored' as empty entries. The exclude pattern is quoted so zip, not the shell, matches it.

Anatomy of a command

$
commandzip

Info-ZIP's zip: creates or updates a .zip archive.

How it works

A ZIP file stores each member as a local header (name, method, CRC32, sizes, DOS timestamp) followed by its data, compressed on its own with DEFLATE (or just stored if compression doesn't help). At the very end comes the central directory: a list of every member with the offset of its local header, plus an end-of-central-directory record. Readers start from the end, read the central directory and jump straight to the member they want: random access, which a .tar.gz can't do. Because members are compressed separately, zip can add, replace (-u, -f) or delete (-d) members by rewriting the archive without recompressing the others, but similar files can't share compression context, so the ratio is worse than tar.gz for many small similar files. Unix permissions are kept in the 'external attributes' field; owners are not. zip -e uses the old ZipCrypto scheme, which is easy to break.

Options you'll actually use

-rRecurse into directories
-qQuiet
-x PATTERNExclude matching files
-i PATTERNInclude only matching files
-jJunk paths: store files without their directory names
-u / -fUpdate changed and add new files / only refresh existing members
-dDelete members from an existing archive
-0 ... -9Store only ... best compression (default 6)
-yStore symlinks as links instead of the files they point to
-TTest the archive after writing it
-eEncrypt with a password (weak ZipCrypto)

Common mistakes

zip app.zip app

Without -r only the directory entry is added; the archive is essentially empty.

zip -r app.zip app
zip -r function.zip build/ for a Lambda package

Files end up under build/ inside the zip and Lambda can't find the handler.

cd build && zip -r ../function.zip .
Relying on zip -e to protect secrets

ZipCrypto is broken; known-plaintext attacks recover the key quickly.

gpg -c file, age, or 7z -p with AES-256
Using zip to back up a Linux server

zip doesn't store owners, and handles special files and hard links poorly.

tar (with --numeric-owner) or rsync

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create logs.zip from the ~/logs directory, including everything inside it.

  2. 2

    Create app.zip from ~/app but leave out every .log file.

  3. 3

    Remove one file from app.zip without recreating it, then test the archive.

Related commands

unzipList, test and extract ZIP archivestarBundle files into an archive and extract themgzipCompress and decompress single files with gzip
PreviousxzNextunzip