Is there a safe way to run a diff on two zip compressed files?

前端 未结 13 1038
长发绾君心
长发绾君心 2020-12-16 12:49

Seems this would not be a deterministic thing, or is there a way to do this reliably?

相关标签:
13条回答
  • 2020-12-16 13:17

    If you're using gzip, you can do something like this:

    # diff <(zcat file1.gz) <(zcat file2.gz)
    
    0 讨论(0)
  • 2020-12-16 13:17

    Beyond compare has no problem with this.

    0 讨论(0)
  • 2020-12-16 13:18

    I generally use an approach like @mrabbit's but run 2 unzip commands and diff the output as required. For example I need to compare 2 Java WAR files.

    $ sdiff --width 160 \
       <(unzip -l -v my_num1.war | cut -c 1-9,59-,49-57 | sort -k3) \
       <(unzip -l -v my_num2.war | cut -c 1-9,59-,49-57 | sort -k3)
    

    Resulting in output like so:

    --------          -------                                                       --------          -------
    Archive:                                                                        Archive:
    -------- -------- ----                                                          -------- -------- ----
    48619281          130 files                                                   | 51043693          130 files
        1116 060ccc56 index.jsp                                                         1116 060ccc56 index.jsp
           0 00000000 META-INF/                                                            0 00000000 META-INF/
         155 b50f41aa META-INF/MANIFEST.MF                                        |      155 701f1623 META-INF/MANIFEST.MF
     Length   CRC-32  Name                                                           Length   CRC-32  Name
        1179 b42096f1 version.jsp                                                       1179 b42096f1 version.jsp
           0 00000000 WEB-INF/                                                             0 00000000 WEB-INF/
           0 00000000 WEB-INF/classes/                                                     0 00000000 WEB-INF/classes/
           0 00000000 WEB-INF/classes/com/                                                 0 00000000 WEB-INF/classes/com/
    ...
    ...
    
    0 讨论(0)
  • 2020-12-16 13:18

    I gave up trying to use existing tools and wrote a little bash script that works for me:

    #!/bin/bash
    # Author: Onno Benschop, onno@itmaze.com.au
    # Note: This requires enough space for both archives to be extracted in the tempdir
    
    if [ $# -ne 2 ] ; then
      echo Usage: $(basename "$0") zip1 zip2
      exit
    fi
    
    # Make temporary directories
    archive_1=$(mktemp -d)
    archive_2=$(mktemp -d)
    
    # Unzip the archives
    unzip -qqd"${archive_1}" "$1"
    unzip -qqd"${archive_2}" "$2"
    
    # Compare them
    diff -r "${archive_1}" "${archive_2}"
    
    # Remove the temporary directories
    rm -rf "${archive_1}" "${archive_2}"
    
    0 讨论(0)
  • 2020-12-16 13:20

    This isn't particularly elegant, but you can use the FileMerge application that comes with Mac OS X developer tools to compare the contents of zip files using a custom filter.

    Create a script ~/bin/zip_filemerge_filter.bash with contents:

    #!/bin/bash
    ##
    #  List the size, CR-32 checksum, and file path of each file in a zip archive,
    #  sorted in order by file path.
    ##
    unzip -v -l "${1}" | cut -c 1-9,59-,49-57 | sort -k3
    exit $?
    

    Make the script executable (chmod +x ~/bin/zip_filemerge_filter.bash).

    Open FileMerge, open the Preferences, and go to the "Filters" tab. Add an item to the list with: Extension:"zip", Filter:"~/bin/zip_filemerge_filter.bash $(FILE)", Display: Filtered, Apply*: No. (I've also added the filer for .jar and .war files.)

    Then use FileMerge (or the command line "opendiff" wrapper) to compare two .zip files.

    This won't let you diff the contents of files within the zip archives, but will let you quickly see which files appear within one only archive and which files exist in both but have different content (i.e. different size and/or checksum).

    0 讨论(0)
  • 2020-12-16 13:22

    WinMerge (windows only) has lots of features and one of them is:

    • Archive file support using 7-Zip
    0 讨论(0)
提交回复
热议问题