首页 > 代码库 > Zip文件格式
Zip文件格式
官方文档
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 | 2 | CRC-32 | CRC-32校验码 |
18 | 4 | Compressed size | 压缩后的大小 |
22 | 4 | Uncompressed size | 未压缩的大小 |
26 | 4 | 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 was type 6,
Imploding, then this bit, if set, indicates
an 8K sliding dictionary was used. If clear,
then a 4K sliding dictionary was used.
Bit 2: If the compression method used was type 6,
Imploding, then this bit, if set, indicates
3 Shannon-Fano trees were used to encode the
sliding dictionary output. If clear, then 2
Shannon-Fano trees were used.
(For Methods 8 and 9 - Deflating)
Bit 2 Bit 1
0 0 Normal (-en) compression option was used.
0 1 Maximum (-exx/-ex) compression option was used.
1 0 Fast (-ef) compression option was used.
1 1 Super Fast (-es) compression option was used.
Note: Bits 1 and 2 are undefined if the compression
method is any other.
Bit 3: If this bit is set, the fields crc-32, compressed
size and uncompressed size are set to zero in the
local header. The correct values are put in the
data descriptor immediately following the compressed
data. (Note: PKZIP version 2.04g for DOS only
recognizes this bit for method 8 compression, newer
versions of PKZIP recognize this bit for any
compression method.)
Bit 4: Reserved for use with method 8, for enhanced
deflating.
Bit 5: If this bit is set, this indicates that the file is
compressed patched data. (Note: Requires PKZIP
version 2.70 or greater)
Bit 6: Strong encryption. If this bit is set, you should
set the version needed to extract value to at least
50 and you must also set bit 0. If AES encryption
is used, the version needed to extract value must
be at least 51.
Bit 7: Currently unused.
Bit 8: Currently unused.
Bit 9: Currently unused.
Bit 10: Currently unused.
Bit 11: Currently unused.
Bit 12: Reserved by PKWARE for enhanced compression.
Bit 13: Used when encrypting the Central Directory to indicate
selected data values in the Local Header are masked to
hide their actual values. See the section describing
the Strong Encryption Specification for details.
Bit 14: Reserved by PKWARE.
Bit 15: Reserved by PKWARE.
compression method: (2 bytes) 压缩方式
(see accompanying documentation for algorithm
descriptions)
0 - The file is stored (no compression)
1 - The file is Shrunk
2 - The file is Reduced with compression factor 1
3 - The file is Reduced with compression factor 2
4 - The file is Reduced with compression factor 3
5 - The file is Reduced with compression factor 4
6 - The file is Imploded
7 - Reserved for Tokenizing compression algorithm
8 - The file is Deflated
9 - Enhanced Deflating using Deflate64(tm)
10 - PKWARE Data Compression Library Imploding
11 - Reserved by PKWARE
12 - File is compressed using BZIP2 algorithm
file data 文件数据
记录了相应压缩文件的数据
data descriptor 数据描述符
用于标识该文件压缩结束,该结构只有在相应的local file header中通用标记字段的第3bit设为1时才会出现,紧接在压缩文件源数据后。这个数据描述符只用在不能对输出的 ZIP 文件进行检索时使用。例如:在一个不能检索的驱动器(如:磁带机上)上的 ZIP 文件中。如果是磁盘上的ZIP文件一般没有这个数据描述符。
Offset | Bytes | Description | 译 |
---|---|---|---|
0 | 4 | crc-32 | CRC-32校验码 |
4 | 4 | compressed size | 压缩后的大小 |
8 | 4 | uncompressed size | 未压缩的大小 |
Central directory 核心目录
记录了压缩文件的目录信息,在这个数据区中每一条纪录对应在压缩源文件数据区中的一条数据。
核心目录结构如下:
Offset | Bytes | Description | 译 |
---|---|---|---|
0 | 4 | Central directory file header signature = 0x02014b50 | 核心目录文件header标识=(0x02014b50) |
4 | 2 | Version made by | 压缩所用的pkware版本 |
6 | 2 | Version needed to extract (minimum) | 解压所需pkware的最低版本 |
8 | 2 | General purpose bit flag | 通用位标记 |
10 | 2 | Compression method | 压缩方法 |
12 | 2 | File last modification time | 文件最后修改时间 |
14 | 2 | File last modification date | 文件最后修改日期 |
16 | 4 | CRC-32 | CRC-32校验码 |
20 | 4 | Compressed size | 压缩后的大小 |
24 | 4 | Uncompressed size | 未压缩的大小 |
28 | 2 | File name length (n) | 文件名长度 |
30 | 2 | Extra field length (m) | 扩展域长度 |
32 | 2 | File comment length (k) | 文件注释长度 |
34 | 2 | Disk number where file starts | 文件开始位置的磁盘编号 |
36 | 2 | Internal file attributes | 内部文件属性 |
38 | 4 | External file attributes | 外部文件属性 |
42 | 4 | relative offset of local header | 本地文件头的相对位移 |
46 | n | File name | 目录文件名 |
46+n | m | Extra field | 扩展域 |
46+n+m | k | File comment | 文件注释内容 |
End of central directory record(EOCD) 目录结束标识
目录结束标识存在于整个归档包的结尾,用于标记压缩的目录数据的结束。每个压缩文件必须有且只有一个EOCD记录。
Offset | Bytes | Description | 译 |
---|---|---|---|
0 | 4 | End of central directory signature = 0x06054b50 | 核心目录结束标记(0x06054b50) |
4 | 2 | Number of this disk | 当前磁盘编号 |
6 | 2 | number of the disk with the start of the central directory | 核心目录开始位置的磁盘编号 |
8 | 2 | total number of entries in the central directory on this disk | 该磁盘上所记录的核心目录数量 |
10 | 2 | total number of entries in the central directory | 核心目录结构总数 |
12 | 2 | Size of central directory (bytes) | 核心目录的大小 |
16 | 4 | offset of start of central directory with respect to the starting disk number | 核心目录开始位置相对于archive开始的位移 |
20 | 2 | .ZIP file comment length(n) | 注释长度 |
22 | n | .ZIP Comment |
Zip文件格式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。