官方文档
https://pkware.cachefly.net/webdocs/APPNOTE/APPNOTE-6.2.0.txt
格式说明
在官方文档中给出的ZIP格式如下:
Overall .ZIP file format:
[local file header 1]
[file data 1]
[data descriptor 1]
.
.
.
[local file header n]
[file data n]
[data descriptor n]
[archive decryption header] (EFS)
[archive extra data record] (EFS)
[central directory]
[zip64 end of central directory record]
[zip64 end of central directory locator]
[end of central directory record]
通常情况下,我们用到的ZIP文件格式:
[local file header + file data + data descriptor]{1,n} + central directory + end of central directory record
即
[文件头+文件数据+数据描述符]{此处可重复n次}+核心目录+目录结束标识
当压缩包中有多个文件时,就会有多个[文件头+文件数据+数据描述符]
本片文章讨论的就是这种通常用到的ZIP文件格式,若想了解完整的ZIP文件格式,请看官方文档。
压缩源文件数据区
[local file header + file data + data descriptor]
记录着压缩的所有文件的内容信息,每个压缩文件都由local file header 、file data、data descriptor三部分组成,在这个数据区中每一个压缩的源文件/目录都是一条记录。
### local file header 文件头
用于标识该文件的开始,记录了该压缩文件的信息。
Offset | Bytes | Description | 译 |
---|---|---|---|
0 | 4 | Local file header signature = 0x04034b50 (read as a little-endian number) | 文件头标识,值固定(0x04034b50) |
4 | 2 | Version needed to extract (minimum) | 解压文件所需 pkware最低版本 |
6 | 2 | General purpose bit flag | 通用比特标志位(置比特0位=加密,详情见后) |
8 | 2 | Compression method | 压缩方式(详情见后) |
10 | 2 | File last modification time | 文件最后修改时间 |
12 | 2 | File last modification date | 文件最后修改日期 |
14 | 4 | CRC-32 | CRC-32校验码 |
18 | 4 | Compressed size | 压缩后的大小 |
22 | 4 | Uncompressed size | 未压缩的大小 |
26 | 2 | File name length (n) | 文件名长度 |
28 | 2 | Extra field length (m) | 扩展区长度 |
30 | n | File name | 文件名 |
30+n | m | Extra field | 扩展区 |
general purpose bit flag: (2 bytes) 通用位标记
Bit 0: If set, indicates that the file is encrypted.
(For Method 6 - Imploding)
Bit 1: If the compression method used