When managing compressed files in Linux, it is common to compare two ZIP files to verify their contents or identify differences. Linux provides several shell tools to assist in comparing ZIP files without needing to extract them.

In this guide, you will learn how to effectively compare ZIP files using different methods in the Linux shell.

Method 1: Extract and Use diff

The easiest way to compare ZIP files is to extract them and then use the diff command.

Step 1: Extract the ZIP files.

unzip file1.zip -d file1_dir
unzip file2.zip -d file2_dir

The -d option allows you to specify the directory where files will be extracted.

Step 2: Use diff to compare the extracted directories.

diff -r file1_dir file2_dir
  • The -r flag makes diff compare directories recursively.
  • This approach provides a line-by-line comparison of the differences between files in the ZIP archives.

Method 2: Using diff with unzip -l

You can also compare ZIP files without extracting their contents. This method uses unzip -l to list the contents and then diff to compare them.

Step 1: List the contents of each ZIP file.

unzip -l file1.zip > file1_contents.txt
unzip -l file2.zip > file2_contents.txt

unzip -l lists the file names, sizes, and modification times.

Step 2: Use diff to compare the file lists.

diff file1_contents.txt file2_contents.txt

This command will show differences in file names, sizes, or timestamps between the two ZIP archives.

Method 3: Using cmp for Byte-Level Comparison

If you need a byte-level comparison between two ZIP files, you can use the cmp command. This compares the raw contents of the ZIP files directly.

cmp file1.zip file2.zip
  • If cmp finds differences, it will output the location of the first differing byte.
  • If there are no differences, cmp produces no output.

Method 4: Compare Using 7z

Another powerful tool for ZIP file comparison is 7z, part of the p7zip package.

Step 1: Install p7zip if not already installed.

apt-get install p7zip-full

Step 2: Use 7z to list contents and compare.

7z l file1.zip > file1_list.txt
7z l file2.zip > file2_list.txt
diff file1_list.txt file2_list.txt
  • The 7z l command lists the contents of the ZIP archive.
  • Then, use diff to compare the lists.

Method 5: Using vbindiff for Visual Comparison

For a visual comparison of two ZIP files, you can use vbindiff.

Step 1: Install vbindiff.

apt-get install vbindiff

Step 2: Use vbindiff to compare files.

vbindiff file1.zip file2.zip

vbindiff provides a visual, byte-by-byte comparison. This can be helpful when you need to see exactly where differences occur.

Method 6: Using bsdiff

If you need to create a binary patch to represent differences between two ZIP files, you can use bsdiff.

Step 1: Install bsdiff.

apt-get install bsdiff

Step 2: Create a binary patch.

bsdiff file1.zip file2.zip patch_file.bsdiff

This creates a patch file that can represent the changes between two ZIP files. It’s useful for version control of large binary files.

Conclusion

Comparing ZIP files in Linux is not as straightforward as comparing regular text files, but it can be easily done with the right tools. Whether you choose to extract and compare using diff, leverage a visual tool like vbindiff, or automate the process with a script, Linux provides versatile solutions for different needs. Compare ZIP files on Atlantic.Net’s dedicated server hosting with Linux shell commands!