Why does Zipping the same content twice gives two files with different SHA1?

前端 未结 4 472
失恋的感觉
失恋的感觉 2021-01-03 21:11

I have run into a strange problem with git and zip files. My build script takes a bunch of documentation html pages and zips them into a docs.zip I then check this file into

4条回答
  •  时光取名叫无心
    2021-01-03 21:35

    I had success on creating files with the same SHA1 using the -X (--no-extra) flag for zip.

    I created a folder and a couple of files to zip to test it, and as expected, getting different SHA1 hashes everytime:

    $ mkdir stuff
    $ echo "Stuff 1" > stuff/stuff1.txt
    $ echo "Stuff 2" > stuff/stuff2.txt
    $ zip -r stuff.zip stuff/
      adding: stuff/ (stored 0%)
      adding: stuff/stuff1.txt (stored 0%)
      adding: stuff/stuff2.txt (stored 0%)
    
    $ shasum stuff.zip
    1c8be43ac859bb57603be1243da14022710d22bd  stuff.zip
    
    $ shasum stuff.zip
    1c8be43ac859bb57603be1243da14022710d22bd  stuff.zip
    
    $ zip -r stuff.zip stuff/
    updating: stuff/ (stored 0%)
    updating: stuff/stuff1.txt (stored 0%)
    updating: stuff/stuff2.txt (stored 0%)
    
    $ shasum stuff.zip
    73920362d0f7de74d87286502e03e7126fdc0a6a  stuff.zip
    

    However, using -X gets me the same hash after consecutive zipping:

    $ zip -r -X stuff.zip stuff/
    updating: stuff/ (stored 0%)
    updating: stuff/stuff1.txt (stored 0%)
    updating: stuff/stuff2.txt (stored 0%)
    
    $ shasum stuff.zip
    1ed228b16d1ee803f26a8b1419f2eb3bf7fcb9f5  stuff.zip
    
    $ zip -r -X stuff.zip stuff/
    updating: stuff/ (stored 0%)
    updating: stuff/stuff1.txt (stored 0%)
    updating: stuff/stuff2.txt (stored 0%)
    
    $ shasum stuff.zip
    1ed228b16d1ee803f26a8b1419f2eb3bf7fcb9f5  stuff.zip
    

    I don't have the time to dig in and find out which extra info is causing the difference to popup in the first case, but maybe this could be helpful to someone trying to solve it. Also only tested on macOS 10.12.6.

提交回复
热议问题