ZIP文件格式是一种数据压缩和文档储存的文件格式,原名Deflate,发明者为菲尔·卡茨(Phil Katz),他于1989年1月公布了该格式的资料。ZIP通常使用后缀名“.zip”,它的MIME格式为application/zip。目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7z格式。
ZIP官方文档中对ZIP文件格式作了总结:
|
|
即zip主要由三部分组成:
压缩源文件数据区 | 核心目录 | 目录结束 |
---|---|---|
local file header + file data + data descriptor | central directory | end of central directory record |
我创建了一个压缩包,结构如下:
这里
NewFile.txt
hellpabc.sh
文件头 local file header
格式如下:
Offset | Bytes | Description | 译 |
---|---|---|---|
0 | 4 | Local file header signature = 0x04034b50 (read as a little-endian number) |
文件头标识,值固定0x 504B0304 小端模式 |
4 | 2 | Version needed to extract (minimum) | 解压文件所需 pkware最低版本 |
6 | 2 | General purpose bit flag | 通用比特标志位 |
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 | 4 | File name length (n) | 文件名长度 |
28 | 2 | Extra field length (m) | 扩展区长度 |
30 | n | File name | 文件名 |
30+n | m | Extra field | 扩展区 |
可以看到选中的部分为文件名字段,选中了7字节
,对应前面文件名长度的0x 0700
,16进制转字符串之后就是这里/
.
文件数据 file data
这里
是我们文件夹的名字,跟在其后面的又是一个新的块。
这里在压缩是选择加密,可以发现,内容都变为了密文
数据描述 data descriptor
数据描述符用于标识该文件压缩结束,该结构只有在相应的local file header
中通用标记字段的第3bit
设为1
时才会出现,紧接在压缩文件源数据后。
Offset | Bytes | Description | 译 |
---|---|---|---|
0 | 4 | 固定值0x 504b0708 作为数据描述符开头,可以没有 |
|
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 | 核心目录文件header标识0x 504B0102 |
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记录。
Offset | Bytes | Description | 译 |
---|---|---|---|
0 | 4 | End of central directory signature | 核心目录结束标记504B0506 |
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 | 注释内容 |