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

前端 未结 4 474
失恋的感觉
失恋的感觉 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:33

    Use below script to create deterministic zip or jar files

    #!/bin/bash
    
    usage() {
        echo "Usage : ./createDeterministicArtifact.sh "
        exit 1
    }
    
    info() {
        echo "$1"
    }
    
    strip_artifact() {
        if [ -z ${file} ]; then
            usage
        fi
        if [ -f ${file} -a -s ${file} ]; then
            mkdir -p ${file}.tmp
            unzip -oq -d ${file}.tmp ${file}
            find ${file}.tmp -follow -exec touch -a -m -t 201912010000.00 {} \+
            if [ "$UNAME" == "Linux" ] ; then
                find ${file}.tmp -follow -exec chattr -a {} \+
            elif [[ "$UNAME" == CYGWIN* || "$UNAME" == MINGW* ]] ; then
                find ${file}.tmp -follow -exec attrib -A {} \+
            fi
            cd ${file}.tmp
            zip -rq -D -X -9 -A --compression-method deflate  ../${file}.new . 
            cd -
            rm -rf ${file}.tmp
            info "Recreated deterministic artifact: ${file}.new"
        else 
            info "Input file is empty. Please validate the file and try again"
        fi
    }
    
    file=$1
    strip_artifact
    

提交回复
热议问题