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

Python tarfile与shell tar命令生成的tar包体积差异排查

Tar包体积差异排查:Python tarfile vs GNU tar

使用webdataset生成的000000.tar(大小18MB),通过GNU tar命令解压后重新打包为example.tar(大小15MB),两者包含的文件内容完全一致,但原包体积比重新打包的包大3MB。由于webdataset基于Python内置tarfile库生成tar包,推测是tarfile与GNU tar的实现差异导致,以下是具体排查方向:

补充信息

操作命令

(base) {22-09-01 2:25}vm:~/playground chenyaofo% ls -lha
total 18M
drwxrwxr-x  2 chenyaofo chenyaofo 4.0K Sep  1 02:25 .
drwxr-xr-x 39 chenyaofo chenyaofo 4.0K Sep  1 02:25 ..
-rw-rw-r--  1 chenyaofo chenyaofo  18M Sep  1 02:19 000000.tar
(base) {22-09-01 2:25}vm:~/playground chenyaofo% mkdir images
(base) {22-09-01 2:25}vm:~/playground chenyaofo% tar -xf 000000.tar -C images
(base) {22-09-01 2:26}vm:~/playground chenyaofo% tar -cf example.tar -C images .
(base) {22-09-01 2:26}vm:~/playground chenyaofo% ls -lha
total 33M
drwxrwxr-x  3 chenyaofo chenyaofo 4.0K Sep  1 02:26 .
drwxr-xr-x 39 chenyaofo chenyaofo 4.0K Sep  1 02:26 ..
-rw-rw-r--  1 chenyaofo chenyaofo  18M Sep  1 02:19 000000.tar
-rw-rw-r--  1 chenyaofo chenyaofo  15M Sep  1 02:26 example.tar
drwxrwxr-x  2 chenyaofo chenyaofo 160K Sep  1 02:26 images

系统与依赖版本

  • 系统:Ubuntu 22.04(内核Linux vm 5.15.0-39-generic #42-Ubuntu SMP Thu Jun 9 23:42:32 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux)
  • Python:3.9.13
  • webdataset:0.2.20
  • GNU tar:tar (GNU tar) 1.34

具体排查方向

  • 块大小与对齐策略:tar包的块大小和填充对齐方式是体积差异的常见原因。使用tar tvvf <tar-file>命令查看两个包的块占用细节,对比每个文件记录的块数、填充字节量。Python tarfile默认块大小可能为10240字节,而GNU tar默认可能使用512字节块并更紧凑地对齐。
  • 元数据存储细节:
    • 对比两个包的文件元数据(用户/组ID、名称、时间戳、权限等),使用tar --list --verbose --verbose <tar-file>查看详细元数据,检查是否存在冗余存储(比如同时存储ID和名称)或额外扩展属性。
    • 检查是否有GNU tar默认省略的元数据项(比如atime访问时间,Python tarfile可能默认保留,而GNU tar默认只记录mtime修改时间)。
  • tar格式版本差异:使用file <tar-file>命令查看两个包的格式类型(比如ustar、gnu、pax),不同格式的头部长度和扩展支持不同,可能导致体积差异。Python tarfile默认可能使用ustar格式,而GNU tar默认使用gnu格式。
  • 空内容与隐藏项:检查原tar包是否包含空文件、空目录或webdataset添加的额外索引/元数据项,这些内容在解压后可能被忽略或丢弃,重新打包时不再包含。使用tar tf 000000.tar和tar tf example.tar对比文件列表。
  • 二进制结构对比:用hexdump或cmp -l工具对比两个tar包的二进制内容,定位差异区域,判断是头部元数据差异还是填充字节差异,从而锁定具体原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 03:50:26