You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

压缩两个内容一致的目录为何得到不同的二进制文件?

问题:相同内容的目录重新压缩后MD5校验/文件不一致的原因?

操作环境

  • Mac系统
  • 系统预装工具:zip 3.0,unzip 6.0

操作步骤

步骤1:第一次处理PPTX文件

从普通pptx文件开始,解压、整理XML后重新压缩:

unzip V1.pptx -d dir
cd dir
find . -name "*.xml" -type f -exec xmllint --output '{}' --format '{}' \;
zip -0 ../V1Orig.pptx -r *

得到压缩文件V1Orig.pptx。

步骤2:第二次处理PPTX文件

将V1Orig.pptx解压、再次整理XML后重新压缩:

unzip V1Orig.pptx -d copy
cd copy
find . -name "*.xml" -type f -exec xmllint --output '{}' --format '{}' \;
zip -0 ../V1Copy.pptx -r *

出现的现象

  1. 目录内容对比一致:
Common subdirectories: orig/_rels and copy/_rels
Common subdirectories: orig/docProps and copy/docProps
Common subdirectories: orig/ppt and copy/ppt
  1. 压缩文件对比不一致:
diff V1Orig.pptx V1Copy.pptx
Binary files V1Orig.pptx and V1Copy.pptx differ

查看两个目录的文件列表(注意文件/文件夹的顺序):

ls -rtla orig
total 8
drwxr-xr-x  11 fultonm  wheel   352 10 Jan 16:49 ppt
drwxr-xr-x   5 fultonm  wheel   160 10 Jan 16:49 docProps
drwxr-xr-x   3 fultonm  wheel    96 10 Jan 16:49 _rels
drwxr-xr-x   6 fultonm  wheel   192 14 Jan 10:40 .
-rw-r--r--   1 fultonm  wheel  3212 14 Jan 10:42 [Content_Types].xml
drwxr-xr-x   8 fultonm  wheel   256 14 Jan 10:57 ..
fultonm@mikes-MacBook-Pro-2 /tmp/handzip>ls -rtla copy
total 8
drwxr-xr-x   5 fultonm  wheel   160 14 Jan 10:42 docProps
drwxr-xr-x   3 fultonm  wheel    96 14 Jan 10:42 _rels
drwxr-xr-x   6 fultonm  wheel   192 14 Jan 10:42 .
drwxr-xr-x  11 fultonm  wheel   352 14 Jan 10:42 ppt
-rw-r--r--   1 fultonm  wheel  3212 14 Jan 10:42 [Content_Types].xml
drwxr-xr-x   8 fultonm  wheel   256 14 Jan 10:57 ..

原因解析

这是因为zip压缩包的差异来自两个核心因素:

  1. 文件在压缩包中的存储顺序:
    使用zip -r *时,shell会按照当前目录下文件/文件夹的排序结果依次添加内容到压缩包。从ls -rtla的输出可以看到,orig和copy目录里的文件夹顺序明显不同(orig是ppt→docProps→_rels,copy是docProps→_rels→ppt),这直接导致压缩包内的文件存储顺序不一样,最终生成的zip文件自然不同。

  2. 文件的元数据差异:
    zip压缩包会记录文件的元数据(比如修改时间、权限等)。orig目录里的ppt、docProps、_rels文件夹修改时间是1月10日,而copy目录里的所有文件夹修改时间都是1月14日,这些元数据的差异也会被写入zip文件,即使文件内容完全一致,也会导致压缩包的MD5值和二进制内容不同。

内容的提问来源于stack exchange,提问作者mike

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 00:40:18