.zip files themselves, many common formats are ZIP archives in disguise: Java .jar files, Android .apk packages, Microsoft Office .docx and .xlsx documents, and OpenDocument .odt files are all valid ZIP archives that Binary Internals can open and parse. The ZIPFile parser locates the End of Central Directory Record at the end of the file, uses it to find the Central Directory, and then maps each Central Directory entry back to its Local File Header at the front of the file. All three structures are exposed as annotated, interactive tree nodes.
File Extension
.zip — registered by the ZIP plugin. Any file opened with a .zip extension will be dispatched to ZIPFile.
ZIP File Structure Overview
A standard ZIP file has three main regions, processed in reverse order by the parser:PK\x05\x06 signature (bytes 0x50 0x4B 0x05 0x06) to find the End of Central Directory Record, then jumps to the Central Directory offset, and finally seeks to each Local File Header using the RelativeOffsetOfLocalHeader stored in the Central Directory.
End of Central Directory Record
The End of Central Directory Record is always located at or near the end of the file. It provides the information needed to parse everything else:
The
EntryTotalNumber and CentralDirectoryOffset fields drive all subsequent parsing. If EntryTotalNumber is zero, the file is a valid but empty archive and no further structures are parsed.
Central Directory Structure
OneCentralDirectoryStructure entry exists per file in the archive. It contains all the metadata about the entry as well as a pointer back to the Local File Header.
FileHeader fields
Local File Header
Each file’s data is preceded by aLocalFileHeader immediately before the compressed bytes. The layout is similar to the Central Directory entry but without the VersionMadeBy, DiskNumberStart, per-file comment, and internal/external attributes fields.
The
LocalFileHeader total length is 30 + FileNameLength + ExtraFieldLength bytes, followed immediately by CompressedSize bytes of compressed file data.
General Purpose Bit Flag
The 16-bitGeneralPurposeBitFlag is expanded in the tree into individual bit nodes. Both the LocalFileHeader and CentralDirectoryStructure.FileHeader expose this flag. Some notable bits:
Known Signatures
The minimum valid ZIP file length is 22 bytes (the bare minimum for an empty archive containing only the End of Central Directory Record). Files shorter than 22 bytes are rejected with a
FileFormatException.