7z (7-Zip archive)
| Full name | 7z archive (7-Zip) |
|---|---|
| Extensions | .7z, .7z.001, .7z.002... (multi-volume), .cb7 (comic book), .exe (self-extracting) |
| MIME type | application/x-7z-compressed |
| Type | archive, lossless compression, optional encryption |
| Starts with | 37 7A BC AF 27 1C (7z\xBC\xAF\x27\x1C) |
| Specification | 7zFormat.txt in the 7-Zip / LZMA SDK source |
| Maintained by | Igor Pavlov (7-Zip) |
| License | open; the format and the LZMA SDK are public domain, no patents |
7z is the native archive format of 7-Zip. It packs many files and folders into one file and compresses them, by default with LZMA2. It usually produces smaller archives than ZIP, especially for many similar files, because it can compress files together in solid blocks. It supports AES-256 encryption of file data and of file names, Unicode names, files larger than 4 GB and archives split into volumes.
7z is used for software distribution, backups, game mods, large data sets and comic books (.cb7). Its main downside is support: ZIP opens everywhere without extra software, 7z needs 7-Zip or another archiver on most systems (Windows 11 now opens it natively).
When to use 7z#
- Use when you want the smallest archive and the people receiving it have 7-Zip, PeaZip, NanaZip or another archiver.
- Use for many similar files (source code, logs, text): solid compression finds redundancy across files.
- Use when you need strong encryption including file names: AES-256 with an encrypted header hides the file list.
- Avoid when you need random access to single files in a large archive: in a solid block, extracting the last file decompresses everything before it. Create non-solid archives (
-ms=off) or use ZIP. - Avoid for streaming: the header is at the end of the file, so a 7z archive can’t be read or written as a stream (pipe). Use TAR with xz or zstd.
- Avoid when Unix permissions, owners and symlinks must be kept exactly: 7z stores Windows attributes; Unix modes only through an extension. Use TAR.
- Avoid when maximum compatibility matters (email attachments, downloads for non-technical users): use ZIP.
File structure#
A 7z file has 3 parts: a fixed 32-byte signature header, the packed streams (compressed file data), and the header that describes everything, stored at the end of the file. The signature header points to the end header, so a reader seeks to the end first.
+---------------------------+ offset 0
| Signature header (32 B) | magic, version, offset/size/CRC of the end header
+---------------------------+ offset 32
| Packed stream 0 | compressed data of folder (block) 0
| Packed stream 1 |
| ... |
+---------------------------+ 32 + NextHeaderOffset
| Header | file list, folders, coders, sizes, CRCs, names, times
| (often itself compressed) |
+---------------------------+ end of file
Signature header#
All integers in the signature header are little-endian.
| Offset | Size | Field | Description |
|---|---|---|---|
| 0 | 6 | Signature | 37 7A BC AF 27 1C |
| 6 | 2 | Version | major, minor: 00 04 in current files |
| 8 | 4 | StartHeaderCRC | CRC-32 of the next 20 bytes |
| 12 | 8 | NextHeaderOffset | offset of the header, counted from byte 32 |
| 20 | 8 | NextHeaderSize | size of the header in bytes |
| 28 | 4 | NextHeaderCRC | CRC-32 of the header |
A complete 139-byte archive made by 7-Zip with one file, hello.txt (13 bytes, “hello, world\n”):
37 7A BC AF 27 1C signature
00 04 version 0.4
F7 06 D1 70 StartHeaderCRC
11 00 00 00 00 00 00 00 NextHeaderOffset = 17
5A 00 00 00 00 00 00 00 NextHeaderSize = 90
19 49 C3 07 NextHeaderCRC
01 00 0C 68 65 6C 6C 6F packed stream (17 bytes): an LZMA2 "stored" chunk
2C 20 77 6F 72 6C 64 0A with the 13 bytes of hello.txt, then end marker 00
00
01 header (90 bytes) starts: kHeader
04 kMainStreamsInfo
06 00 01 09 11 00 kPackInfo: pack pos 0, 1 stream, size 17
07 0B 01 00 01 21 21 01 00 kUnpackInfo: 1 folder, 1 coder, id 21 = LZMA2, 1 property byte
0C 0D 00 unpacked size 13
08 0A 01 53 74 24 F4 00 00 kSubStreamsInfo: CRC-32 of the file F4247453
05 01 kFilesInfo: 1 file
19 0C 00 ... 00 kDummy (padding for alignment)
11 15 00 68 00 65 00 ... kName: "hello.txt" in UTF-16LE, 0-terminated
14 0A 01 00 88 04 DB B9 ... kMTime: Windows FILETIME
15 06 01 00 20 00 00 00 kWinAttributes: 0x20 = archive
00 00 end of FilesInfo, end of Header
The header#
The header is a tree of properties. Each property starts with a 1-byte ID; numbers inside use a variable-length encoding (1 to 9 bytes, the count of leading 1-bits in the first byte gives the number of extra bytes). Main property IDs:
| ID | Name | Contents |
|---|---|---|
01 |
kHeader | the plain header |
17 |
kEncodedHeader | the header is itself compressed (usually LZMA); this record says how to unpack it |
04 |
kMainStreamsInfo | PackInfo + UnpackInfo + SubStreamsInfo |
06 |
kPackInfo | position, number and sizes of packed streams |
07 |
kUnpackInfo | list of folders: the coder chain of each block and unpacked sizes |
08 |
kSubStreamsInfo | how many files are in each folder, their sizes and CRC-32 values |
05 |
kFilesInfo | number of files, then per-file properties |
0E, 0F |
kEmptyStream, kEmptyFile | directories and 0-byte files have no data stream |
11 |
kName | file names with relative paths, UTF-16LE, each ending with a 0 character |
12, 13, 14 |
kCTime, kATime, kMTime | creation, access, modification time (Windows FILETIME) |
15 |
kWinAttributes | Windows attributes; Unix mode in the high 16 bits when bit 0x8000 is set |
00 |
kEnd | ends the current record |
Most archives created by 7-Zip have an encoded header: the 32-byte signature header points to a small 17 record, which describes an LZMA stream that unpacks to the real 01 header. With header encryption (-mhe=on) that stream is also AES-encrypted, so the file names can’t be listed without the password.
Folders, coders and solid blocks#
A folder is one block of data passed through a chain of coders (compression, filters, encryption). Files are concatenated and compressed together inside a folder. When one folder holds many files the archive is solid: better compression, but extracting one file means decompressing everything before it in the same folder. 7-Zip limits solid block size by default (-ms=on, block size depends on level); -ms=off puts each file in its own folder.
Common method IDs:
| Method ID | Coder |
|---|---|
00 |
Copy (stored) |
21 |
LZMA2 (default) |
03 01 01 |
LZMA |
03 04 01 |
PPMd (good for text) |
04 02 02 |
BZip2 |
04 01 08, 04 01 09 |
Deflate, Deflate64 |
03 03 01 03, 03 03 01 1B |
BCJ (x86 filter), BCJ2 (x86 filter with 4 streams) |
0A, 0B |
ARM64, RISC-V filters |
03 |
Delta |
06 F1 07 01 |
AES-256 + SHA-256 (encryption) |
Filters like BCJ convert relative addresses in executable code to absolute ones before compression, which makes .exe and .dll files compress 10-20% better. Third-party builds (7-Zip ZS, NanaZip) add Zstandard, Brotli, LZ4 and others with their own IDs; standard 7-Zip can’t open archives that use them.
Encryption#
7z encrypts with AES-256 in CBC mode. The key is derived from the password (UTF-16LE) with 219 rounds of SHA-256. Only file data is encrypted unless the header is encrypted too. There is no authentication tag: a wrong password is usually detected by a CRC error after decompression.
Multi-volume and self-extracting archives#
- Volumes (
archive.7z.001,.002, …) are plain byte splits of one archive. Concatenate them (copy /borcat) to get a normal.7zfile. - A self-extracting archive is an executable stub (
7z.sfx,7zCon.sfx) followed by a normal 7z archive. Readers find the archive by searching for the signature.
Creating 7z files with the command line#
7z a archive.7z folder/ # create, LZMA2, level 5
7z a -mx=9 archive.7z folder/ # maximum compression (slower, more memory)
7z a -ms=off archive.7z folder/ # non-solid: fast access to single files
7z a -p -mhe=on secret.7z folder/ # ask for a password, encrypt data and file names
7z a -v100m archive.7z folder/ # split into 100 MB volumes
7z x archive.7z -ooutput/ # extract with full paths
7z l archive.7z # list contents
7z t archive.7z # test CRCs
On Linux the official 7-Zip command is 7zz (package 7zip); the older p7zip fork installs 7z.
Specifications and documentation#
- 7zFormat.txt: the format description by the author of 7-Zip, in the 7-Zip source tree
- LZMA SDK: public-domain reference code for LZMA, LZMA2, XZ and 7z decoding, with
7zFormat.txtandMethods.txt(all method IDs) - 7z format on 7-zip.org: features and compression methods
- 7-Zip command line syntax: switches such as
-mx,-ms,-mhe,-v - py7zr 7z format specification: a more detailed, readable description of the header structures
- 7z on Wikipedia
Software that opens 7z files#
| Software | Windows | macOS | Linux | Web | Can | Notes |
|---|---|---|---|---|---|---|
| SumatraPDF | ✓ | view | free, open source; shows images inside .7z and .cb7 comic book archives as pages | |||
| Windows File Explorer | ✓ | view, create | built into Windows 11 (23H2 opens 7z, 24H2 creates it); no encrypted archives | |||
| 7-Zip | ✓ | ✓ | ✓ | view, create, convert | free, open source (LGPL); the reference implementation; 7zz command line on Linux and macOS | |
| NanaZip | ✓ | view, create | free, open source 7-Zip fork for Windows 11 with Zstandard, Brotli and other extra methods | |||
| PeaZip | ✓ | ✓ | ✓ | view, create, convert | free, open source GUI over 7-Zip and others | |
| WinRAR | ✓ | view | commercial (trial); extracts 7z, creates only RAR and ZIP | |||
| Bandizip | ✓ | ✓ | view, create | free edition with ads, paid editions | ||
| The Unarchiver | ✓ | view | free, extracts 7z including encrypted ones | |||
| Keka | ✓ | view, create | free, open source; also in the Mac App Store (paid) | |||
| Ark | ✓ | view, create | free, open source KDE archiver | |||
| File Roller (Archive Manager) | ✓ | view, create | free, open source GNOME archiver; needs 7-Zip or p7zip installed for 7z | |||
| ZArchiver | view, create | free Android archiver. Also on Android | ||||
| ezyZip | ✓ | view, convert | free, extracts 7z in the browser without uploading |
Libraries#
Open-source libraries that read and write 7z archives. See all 7z libraries for a comparison.
| C/C++ | bit7z, libarchive, LZMA SDK |
|---|---|
| C# | SevenZipExtractor, SharpCompress |
| Java | Commons Compress |
| Go | sevenzip |
| Python | libarchive-c, py7zr |
| Ruby | ffi-libarchive, seven-zip |
| Rust | sevenz-rust2 |
| PHP | Archive7z |
Related formats#
- ZIP: opens everywhere without extra software; per-file compression, larger archives
- RAR: proprietary archive format with recovery records; creating it needs WinRAR
- TAR: Unix archive format, keeps permissions; usually compressed with gzip, xz (LZMA2, the same compression as 7z) or zstd
- XZ: single-file LZMA2 compression from the same author, used for
.tar.xz - CB7: comic book archive; a 7z archive of images