Diff files inside zip without extracting

Is there a way to perform diff operetion on two files in two ZIP files without extracting them? If not, is there any other workaround to compare them without extracting?

Thanks.

+9
source share
5 answers

unzip -l display the contents of the zip file. Then you can pass this diff usual way, as indicated here: https://askubuntu.com/questions/229447/how-do-i-diff-the-output-of-two-commands

So, for example, if you had two ZIP files:

 foo.zip bar.zip 

You can run diff -y <(unzip -l foo.zip) <(unzip -l bar.zip) to scatter the contents of the two files in parallel.

Hope this helps!

+6
source

By combining the answers so far, the following bash function will compare file lists from zip files. Lists include verbose output ( unzip -v ), so checksums can be compared. The output is sorted by file name ( sort -k8 ) to allow side-by-side comparison and diff extension ( W200 ) so that the file names are visible inside from the side.

 function zipdiff() { diff -W200 -y <(unzip -vql $1 | sort -k8) <(unzip -vql $2 | sort -k8); } 

This can be added to your ~/.bashrc file, which will be used from any console. It can be used with zipdiff a.zip b.zip . For large zip files, it is useful to redirect the output to a smaller one or redirect to a file.

+6
source

If you want to diff two files (as you see in the difference), you must extract them - even if only in memory!

To see the diff of two files in two ZIP files, you can do something like this (without error checking or not at all):

 # define a little bash function function zipdiff () { diff -u <(unzip -p $1 $2) <(unzip -p $3 $4); } # test it: create a.zip and b.zip, each with a different file.txt echo hello >file.txt; zip a.zip file.txt echo world >file.txt; zip b.zip file.txt zipdiff a.zip file.txt b.zip file.txt --- /dev/fd/63 2016-02-23 18:18:09.000000000 +0100 +++ /dev/fd/62 2016-02-23 18:18:09.000000000 +0100 @@ -1 +1 @@ -hello +world 

Note: unzip -p extracts files in p ipe (stdout).

If you want to know if the files are different, you can check their checksums using

 unzip -v -l zipfile [file_to_inspect] 

Note: -v means verbose and -l list contents)

 unzip -v -l a.zip Archive: a.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 6 Stored 6 0% 2016-02-23 18:23 363a3020 file.txt -------- ------- --- ------- 6 6 0% 1 file unzip -v -l b.zip Archive: b.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 6 Stored 6 0% 2016-02-23 18:23 dd3861a8 file.txt -------- ------- --- ------- 6 6 0% 1 file 

In the above example, you can see that the checksums (CRC-32) are different.

You may also be interested in this project: https://github.com/nhnb/zipdiff

+4
source

If you just need to check if the files are equal, you can compare the CRC32 checksums that are stored in the fields of the local archive header / central directory.

0
source

I wanted the actual diff between files in zip codes in a human-readable format. Here is the bash function I wrote for this purpose that uses git. This is a good UX if you are already using git as part of your normal workflow and can read git diff.

 # usage: zipdiff before.zip after.zip function zipdiff { current=$(pwd) before="$current/$1" after="$current/$2" tempdir=$(mktemp -d) cd $tempdir git init &> /dev/null unzip -qq $before * git add . &> /dev/null git commit -m "before" &> /dev/null rm -rf $tempdir/* yes | unzip -qq $after * &> /dev/null git add . git diff --cached cd $current rm -rf $tempdir } 
0
source

All Articles