001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import org.apache.commons.io.IOUtils;
007:
008: import java.io.File;
009: import java.io.IOException;
010: import java.util.Arrays;
011: import java.util.Collections;
012: import java.util.Enumeration;
013: import java.util.Iterator;
014: import java.util.Set;
015: import java.util.TreeSet;
016: import java.util.zip.ZipEntry;
017: import java.util.zip.ZipFile;
018:
019: public class ZipCompare {
020: private static final String TAB = " ";
021:
022: public static void main(String args[]) throws IOException {
023: boolean hasDiffs = false;
024:
025: if (args.length != 2) {
026: System.err.println("usage: " + ZipCompare.class.getName()
027: + " <zip file 1> <zip file 2>");
028: return;
029: }
030:
031: File zipfile1 = new File(args[0]);
032: File zipfile2 = new File(args[1]);
033:
034: check(zipfile1);
035: check(zipfile2);
036:
037: ZipFile zip1 = new ZipFile(zipfile1);
038: ZipFile zip2 = new ZipFile(zipfile2);
039:
040: Set files1 = getFiles(zip1);
041: Set files2 = getFiles(zip2);
042:
043: Set zip1Only = difference(files1, files2);
044: Set zip2Only = difference(files2, files1);
045:
046: if (zip1Only.size() != zip2Only.size()) {
047: hasDiffs = true;
048: System.out.println("Number of entries differ:");
049: System.out.println(TAB + zip1Only.size() + " " + zipfile1);
050: System.out.println(TAB + zip2Only.size() + " " + zipfile2);
051: System.out.println();
052: }
053:
054: listOnlyFiles(zipfile1, zip1Only);
055: listOnlyFiles(zipfile2, zip2Only);
056:
057: Set diffs = new TreeSet();
058: for (Iterator iter = intersect(files1, files2).iterator(); iter
059: .hasNext();) {
060: String file = (String) iter.next();
061: boolean same = diff(zip1, zip2, file);
062:
063: if (!same) {
064: diffs.add(file);
065: }
066:
067: hasDiffs |= !same;
068: }
069:
070: listDiffs(diffs);
071:
072: if (!hasDiffs) {
073: System.out.println("No differences");
074: }
075: }
076:
077: private static void listDiffs(Set diffs) {
078: if (diffs.size() > 0) {
079: System.out.println("Files that differ:");
080: for (Iterator iter = diffs.iterator(); iter.hasNext();) {
081: System.out.println(TAB + iter.next());
082: }
083: System.out.println();
084: }
085: }
086:
087: private static boolean diff(ZipFile zip1, ZipFile zip2, String file)
088: throws IOException {
089: ZipEntry entry1 = zip1.getEntry(file);
090: ZipEntry entry2 = zip2.getEntry(file);
091:
092: if (entry1 == null) {
093: throw new AssertionError("Null entry for file " + file
094: + " in zip " + zip1.getName());
095: }
096: if (entry2 == null) {
097: throw new AssertionError("Null entry for file " + file
098: + " in zip " + zip2.getName());
099: }
100:
101: if (entry1.getSize() != entry2.getSize()) {
102: return false;
103: }
104: if (entry1.getCrc() != entry2.getCrc()) {
105: return false;
106: }
107:
108: byte[] data1 = IOUtils.toByteArray(zip1.getInputStream(entry1));
109: byte[] data2 = IOUtils.toByteArray(zip2.getInputStream(entry2));
110:
111: return Arrays.equals(data1, data2);
112: }
113:
114: private static void listOnlyFiles(File zipfile, Set files) {
115: if (files.size() > 0) {
116: System.out.println("Only in " + zipfile + ":");
117: for (Iterator iter = files.iterator(); iter.hasNext();) {
118: System.out.println(TAB + iter.next());
119: }
120: System.out.println("\n");
121: }
122: }
123:
124: private static Set intersect(Set set1, Set set2) {
125: Set intersection = new TreeSet(set1);
126: intersection.retainAll(set2);
127: return Collections.unmodifiableSet(intersection);
128: }
129:
130: private static Set difference(Set set1, Set set2) {
131: Set copy = new TreeSet(set1);
132: copy.removeAll(set2);
133: return Collections.unmodifiableSet(copy);
134: }
135:
136: private static Set getFiles(ZipFile zip) {
137: Set files = new TreeSet();
138:
139: Enumeration e = zip.entries();
140: while (e.hasMoreElements()) {
141: ZipEntry entry = (ZipEntry) e.nextElement();
142: files.add(entry.getName());
143: }
144:
145: return Collections.unmodifiableSet(files);
146: }
147:
148: private static void check(File file) {
149: if (!file.exists()) {
150: exit(file + " does not exist");
151: }
152:
153: if (!file.isFile()) {
154: exit(file + " is not a file");
155: }
156:
157: if (!file.canRead()) {
158: exit(file + " is not readable");
159: }
160:
161: if (!file.getName().toLowerCase().endsWith(".zip")) {
162: System.err
163: .println("WARN --> filename does not have a .zip extension: "
164: + file);
165: }
166: }
167:
168: private static void exit(String msg) {
169: System.err.println(msg);
170: System.exit(1);
171: }
172:
173: }
|