ZIP文件格式分析
2016-06-12 14:55
417 查看
官方文档
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 | 注释内容 |
样例分析
这里只考虑最简单的一种场景,只包括一个文本文件的压缩文件。新建一个名为123.txt的文本文件,内容为123456
将123.txt压缩为123.zip
使用winhex打开123.zip(注:winhex是以小端模式展示数据的)
Offset | Bytes | Contents | Descriptor |
---|---|---|---|
LOCAL FILE HEADER | |||
00000000 | 4 | 50 4B 03 04 | 文件头标识(0x04034b50) |
00000004 | 2 | 0A 00 | 解压文件所需 pkware最低版本 |
00000006 | 2 | 00 00 | 通用比特标志位 |
00000008 | 2 | 08 00 | 压缩方式 |
0000000A | 2 | E1 5D | 文件最后修改时间 |
0000000C | 2 | CC 48 | 文件最后修改日期 |
0000000E | 4 | 61 D3 72 09 | crc-32校验码 |
00000012 | 4 | 08 00 00 00 | 压缩后的大小 |
00000016 | 4 | 06 00 00 00 | 未压缩的大小 |
0000001A | 2 | 07 00 | 文件名长度 |
0000001C | 2 | 00 00 | 扩展区长度 |
0000001E | 6 | 31 32 33 2E 74 78 74 | 文件名 123.txt |
FILE DATA | |||
00000025 | 8 | 33 34 32 36 31 35 03 00 | 压缩文件数据,此处就是压缩文本文件123.txt压缩后的数据 |
Central Directory Header | |||
0000002D | 4 | 50 4B 01 02 | 核心目录文件header标识=(0x02014b50) |
00000031 | 2 | 0A 00 | 压缩所用的pkware版本 |
00000033 | 2 | 0A 00 | 解压所需pkware的最低版本 |
00000035 | 2 | 00 00 | 通用位标记 |
00000037 | 2 | 08 00 | 压缩方法 |
00000039 | 2 | E1 5D | 文件最后修改时间 |
0000003B | 2 | CC 48 | 文件最后修改日期 |
0000003D | 4 | 61 D3 72 09 | CRC-32校验码 |
00000041 | 4 | 08 00 00 00 | 压缩后的大小 |
00000045 | 4 | 06 00 00 00 | 未压缩的大小 |
00000049 | 2 | 07 00 | 文件名长度 |
0000004B | 2 | 00 00 | 扩展域长度 |
0000004D | 2 | 00 00 | 文件注释长度 |
0000004F | 2 | 00 00 | 文件开始位置的磁盘编号 |
00000051 | 2 | 00 00 | 内部文件属性 |
00000053 | 4 | 20 00 00 00 | 外部文件属性 |
00000057 | 4 | 00 00 00 00 | 本地文件header的相对位移 |
0000005B | 7 | 31 32 33 2E 74 78 74 | 目录文件名 |
End of central directory record | |||
00000062 | 4 | 50 4B 05 06 | 核心目录结束标记(0x06054b50) |
00000066 | 2 | 00 00 | 当前磁盘编号 |
00000068 | 2 | 00 00 | 核心目录开始位置的磁盘编号 |
0000006A | 2 | 01 00 | 该磁盘上所记录的核心目录数量 |
0000006C | 2 | 01 00 | 核心目录结构总数 |
0000006E | 4 | 35 00 00 00 | 核心目录的大小 |
00000072 | 4 | 2D 00 00 00 | 核心目录开始位置相对于archive开始的位移 |
00000076 | 2 | 00 00 | 注释长度 |
参考文献
[1] 官方文档 - APPNOTE-6.2.0.txt[1] zip文件格式分析 - A Flying Bird - 博客频道 - CSDN.NET
[2] zip文件格式说明_elly_新浪博客
[3] ZIP文件格式详解 - sony303的专栏 - 博客频道 - CSDN.NET
[4] ZIP文件格式详解 - 董盼山的专栏 - 博客频道 - CSDN.NET
[5] zip格式分析 - lianghongge的专栏 - 博客频道 - CSDN.NET
相关文章推荐
- Python中zip()函数用法举例
- 在 Linux 中使用 SoundConverter 轻松转换音频文件格式
- 如何在 Ubuntu Linux 中使用 RAR 文件
- zip 的压缩原理与实现
- rm格式插入广告代码
- C#判断指定驱动器是否是Fat分区格式的方法
- C#使用DeflateStream解压缩数据文件的方法
- C# 利用ICSharpCode.SharpZipLib实现在线压缩和解压缩
- 用vbs实现zip功能的脚本
- C#使用WinRar命令进行压缩和解压缩操作的实现方法
- C#调用WinRar执行rar、zip压缩的方法
- C语言压缩文件和用MD5算法校验文件完整性的实例教程
- C#实现页面GZip或Deflate压缩的方法
- C#使用iCSharpcode进行文件压缩实现方法
- C#图片切割、图片压缩、缩略图生成代码汇总
- C#简易图片格式转换器实现方法
- C#判断指定分区是否是ntfs格式的方法
- C#实现压缩HTML代码的方法
- Asp.net在线备份、压缩和修复Access数据库示例代码
- C#实现判断图形文件格式的方法