Skip to content
Aback ZIP

Deep dives · 11 min read · Updated

Inside a ZIP File, Byte by Byte

A guided walk through a ZIP's local headers, file data, central directory and end record, with a small worked example and the reason each field exists.

A ZIP file is three things in sequence: a local header and data for each file, a central directory describing all entries, and a short end record that points to that directory. Read in that order, the format is surprisingly straightforward, and knowing it explains why some archives fail and why inspection can be fast.

The three-part structure

Most binary formats are easiest to understand by drawing a map. A ZIP's map looks like this, with offsets measured from the start of the file.

[local header 1][file data 1][local header 2][file data 2]...
[central directory header 1][central directory header 2]...
[end of central directory record]

The central directory repeats much of what the local headers say. That redundancy is deliberate: a reader can jump to the end of the file, read the index and learn everything about the archive without decompressing a single entry. This is why ZIP Viewer can list a large archive quickly.

The local file header

Each entry begins with a 30-byte fixed header, followed immediately by the file name and an optional extra field. Multi-byte numbers are stored little-endian: the least significant byte comes first.

OffsetSizeFieldPurpose
04Signature 0x04034b50Identifies a local file header; bytes appear as 50 4B 03 04
42Version neededMinimum ZIP feature level required to extract
62General purpose flagsEncryption, data descriptor and filename encoding flags
82Compression method0 = stored, 8 = Deflate, and other numeric identifiers
102Last-modified timeDOS time: hours, minutes and seconds/2
122Last-modified dateDOS date: year from 1980, month and day
144CRC-32Checksum of the uncompressed bytes
184Compressed sizeNumber of data bytes that follow
224Uncompressed sizeSize after decompression
262File name lengthBytes in the name that follows the header
282Extra field lengthBytes of extra data after the name
30variableFile name and extra fieldUTF-8 name when the flag is set, plus optional structured fields

The CRC-32 is a checksum, not a cryptographic hash. It catches accidental corruption. The CRC-32 guide explains what it does and does not prove.

File data and data descriptors

Immediately after the name and extra field come the compressed bytes. When method 0 is used, those bytes are the original file exactly. With Deflate, they are a raw compressed stream as defined in RFC 1951, not a gzip stream and not a zlib stream with its own header.

If a writer does not know the final size and CRC before streaming the data, it sets a flag in the local header and writes a data descriptor after the file data. That descriptor repeats the CRC and sizes. A reader must honour the flag; assuming that the local header always contains correct sizes is a common cause of extraction bugs.

This streaming detail is why some archives can still be recovered when the central directory is damaged: a repair tool can scan for local headers and read each entry in sequence.

The central directory

After all entries comes the central directory. Each record is 46 bytes plus a variable name, extra field and comment. It repeats the CRC, sizes, method, flags and time, and adds fields the local header does not have.

FieldWhat it adds
Version made byWhich ZIP implementation and host system wrote the entry
Disk number startWhich part of a multi-disk archive holds the entry
Internal attributesHints about the file type
External attributesHost-specific information such as Unix permission bits
Relative offset of local headerWhere the entry's local header starts, measured from the beginning of the archive
File commentAn optional short text field for this entry

The offset field is the key to fast random access: a reader can pick one entry from the index, seek to its local header and decompress only that file. This is the basis of selective extraction in Extract ZIP.

The end of central directory record

The last structure is the end of central directory record, or EOCD. It is 22 bytes plus an optional archive comment. It records the disk numbers, the number of entries on this disk and in total, the size of the central directory and its offset from the start of the archive.

Readers normally search backwards from the end of the file for the EOCD signature. A truncated download or a broken comment can make that search fail, which is why a damaged ZIP often produces the message that the archive is invalid. Aback ZIP's repair tool scans for local headers when the index cannot be trusted.

A worked example: one tiny stored ZIP

Consider an archive containing one file named a.txt with the two bytes Hi, stored without compression and dated 1 January 1980. The bytes look like this, grouped by structure. Offsets are decimal.

offset 0   local header
50 4B 03 04   signature PK\x03\x04
14 00         version needed 2.0
00 00         flags
00 00         method 0 (stored)
00 00         DOS time 00:00:00
21 00         DOS date 1980-01-01
0E 0E 17 4D   CRC-32 of "Hi" = 0x4D170E0E
02 00 00 00   compressed size 2
02 00 00 00   uncompressed size 2
05 00         name length 5
00 00         extra length 0
61 2E 74 78 74   "a.txt"
48 69         file data "Hi"

offset 37  central directory header
50 4B 01 02   signature PK\x01\x02
14 00         version made by
14 00         version needed
00 00 00 00 00 00 21 00   flags, method, time, date
0E 0E 17 4D   CRC-32
02 00 00 00   compressed size
02 00 00 00   uncompressed size
05 00 00 00 00 00 00 00   name, extra and comment lengths
00 00 00 00 00 00 00 00   disk, internal and external attributes
25 00 00 00   local header offset 37
61 2E 74 78 74   "a.txt"

offset 88  end of central directory
50 4B 05 06   signature PK\x05\x06
00 00 00 00 00 00 01 00 01 00   disk numbers and entry counts
33 00 00 00   central directory size 51
25 00 00 00   central directory offset 37
00 00         comment length 0

Check the arithmetic: the local header is 30 bytes, the name is 5 and the data is 2, so the central directory starts at offset 37. It is 46 bytes plus the 5-byte name, giving 51 bytes. The EOCD therefore begins at 37 + 51 = 88. Every offset in the file follows from those three numbers.

Change the compression method to 8 and the same structure holds; only the data bytes and the method field change. Add a second file and a second pair of local header and central directory records appear. The design scales linearly, which is why a ZIP with a few hundred entries is still simple to parse.

Where ZIP64 fits

Classic ZIP fields are 32 bits, which caps stored sizes at just under 4 GB and counts at 65,535. ZIP64 solves this with 64-bit size and offset fields carried in extra fields, plus a ZIP64 end-of-central-directory record and locator. A reader that does not understand ZIP64 will misread the placeholder values in the classic fields.

The ZIP64 explained guide covers the practical implications. For an implementation, the rule is simple: check for ZIP64 structures before trusting a 32-bit size, or you will report a large archive as corrupt.

Why this matters for everyday tools

  • Inspection is cheap because the central directory holds the index. You do not have to decompress files to list them.
  • Corruption has a location. A bad EOCD, a bad local header and a bad CRC each fail at a different stage, which is why a precise error message is useful.
  • Selective extraction is possible because each entry has an offset in the index.
  • Unsafe names are just strings. Path security is the tool's responsibility, not something the format provides.
  • Extra fields carry modern features, from timestamps to ZIP64 and encryption markers. Ignoring them is how tools silently lose metadata.

Aback ZIP's Test ZIP Integrity tool decompresses every entry and recomputes its CRC, while ZIP Viewer reads only the index. Those are different operations built on different parts of this layout. If you want to see why rebuilding an archive can change its size without changing its contents, continue with why your ZIP is not getting smaller.

The authoritative reference is PKWARE's APPNOTE, which defines every field and method identifier. RFC 1951 defines the Deflate stream used inside most entries. Small worked examples like the one above are useful for orientation; the specification is what implementations must follow.

Tools for this

Frequently asked questions

So a reader can learn the whole archive's contents from one place near the end, without decompressing entries or scanning from the beginning. It enables fast listings and selective extraction.

More posts