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

