楚狂人
楚狂人

Reputation: 105

zip two file with same content, but final md5sum is different

I have the following operation on my mac:

$ echo "dgrgrrgrgrg" > test1.txt

after a few seconds, copy test1.txt:

$ cp test1.txt test2.txt
$ ls -l
total 16
-rw-r--r--  1 hqfy  staff  12 Mar 31 10:18 test1.txt
-rw-r--r--  1 hqfy  staff  12 Mar 31 10:19 test2.txt

now chech md5sum:

$ md5 *.txt
MD5 (test1.txt) = 8bab5a3e202c901499d83cb25d5a8c80
MD5 (test2.txt) = 8bab5a3e202c901499d83cb25d5a8c80

it's obvious that test1.txt and test2.txt have the same md5sum, now I zip these two files:

$ zip -X test1.zip test1.txt
    adding: test1.txt (deflated 8%)
$ zip -X test2.zip test2.txt
    adding: test2.txt (deflated 8%)
$ ls -l
total 32
-rw-r--r--  1 hqfy  staff   12 Mar 31 10:18 test1.txt
-rw-r--r--  1 hqfy  staff  127 Mar 31 10:22 test1.zip
-rw-r--r--  1 hqfy  staff   12 Mar 31 10:19 test2.txt
-rw-r--r--  1 hqfy  staff  127 Mar 31 10:23 test2.zip

size of test1.zip and test2.zip are the same, but when I check md5sum:

$ md5 *.zip
MD5 (test1.zip) = af8783f96ce98aef717ecf6229ffb07e
MD5 (test2.zip) = 59e752a03a2930adbe7f30b9cbf14561

I've googled it, using zip with option -X, but it did not work in my case, how can I create the two zip files with the same md5sum?

Upvotes: 5

Views: 2257

Answers (2)

S.S. Anne
S.S. Anne

Reputation: 15576

I know that this question is very old, but I may have an answer for you: The timestamps for the two files (which are very obviously different) are included in the .zip file. That is why the md5sums are different. If you can somehow remove those timestamps, then the md5sums will be the same.

Also note that macOS adds a folder (__MACOSX) to a zip file that contains extra metadata and such. That may also be the issue.

Upvotes: 3

vidit
vidit

Reputation: 6451

Quoting from the zip man page here..

With -X, zip strips all old fields and only includes the Unicode and Zip64 extra fields (currently these two extra fields cannot be disabled).

So, a different md5sum is expected when zipping (even with -X).

Upvotes: 3

Related Questions