# zip — Create and Extract ZIP Archives

> Practical guide to zip and unzip: create, extract, compress and encrypt ZIP archives – the most universal cross-platform archive format.

Source: https://www.jpkc.com/db/en/cheatsheets/archives/zip/

<!-- PROSE:intro -->
zip and unzip are the classic tools for the ZIP format – the one archive format you can open virtually anywhere, from Windows to macOS to Linux. With `zip` you bundle and compress files in a single step; with `unzip` you get them back out. This guide walks you through the commands you reach for daily: recursive archiving, compression levels, exclude patterns, encryption and extracting individual files on demand.
<!-- PROSE:intro:end -->

## Create Archives

`zip <archive>.zip <files>` — Create a ZIP archive from files.

```bash
zip backup.zip file1.txt file2.txt
```

`zip -r <archive>.zip <dir>` — Create a ZIP archive from a directory (recursive).

```bash
zip -r project.zip project/
```

`zip -j <archive>.zip <files>` — Create archive without directory paths (junk paths).

```bash
zip -j flat.zip path/to/file1.txt path/to/file2.txt
```

`zip -<level> <archive>.zip <files>` — Set compression level (0=store, 1=fastest, 9=best).

```bash
zip -9 best.zip largefile.dat
```

`zip -0 <archive>.zip <files>` — Store files without compression (faster for pre-compressed data).

```bash
zip -0 images.zip *.jpg
```

## Add, Update & Delete

`zip -u <archive>.zip <files>` — Update: add new or changed files to an existing archive.

```bash
zip -u backup.zip newfile.txt
```

`zip -d <archive>.zip <files>` — Delete files from an existing archive.

```bash
zip -d backup.zip oldfile.txt
```

`zip -g <archive>.zip <files>` — Grow: append files to an existing archive.

```bash
zip -g backup.zip extra.txt
```

## Exclude & Filter

`zip -r <archive>.zip <dir> -x '<pattern>'` — Create archive excluding files matching a pattern.

```bash
zip -r project.zip project/ -x '*.git*' '*node_modules*'
```

`zip -r <archive>.zip <dir> -x '*.log' '*.tmp'` — Exclude multiple file patterns.

```bash
zip -r deploy.zip src/ -x '*.log' '*.tmp' '*.test.js'
```

`zip -r <archive>.zip <dir> -i '*.php' '*.html'` — Include only files matching patterns.

```bash
zip -r code.zip project/ -i '*.php' '*.html' '*.css'
```

## Encryption & Split

`zip -e <archive>.zip <files>` — Create a password-encrypted ZIP archive.

```bash
zip -e -r secret.zip confidential/
```

`zip -P '<password>' <archive>.zip <files>` — Create encrypted archive with inline password (insecure in history).

```bash
zip -P 'mypass' secret.zip file.txt
```

`zip -s <size> -r <archive>.zip <dir>` — Create a split archive (multi-part).

```bash
zip -s 100m -r large.zip bigfolder/
```

## unzip — Extract

`unzip <archive>.zip` — Extract all files from a ZIP archive.

```bash
unzip backup.zip
```

`unzip <archive>.zip -d <dir>` — Extract to a specific directory.

```bash
unzip backup.zip -d /tmp/restore/
```

`unzip <archive>.zip '<file>'` — Extract only specific files.

```bash
unzip backup.zip 'config.yaml'
```

`unzip -o <archive>.zip` — Overwrite existing files without prompting.

```bash
unzip -o update.zip -d /var/www/
```

`unzip -n <archive>.zip` — Never overwrite existing files.

```bash
unzip -n backup.zip
```

`unzip -l <archive>.zip` — List contents of a ZIP archive without extracting.

```bash
unzip -l backup.zip
```

`unzip -t <archive>.zip` — Test archive integrity without extracting.

```bash
unzip -t backup.zip
```

`unzip -p <archive>.zip '<file>'` — Extract a file to stdout (pipe to another command).

```bash
unzip -p backup.zip config.json | jq .
```

<!-- PROSE:outro -->
## Conclusion

The ZIP format wins on ubiquity above all: if you need to hand an archive to someone on another platform, `zip` is almost always a safe bet. Don't rely on its built-in encryption, though – the legacy ZipCrypto scheme (`zip -e`, `zip -P`) is considered weak and easy to break, and `-P` writes your password straight into the shell history. For genuine confidentiality, reach for AES-capable tools such as 7-Zip, or age and GPG.

## Further Reading

- [Info-ZIP – official project site](http://www.info-zip.org/) – home of zip and unzip, with documentation and source code
- [zip(1) – Linux man page](https://linux.die.net/man/1/zip) – complete option reference for the zip command
<!-- PROSE:outro:end -->

## Related Commands

- [7z](https://www.jpkc.com/db/en/cheatsheets/archives/7z/) – high-compression archiver with strong AES-256 encryption
- [gzip](https://www.jpkc.com/db/en/cheatsheets/archives/gzip/) – fast stream compression for single files
- [tar](https://www.jpkc.com/db/en/cheatsheets/archives/tar/) – bundles directory trees into one archive (often paired with gzip)

