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 文件头
用于标识该文件的开始,记录了该压缩文件的信息。

OffsetBytesDescription
04Local file header signature = 0x04034b50 (read as a little-endian number)文件头标识,值固定(0x04034b50)
42Version needed to extract (minimum)解压文件所需 pkware最低版本
62General purpose bit flag通用比特标志位(置比特0位=加密,详情见后)
82Compression method压缩方式(详情见后)
102File last modification time文件最后修改时间
122File last modification date文件最后修改日期
144CRC-32CRC-32校验码
184Compressed size压缩后的大小
224Uncompressed size未压缩的大小
262File name length (n)文件名长度
282Extra field length (m)扩展区长度
30nFile name文件名
30+nmExtra 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文件一般没有这个数据描述符。

OffsetBytesDescription
04crc-32CRC-32校验码
44compressed size压缩后的大小
84uncompressed size未压缩的大小

## Central directory 核心目录
记录了压缩文件的目录信息,在这个数据区中每一条纪录对应在压缩源文件数据区中的一条数据。

核心目录结构如下:

OffsetBytesDescription
04Central directory file header signature = 0x02014b50核心目录文件header标识=(0x02014b50)
42Version made by压缩所用的pkware版本
62Version needed to extract (minimum)解压所需pkware的最低版本
82General purpose bit flag通用位标记
102Compression method压缩方法
122File last modification time文件最后修改时间
142File last modification date文件最后修改日期
164CRC-32CRC-32校验码
204Compressed size压缩后的大小
244Uncompressed size未压缩的大小
282File name length (n)文件名长度
302Extra field length (m)扩展域长度
322File comment length (k)文件注释长度
342Disk number where file starts文件开始位置的磁盘编号
362Internal file attributes内部文件属性
384External file attributes外部文件属性
424relative offset of local header本地文件头的相对位移
46nFile name目录文件名
46+nmExtra field扩展域
46+n+mkFile comment文件注释内容

End of central directory record(EOCD) 目录结束标识

目录结束标识存在于整个归档包的结尾,用于标记压缩的目录数据的结束。每个压缩文件必须有且只有一个EOCD记录。

OffsetBytesDescription
04End of central directory signature = 0x06054b50核心目录结束标记(0x06054b50)
42Number of this disk当前磁盘编号
62number of the disk with the start of the central directory核心目录开始位置的磁盘编号
82total number of entries in the central directory on this disk该磁盘上所记录的核心目录数量
102total number of entries in the central directory核心目录结构总数
124Size of central directory (bytes)核心目录的大小
164offset of start of central directory with respect to the starting disk number核心目录开始位置相对于archive开始的位移
202.ZIP file comment length(n)注释长度
22n.ZIP Comment注释内容

样例分析

这里只考虑最简单的一种场景,只包括一个文本文件的压缩文件。
新建一个名为123.txt的文本文件,内容为123456
将123.txt压缩为123.zip

使用winhex打开123.zip(注:winhex是以小端模式展示数据的)

这里写图片描述

OffsetBytesContentsDescriptor
LOCAL FILE HEADER
00000000450 4B 03 04文件头标识(0x04034b50)
0000000420A 00解压文件所需 pkware最低版本
00000006200 00通用比特标志位
00000008208 00压缩方式
0000000A2E1 5D文件最后修改时间
0000000C2CC 48文件最后修改日期
0000000E461 D3 72 09crc-32校验码
00000012408 00 00 00压缩后的大小
00000016406 00 00 00未压缩的大小
0000001A207 00文件名长度
0000001C200 00扩展区长度
0000001E631 32 33 2E 74 78 74文件名 123.txt
FILE DATA
00000025833 34 32 36 31 35 03 00压缩文件数据,此处就是压缩文本文件123.txt压缩后的数据
Central Directory Header
0000002D450 4B 01 02核心目录文件header标识=(0x02014b50)
0000003120A 00压缩所用的pkware版本
0000003320A 00解压所需pkware的最低版本
00000035200 00通用位标记
00000037208 00压缩方法
000000392E1 5D文件最后修改时间
0000003B2CC 48文件最后修改日期
0000003D461 D3 72 09CRC-32校验码
00000041408 00 00 00压缩后的大小
00000045406 00 00 00未压缩的大小
00000049207 00文件名长度
0000004B200 00扩展域长度
0000004D200 00文件注释长度
0000004F200 00文件开始位置的磁盘编号
00000051200 00内部文件属性
00000053420 00 00 00外部文件属性
00000057400 00 00 00本地文件header的相对位移
0000005B731 32 33 2E 74 78 74目录文件名
End of central directory record
00000062450 4B 05 06核心目录结束标记(0x06054b50)
00000066200 00当前磁盘编号
00000068200 00核心目录开始位置的磁盘编号
0000006A201 00该磁盘上所记录的核心目录数量
0000006C201 00核心目录结构总数
0000006E435 00 00 00核心目录的大小
0000007242D 00 00 00核心目录开始位置相对于archive开始的位移
00000076200 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

  • 28
    点赞
  • 50
    收藏
  • 打赏
    打赏
  • 13
    评论
Hex Editor Neo Ultimate 文版是一款功能非常强劲的二进制、十六进制文件编辑和查看工具,程序体积虽然不大,但包含了你所有需要用到的功能,非常的全面,界面也设计的非常的合理,非常容易使用。Hex Editor Neo Ultimate 可以创建帮助你编辑的 ASCII,十六进制,十进制,float,double 和二进制数据的应用程序。 Hex Editor Neo Ultimate 可以查看,修改,分析您的十六进制和二进制数据文件,编辑,与其他应用程序通过剪贴板,插入新的数据,交换数据和删除现有数据,以及执行其他编辑操作。只用鼠标点击补丁,操纵你的EXE,DLL,DAT,AVI,MP3播放,具有无限撤消/重做JPG文件。品味与分支可视化操作的历史。 16 进制编辑工具 Hex Editor Neo Ultimate Edition 文特别版16 进制编辑工具 Hex Editor Neo Ultimate Edition 文特别版 这十六进制和二进制代码数据编辑软件工具包括以下基本功能:无限次的撤销/重做;找到;更换;视觉历史保存和载入膜片创造;剪贴板操作,Bytes, Words, Double Words, Quad Words等双打编辑模式。 十六进制编辑器的使用范围:二进制文件,补丁,DLLs,AVI文件,MP3文件,JPG文件 16 进制编辑工具 Hex Editor Neo Ultimate Edition 文特别版16 进制编辑工具 Hex Editor Neo Ultimate Edition 文特别版 Hex Editor Neo Ultimate 主要特点: -支持使用正则表达式在替换模式 -替换和全部替换命令现在支持正则表达式作为替代模式,让您参考匹配的表达部位时,执行替换。 -支持所有的编码工作的命令与正则表达式 -查找,查找所有,替换,全部替换,在文件查找,在文件替换,统计模式 – 所有这些-命令现在完全支持目前选定的编码方式时,他们在正则表达式的工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

saltor

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值