文件解码:zip-6.2

本文详细介绍了XLSX文件的结构,包括ZIP文件的组成部分和压缩文件数据区、压缩文件目录区、压缩文件结束标志等。通过解析XML来理解表结构、表格式和表数据,展示了如何利用shell命令和Excel功能处理批量数据。内容涉及文件头、文件数据、数据描述符、中央目录等多个关键元素,为自定义Excel处理工具提供了基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

说出来你可能不信,最近经常用excel写代码,经过image2lcd批量处理的图片资源,再往回读就没有批量的工具链了,但又没有到非得写一个工具的程度,所以利用一些shell命令,比如tree获得目录下所有文件,就能直接导入到excel里,现在excel能识别换行,把下一行的内容放到下面的表格上,然后用LEFT或者RIGHT来处理字符串,再用&来拼凑,无论include还是extern,都非常简便,对非序列的资源特别友好。

很多年前,我还在做仓库管理的时候,就想做excel格式的解码,因为从oa上导出的表格,需要经过许多处理才能成为能用的原始数据,再导回oa,又需要按照给定的格式重新编造表格,导入模块做得又特别烂,只是insert,根本就不是update,就想自己写导入导出,甚至可以做一版简易的管理系统。

要做这些,excel的文件解码是绕不过的,excel有两种文件格式,一种是2003以前支持的xls,一种是之后支持的xlsx,从复杂度上说,xls显然更简单一些,但是xls巨量的宏病毒当年就让我疲于奔命,所以打算从xlsx开始。

XLSX格式

xlsx格式核心用xml来描述表结构、表格式、表数据,然后用zip来压缩。所以要解码xlsx格式,需要先解码zip格式。

ZIP文件结构

一个完整的ZIP文件总体是这样的

    [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]

主要由3类数据组成

压缩文件数据区EFS压缩文件目录区压缩文件结束标志
组成local files header(0x04034b50)、files data、data decriptorarchive decryption header、archive extra data record(0x08064b50)central directory(0x02014b50)、digital signature(0x05054b50)、zip64 end of central directory record(0x06064b50)、zip64 end of central directory locator(0x07064b50)end of central directory record(0x06054b50)

1.压缩源文件数据区

压缩源文件区用来记录被打包前的文件的信息,以记录的形式组织,每一个文件或目录(在FAT中,目录就是特殊的文件记录)对应一条记录

源文件记录结构
part1part2part3
文件头文件数据数据描述
local file headerfile datadata descriptor
①.文件头结构(local file header )
offsetname作用长度(字节)注解
0x0local file header signature文件头标记40x04034b50
0x4version needed to extract解压文件所需 pkware 版本2
0x6general purpose bit flag全局方式位标记2
0x8compression method压缩方式2
0xalast mod file time最后修改文件时间2
0xclast mod file date最后修改文件日期2
0xecrc-32CRC-32校验4
0x12compressed size压缩后尺寸4
0x16uncompressed size未压缩尺寸4
0x1afile name length文件名长度2
0x1cextra field length扩展记录长度2
-file name文件名(不定长度)取决于扩展记录长度
-extra field扩展字段(不定长度)取决于扩展记录长度
②.文件数据(file data)

跟在文件头后,文件的压缩或者存储数据

③.数据描述符结构(data descriptor)

这个部分是否存在取决于文件头0x6的全局方式位标记的第三位是否置位。

offsetname作用长度(字节)注解
0x0crc-32CRC-32校验4
0x4compressed size压缩后尺寸4
0x8uncompressed size未压缩尺寸4

EFS

6.2版本中引入的格式规范

Archive extra data record: (EFS)

offsetname作用长度(字节)注解
0x0archive extra data signature4(0x08064b50)
0x4extra field length4
0x8extra field data(variable size)

2.压缩源文件目录区

该区由三个部分组成

part1part2part3
0x02014b50、0x05054b500x06064b500x07064b50
central directoryzip64 end of central directory recordzip64 end of central directory locator
①.central directory

由两个部分组成,file header和 digital signature。

  [file header 1]
  .
  .
  . 
  [file header n]
  [digital signature] 
Ⅰ.file header结构
offsetname作用长度(字节)注解
0x0central file header signature目录中文件文件头标记40x02014b50
0x4version made by压缩使用的 pkware 版本2
0x6version needed to extract解压文件所需 pkware 版本2
0x8general purpose bit flag全局方式位标记2
0xacompression method压缩方式2
0xclast mod file time最后修改文件时间2
0xelast mod file date最后修改文件日期2
0x10crc-32CRC-32校验4
0x14compressed size压缩后尺寸4
0x18uncompressed size未压缩尺寸4
0x1cfile name length文件名长度2
0x1eextra field length扩展字段长度2
0x20file comment length文件注释长度2
0x22disk number start磁盘开始号2
0x24internal file attributes内部文件属性2
0x26external file attributes外部文件属性4
0x2arelative offset of local header局部头部偏移量4
-file name文件名(不定长度)取决于扩展记录长度
-extra field扩展字段(不定长度)取决于扩展记录长度
-file comment文件注释(不定长度)取决于扩展记录长度
Ⅱ.digital signature结构
offsetname作用长度(字节)注解
0x0header signature文件头标记40x05054b50
0x4size of data数据大小2
0x6signature data数据(不定长度)取决于数据大小
②.Zip64 end of central directory record
offsetname作用长度(字节)注解
0x0zip64 end of central dir signature4(0x06064b50)
0x4size of zip64 end of central directory record8
0xcversion made by2
0xeversion needed to extract2
0x10number of this disk4
0x14number of the disk with the start of the central directory4
0x18total number of entries in the central directory on this disk8
0x20total number of entries in the central directory8
0x28size of the central directory8
0x30offset of start of central directory with respect to the starting disk number8
0x38zip64 extensible data sector(variable size)
③.Zip64 end of central directory locator
offsetname作用长度(字节)注解
0x0zip64 end of central dir locator signature4(0x07064b50)
0x4number of the disk with the start of the zip64 end of central directory4
0x8relative offset of the zip64 end of central directory record8
0x10total number of disks4

3.压缩源文件目录结束标志

offsetname作用长度(字节)注解
0x0end of central dir signature目录结束标记40x06054b50
0x4number of this disk当前磁盘编号2
0x6number of the disk with the start of the central directory目录区开始磁盘编号2
0x8total number of entries in the central directory on this disk本磁盘上纪录总数2
0xatotal number of entries in the central directory目录区中纪录总数2
0xcsize of the central directory目录区尺寸大小4
0x10offset of start of central directory with respect to the starting disk number目录区对第一张磁盘的偏移量4
0x14.ZIP file comment lengthZIP 文件注释长度2
0x16.ZIP file commentZIP 文件注释不定长度

参考文献

[1] 官方文档 -APPNOTE-6.2.0.txt
[2] ZIP文件格式分析-saltor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值