# 7-Zip — High-Ratio Archiving with 7z

> Practical guide to 7-Zip: pack 7z, ZIP, TAR and more, squeeze files with LZMA2 and protect archives with AES-256 encryption.

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

<!-- PROSE:intro -->
7-Zip is an open-source archiver whose LZMA/LZMA2 algorithm often achieves noticeably higher compression ratios than classic ZIP or gzip tools. On the command line it packs and extracts not only its own `.7z` format but also ZIP, TAR, GZIP, BZIP2 and XZ. This guide walks you through the commands you reach for daily – from a simple archive to AES-256 encryption, multi-volume archives and exclude patterns.
<!-- PROSE:intro:end -->

## Create Archives

`7z a <archive> <files>` — Create an archive (format detected from extension).

```bash
7z a backup.7z documents/
```

`7z a <archive>.zip <files>` — Create a ZIP archive.

```bash
7z a project.zip src/
```

`7z a -mx=<level> <archive> <files>` — Set compression level (0=store, 1=fastest, 5=normal, 9=ultra).

```bash
7z a -mx=9 best.7z largefile.dat
```

`7z a -t<type> <archive> <files>` — Specify archive type (7z, zip, tar, gzip, bzip2, xz).

```bash
7z a -ttar archive.tar directory/
```

`7z a -r <archive> '<pattern>'` — Add files recursively matching a pattern.

```bash
7z a code.7z -r '*.py'
```

## Extract

`7z x <archive>` — Extract with full paths (preserves directory structure).

```bash
7z x backup.7z
```

`7z x <archive> -o<dir>` — Extract to a specific directory.

```bash
7z x backup.7z -o/tmp/restore
```

`7z e <archive>` — Extract without paths (all files into one directory).

```bash
7z e backup.7z
```

`7z x <archive> '<file>'` — Extract specific files only.

```bash
7z x backup.7z 'config.yaml' 'README.md'
```

`7z x -y <archive>` — Extract and overwrite without prompting.

```bash
7z x -y update.7z -o/var/www/
```

## List & Test

`7z l <archive>` — List contents of an archive.

```bash
7z l backup.7z
```

`7z l -slt <archive>` — List with technical details (size, date, attributes).

```bash
7z l -slt backup.7z
```

`7z t <archive>` — Test archive integrity.

```bash
7z t backup.7z
```

## Encryption

`7z a -p <archive> <files>` — Create an encrypted archive (prompts for password).

```bash
7z a -p secret.7z confidential/
```

`7z a -p'<password>' <archive> <files>` — Create encrypted archive with inline password.

```bash
7z a -p'MySecret123' secret.7z data/
```

`7z a -p -mhe=on <archive> <files>` — Encrypt file names too (not just content).

```bash
7z a -p -mhe=on secret.7z confidential/
```

## Split & Update

`7z a -v<size> <archive> <files>` — Create a split (multi-volume) archive.

```bash
7z a -v100m backup.7z large-directory/
```

`7z u <archive> <files>` — Update files in an existing archive.

```bash
7z u backup.7z updated-file.txt
```

`7z d <archive> '<file>'` — Delete files from an archive.

```bash
7z d backup.7z 'temp.log'
```

`7z rn <archive> '<old>' '<new>'` — Rename a file inside an archive.

```bash
7z rn backup.7z 'old-name.txt' 'new-name.txt'
```

## Exclude & Common Patterns

`7z a <archive> <dir> -x!'<pattern>'` — Exclude files matching a pattern.

```bash
7z a project.7z src/ -x!'*.log' -x!'node_modules'
```

`7z a <archive> <dir> -xr!'<pattern>'` — Recursively exclude a pattern.

```bash
7z a project.7z ./ -xr!'*.git' -xr!'node_modules'
```

`7z a -ttar -so . | 7z a -si backup.tar.7z` — Create a .tar.7z archive (tar + 7z compression).

```bash
7z a -ttar -so directory/ | 7z a -si backup.tar.7z
```

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

On the command line, 7-Zip is a Swiss Army knife for archives – but mind the crucial difference between `7z x` (restores the full directory structure) and `7z e` (extracts every file flat into one directory, which can overwrite same-named files). AES-256 encryption is only available for `.7z` and `.zip` archives protected with a password; add `-mhe=on` to encrypt the file names too. Avoid passing the password inline via `-p'…'`, as it then ends up in your shell history and the process list – the interactive `-p` prompt is safer.

## Further Reading

- [7-zip.org – official project site](https://www.7-zip.org/) – downloads, release notes and FAQ
- [7-Zip – command-line documentation](https://documentation.help/7-Zip/) – reference for all switches and commands
<!-- PROSE:outro:end -->

## Related Commands

- [gzip](https://www.jpkc.com/db/en/cheatsheets/archives/gzip/) – compresses single files with DEFLATE
- [tar](https://www.jpkc.com/db/en/cheatsheets/archives/tar/) – bundles files into a single archive, often combined with compression
- [zip](https://www.jpkc.com/db/en/cheatsheets/archives/zip/) – creates and extracts classic ZIP archives

