Home » xmlbeans-2.5.0-src » zipcompare » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package zipcompare;
   17   
   18   import java.util.zip.ZipFile;
   19   import java.util.zip.ZipEntry;
   20   import java.util;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   
   24   public class ZipCompare
   25   {
   26       public static void main(String[] args)
   27       {
   28           if (args.length != 2)
   29           {
   30               System.out.println("Usage: zipcompare [file1] [file2]");
   31               System.exit(1);
   32           }
   33   
   34           ZipFile file1;
   35           try { file1 = new ZipFile(args[0]); }
   36           catch (IOException e) { System.out.println("Could not open zip file " + args[0] + ": " + e); System.exit(1); return; }
   37   
   38           ZipFile file2;
   39           try { file2 = new ZipFile(args[1]); }
   40           catch (IOException e) { System.out.println("Could not open zip file " + args[0] + ": " + e); System.exit(1); return; }
   41   
   42           System.out.println("Comparing " + args[0] + " with " + args[1] + ":");
   43   
   44           Set set1 = new LinkedHashSet();
   45           for (Enumeration e = file1.entries(); e.hasMoreElements(); )
   46               set1.add(((ZipEntry)e.nextElement()).getName());
   47   
   48           Set set2 = new LinkedHashSet();
   49           for (Enumeration e = file2.entries(); e.hasMoreElements(); )
   50               set2.add(((ZipEntry)e.nextElement()).getName());
   51   
   52           int errcount = 0;
   53           int filecount = 0;
   54           for (Iterator i = set1.iterator(); i.hasNext(); )
   55           {
   56               String name = (String)i.next();
   57               if (!set2.contains(name))
   58               {
   59                   System.out.println(name + " not found in " + args[1]);
   60                   errcount += 1;
   61                   continue;
   62               }
   63               try
   64               {
   65                   set2.remove(name);
   66                   if (!streamsEqual(file1.getInputStream(file1.getEntry(name)), file2.getInputStream(file2.getEntry(name))))
   67                   {
   68                       System.out.println(name + " does not match");
   69                       errcount += 1;
   70                       continue;
   71                   }
   72               }
   73               catch (Exception e)
   74               {
   75                   System.out.println(name + ": IO Error " + e);
   76                   e.printStackTrace();
   77                   errcount += 1;
   78                   continue;
   79               }
   80               filecount += 1;
   81           }
   82           for (Iterator i = set2.iterator(); i.hasNext(); )
   83           {
   84               String name = (String)i.next();
   85               System.out.println(name + " not found in " + args[0]);
   86               errcount += 1;
   87           }
   88           System.out.println(filecount + " entries matched");
   89           if (errcount > 0)
   90           {
   91               System.out.println(errcount + " entries did not match");
   92               System.exit(1);
   93           }
   94           System.exit(0);
   95       }
   96   
   97       static boolean streamsEqual(InputStream stream1, InputStream stream2) throws IOException
   98       {
   99           byte[] buf1 = new byte[4096];
  100           byte[] buf2 = new byte[4096];
  101           boolean done1 = false;
  102           boolean done2 = false;
  103   
  104           try
  105           {
  106           while (!done1)
  107           {
  108               int off1 = 0;
  109               int off2 = 0;
  110   
  111               while (off1 < buf1.length)
  112               {
  113                   int count = stream1.read(buf1, off1, buf1.length - off1);
  114                   if (count < 0)
  115                   {
  116                       done1 = true;
  117                       break;
  118                   }
  119                   off1 += count;
  120               }
  121               while (off2 < buf2.length)
  122               {
  123                   int count = stream2.read(buf2, off2, buf2.length - off2);
  124                   if (count < 0)
  125                   {
  126                       done2 = true;
  127                       break;
  128                   }
  129                   off2 += count;
  130               }
  131               if (off1 != off2 || done1 != done2)
  132                   return false;
  133               for (int i = 0; i < off1; i++)
  134               {
  135                   if (buf1[i] != buf2[i])
  136                       return false;
  137               }
  138           }
  139           return true;
  140           }
  141           finally { stream1.close(); stream2.close(); }
  142       }
  143   }

Home » xmlbeans-2.5.0-src » zipcompare » [javadoc | source]