当tar相同的两个包时,其md5不相同

0 linux compression tar md5sum

有两个包,里面有一个文件。

\n
[root@build-production test]# ll\ndrwxr-xr-x. 2 root root 19 9\xe6\x9c\x88  18 07:58 pack1\ndrwxr-xr-x. 2 root root 19 9\xe6\x9c\x88  18 07:58 pack2\n[root@build-production test]# ll pack1\n-rw-r--r--. 1 root root 2 9\xe6\x9c\x88  18 07:58 file1\n[root@build-production test]# ll pack2\n-rw-r--r--. 1 root root 2 9\xe6\x9c\x88  18 07:58 file2\n
Run Code Online (Sandbox Code Playgroud)\n

并且文件具有相同的md5。

\n
[root@build-production test]# md5sum  pack1/file1 pack2/file2\nb026324c6904b2a9cb4b88d6d61c81d1  pack1/file1\nb026324c6904b2a9cb4b88d6d61c81d1  pack2/file2\n
Run Code Online (Sandbox Code Playgroud)\n

但是当我使用下面的命令压缩每个包时,它们的 tarball 的 md5 有所不同。

\n
root@build-production test]# GZIP=-n tar zchf  pack1.tar.gz pack1 --mtime=\'2023-01-01 00:00:00\'\n[root@build-production test]# GZIP=-n tar zchf  pack2.tar.gz pack2 --mtime=\'2023-01-01 00:00:00\'\n[root@build-production test]#\n[root@build-production test]# md5sum pack1.tar.gz pack2.tar.gz\n629e155e9853f17cbd3dbdb5daf05240  pack1.tar.gz\nbca577bda8bc22d600dd91fffc51574c  pack2.tar.gz\n
Run Code Online (Sandbox Code Playgroud)\n

这是预期的结果吗?有没有办法让压缩文件的md5值一致?

\n

Ton*_*nny 5

原始文件名(和路径)存储在 tar 文件内。
这些不相同(pack1/file1 与 pack2/file2),导致 tar 文件有 2 个不同的哈希值。这完全符合预期。

为了使两个 tar 文件具有相同的 md5 哈希值,它们的内容必须完全相同。如果将整个结构放入 tar 文件中,则不仅存储的文件内容,而且文件/目录的目录结构和时间戳/权限也必须匹配。