Compare two zip files : Zip Tar File « File Input Output « Java






Compare two zip files

       

/*   Copyright 2004 The Apache Software Foundation
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

// From xml beans
public class ZipCompare {
  public static void main(String[] args) {
    if (args.length != 2) {
      System.out.println("Usage: zipcompare [file1] [file2]");
      System.exit(1);
    }

    ZipFile file1;
    try {
      file1 = new ZipFile(args[0]);
    } catch (IOException e) {
      System.out.println("Could not open zip file " + args[0] + ": " + e);
      System.exit(1);
      return;
    }

    ZipFile file2;
    try {
      file2 = new ZipFile(args[1]);
    } catch (IOException e) {
      System.out.println("Could not open zip file " + args[0] + ": " + e);
      System.exit(1);
      return;
    }

    System.out.println("Comparing " + args[0] + " with " + args[1] + ":");

    Set set1 = new LinkedHashSet();
    for (Enumeration e = file1.entries(); e.hasMoreElements();)
      set1.add(((ZipEntry) e.nextElement()).getName());

    Set set2 = new LinkedHashSet();
    for (Enumeration e = file2.entries(); e.hasMoreElements();)
      set2.add(((ZipEntry) e.nextElement()).getName());

    int errcount = 0;
    int filecount = 0;
    for (Iterator i = set1.iterator(); i.hasNext();) {
      String name = (String) i.next();
      if (!set2.contains(name)) {
        System.out.println(name + " not found in " + args[1]);
        errcount += 1;
        continue;
      }
      try {
        set2.remove(name);
        if (!streamsEqual(file1.getInputStream(file1.getEntry(name)), file2.getInputStream(file2
            .getEntry(name)))) {
          System.out.println(name + " does not match");
          errcount += 1;
          continue;
        }
      } catch (Exception e) {
        System.out.println(name + ": IO Error " + e);
        e.printStackTrace();
        errcount += 1;
        continue;
      }
      filecount += 1;
    }
    for (Iterator i = set2.iterator(); i.hasNext();) {
      String name = (String) i.next();
      System.out.println(name + " not found in " + args[0]);
      errcount += 1;
    }
    System.out.println(filecount + " entries matched");
    if (errcount > 0) {
      System.out.println(errcount + " entries did not match");
      System.exit(1);
    }
    System.exit(0);
  }

  static boolean streamsEqual(InputStream stream1, InputStream stream2) throws IOException {
    byte[] buf1 = new byte[4096];
    byte[] buf2 = new byte[4096];
    boolean done1 = false;
    boolean done2 = false;

    try {
      while (!done1) {
        int off1 = 0;
        int off2 = 0;

        while (off1 < buf1.length) {
          int count = stream1.read(buf1, off1, buf1.length - off1);
          if (count < 0) {
            done1 = true;
            break;
          }
          off1 += count;
        }
        while (off2 < buf2.length) {
          int count = stream2.read(buf2, off2, buf2.length - off2);
          if (count < 0) {
            done2 = true;
            break;
          }
          off2 += count;
        }
        if (off1 != off2 || done1 != done2)
          return false;
        for (int i = 0; i < off1; i++) {
          if (buf1[i] != buf2[i])
            return false;
        }
      }
      return true;
    } finally {
      stream1.close();
      stream2.close();
    }
  }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Extract contents of a zip file
2.List the contents of a zip file
3.Read entries in a zip / compressed file
4.Decompress a zip file using ZipInputStream
5.Decompress a zip file using ZipFile
6.Create checksum for a zip file
7.Read a zip file checksum value
8.Create a zip file with java.util.zip package
9.Extract file/files from a zip file
10.Read files within a zip file
11.Retrieve a compressed file from a ZIP file
12.Retrieve the contents of a ZIP file
13.Making a zip file of directory including its subdirectories recursively
14.Displaying contents of a compressed zip file
15.Compress a Byte Array
16.Decompress a Byte Array
17.Read zip file
18.Write Zip file
19.The java.util.zip package can be used to create a checksum.
20.Read the content of a zip file ZipFile
21.List the entries of a zip file
22.Compressing Streams: Zipper, Java example
23.Compressing Streams: Parity Checksum
24.Compressing Streams: File Summer
25.Create a simple ZIP File: not retain any directory path information about the files.
26.Decompress a ZIP file.
27.Decompressing a Byte Array
28.Zip unzip byte array
29.Creating a ZIP File
30.Listing the Contents of a ZIP File
31.Retrieving a Compressed File from a ZIP File
32.Calculating the Checksum of a Byte Array (Compute Adler-32 checksum)
33.Compute CRC-32 checksum
34.Calculating the Checksum of a File
35.Compress string(byte array) by Deflater
36.Use Java code to zip a folder
37.Uses Zip compression to compress any number of files given on the command line
38.Load zip file and scan zip file
39.Reading the Contents of a ZIP File
40.UnZip -- print or unzip a JAR or PKZIP file using java.util.zip
41.Tape Archive Lister: Tar file
42.Compressing a Byte Array
43.Tar file stream
44.Tar file and untar file
45.bzip source code
46.Unpack an archive from a URL
47.Determine whether a file is a ZIP File.
48.Zip up a directory
49.Check sum for a path
50.Check sum for an InputStream
51.Extract zip file to destination folder
52.Return the first directory of this archive. This is needed to determine the plugin directory
53.Makes a zip file named xmlFileName from xmlURL at path
54.Unzipps a zip file placed at zipURL to path
55.A single checksum calculation for multiple files
56.Put file To Zip File
57.Provides both writing and reading from a file which is transparently compressed in Zip
58.TarInputStream reads a UNIX tar archive as an InputStream
59.TarOutputStream writes a UNIX tar archive as an OutputStream
60.Package files utility
61.Zip Compare
62.Unpack a segment from a zip
63.Unpack a zip file
64.Unzip file to a directory
65.Zip a list of file into one zip file.
66.Validate that an archive contains a named entry
67.A frame with a text area to show the contents of a file inside a ZIP archive
68.Compress object and decompress
69.Util for extracting *.jar, *.war and *.zip archives.