30

diff command compares to see any difference betwenn two files. Can the same be used to compare two zip files, i.e if there is any difference in data ,like counts etc in individual files in the zipped files?

3
  • 1
    Is the order in which files are included in the zip archive important for you?
    – guido
    Commented Jun 29, 2018 at 17:25
  • order doesnt matter, as along the correct files are compared
    – UnixPhile
    Commented Jun 29, 2018 at 17:27
  • has anyone mentioned or suggested zdiff ?
    – Marcel
    Commented Feb 7 at 10:37

5 Answers 5

31

You will have to unzip them (if only in memory) to compare the two. A cool way I have seen to do this with diff is:

diff -y <(unzip -l file1.zip) <(unzip -l file2.zip)

That will show you if there are any files contained in one and not the other

7
  • 5
    I was thinking about the same, but adding -qql instead of -l to suppress some noise , and sorting by filename at the end | sort -k4
    – guido
    Commented Jun 29, 2018 at 17:29
  • 1
    excellent suggestions @guido!
    – Jaken551
    Commented Jun 29, 2018 at 17:31
  • 2
    this compares the number of files. But what about the content inside the files?
    – UnixPhile
    Commented Jun 29, 2018 at 17:42
  • I would unzip the zip files and create two arrays with the files. Then I would use something like cmp to compare nth element of arr1 to nth element of arr2
    – Jaken551
    Commented Jun 29, 2018 at 17:47
  • 2
    @UnixPhile diff -y --suppress-common-lines -W 333 <(unzip -lqq file1.zip | sort -k 4) <(unzip -lqq /file2.zip | sort -k 4) will suppress same entries and show missed/extra files as well as files different in size or timestamp. -W is about width, should be reasonably big for looong/path/to/files. Comparing by content will take more effort, let me know if that's really required.
    – Tagwint
    Commented Jun 29, 2018 at 18:14
9

I posted the longer explanation at "diff files inside of zip without extracting it" but if you want to compare the contents of the files within the zipfile and ignore all the metadata (timestamps in particular) then you should run:

diff \
  <(unzip -vqq file1.zip | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3) \
  <(unzip -vqq file2.zip | awk '{$2=""; $3=""; $4=""; $5=""; $6=""; print}' | sort -k3)
1
  • Much better than the currently most-upvoted answer, thanks ! How would you show the file content diff ?
    – Lenormju
    Commented Sep 6 at 15:57
1

One option to compare ZIP files and directories is to use zipcmp as mentioned in another post.

zipcmp works as longs as the ZIP archives uses the same directory structure.

If you need to compare 2 ZIP archives with the same files, but in one archive the files are contained in an additional subdirectory, zipcmp flags all files as modified, which can be a problem. So zipcmp can only used to verify if the contents are exactly the same.

I created folderdiff because there are some use cases where you want to compare a backup of you web application with a trusted source.

For example, if you want to find modified files (e.g. possible backdoors) you can use folderdiff for this task and can be used with ZIP archives and folders.

Example:

Wordpress uses a ZIP archive, where the files in the archive are stored in a folder called wordpress. In this example the files are extracted to /var/www/ but without the wordpress subfolder.

folderdiff can ignore the wordpress/ folder from the installation archive, with the --prefix argument and lists only webshell.php and index.php as different files.

$ folderdiff wordpress-6.0.3-de_AT.zip backup.zip --prefix wordpress/
===================== Added ======================
+ webshell.php
==================== Modified ====================
* index.php

zipcmp is not able to compare the files, because of the different path used in the archive.

0

You could try the following:

  1. Create a repository with some version control system (e.g. Git)
  2. Unzip the first zip file
  3. Commit the current contents of the repository
  4. Empty the contents of the repository (except its metadata, e.g. the .git folder)
  5. Unzip the second zip file
  6. Run a diff (e.g. git diff)
0
-2

Checksums is the proper way.

diff <(md5sum file1.zip | cut -f1 -d ' ') <(md5sum file2.zip | cut -f1 -d ' ')
3
  • 1
    This doesn't work, as if you add the same files to two zips files, but the file timestamps are different, then the file hash won't match. Commented Jun 8, 2022 at 14:36
  • 1
    If file1.zip and file2.zip does not use the same compression ratio, and have other differing options, the result would be invalid. Comparing zip files contents needs to uncompress the files before using md5sum on the uncompressed contents.
    – Biapy
    Commented Oct 26, 2022 at 15:13
  • You might as well skip the MD5 hashsum computation and just run cmp directly on those files. Commented Feb 6 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .