Compare two zip files : Zip Unzip « File « Java Tutorial






/*   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();
    }
  }
}








11.59.Zip Unzip
11.59.1.Uses Zip compression to compress any number of files given on the command line
11.59.2.Decompress a zip file using ZipFile
11.59.3.Unzip
11.59.4.gzip
11.59.5.Compress a Byte Array
11.59.6.Decompress a Byte Array
11.59.7.Compress Java objects
11.59.8.Decompress Java objects
11.59.9.Compress string(byte array) by Deflater
11.59.10.Compare two zip files
11.59.11.Put file To Zip File
11.59.12.Unpack a segment from a zip
11.59.13.Unpack an archive from a URL
11.59.14.unzip File Into Directory
11.59.15.Zip a list of file into one zip file.
11.59.16.Zip up a directory
11.59.17.Zip files