Hi

Attached find a small and maybe usefull task called ZipParanoia.

ZipParanoia helps controlling the contents of zip or jar archives.
ZipParanioa takes an input archive, include lists and exclude lists and
creates an output archive applying the following rules:
- every file from the include lists is in the output archive
- every file from the exclude lists is not in the output archive
in paranoia mode the following must be true:
- include and exclude are not allowed to overlap
- the union of all include and exclude lists must match the contents of the
input archive

WHY
- QA wants to know exactly what an archive consists of, hence paranioa mode
- an input archive may result in several output archives with various
contents
- input archives are created by merging many different archives together. We
found it easier to create one huge archive and than remove in a controlled
manner the uneeded stuff.
- No current ant task provided the paranoia level we needed.


ZipParanoia serves us well. Let me know, if you think this should be
included as an optional task or not. Improvements or better ways to solve
this problem are welcomed as well.

- tom


--
* Thomas Haas             <mailto:[EMAIL PROTECTED]>
* SoftWired AG                   <http://ww.softwired-inc.com/>
* Technoparkstr. 1  ***  CH-8005 Zurich  ***  +41-1-4452370



--- ZipParanoia.java.orig       Sun Jul 02 14:30:24 2000
+++ ZipParanoia.java    Sun Jul 02 14:30:24 2000
@@ -0,0 +1,283 @@
+/*
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 1999 The Apache Software Foundation.  All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ *    any, must include the following acknowlegement:
+ *       "This product includes software developed by the
+ *        Apache Software Foundation (http://www.apache.org/)."
+ *    Alternately, this acknowlegement may appear in the software itself,
+ *    if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
+ *    Foundation" must not be used to endorse or promote products derived
+ *    from this software without prior written permission. For written
+ *    permission, please contact [EMAIL PROTECTED]
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ *    nor may "Apache" appear in their names without prior written
+ *    permission of the Apache Group.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+
+package org.apache.tools.ant.taskdefs.optional;
+
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.BuildException;
+
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.BufferedReader;
+import java.io.Reader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Hashtable;
+import java.util.Enumeration;
+import java.util.Vector;
+import java.util.zip.*;
+import java.util.StringTokenizer;
+
+
+/**
+ * ZipParanoia checks and trims zip or jar archives.
+ *
+ * ZipParanoia needs a list of files to remain in the archive
+ * and a list of files to be removed. No wildcards are allowed and every file
+ * in the archive must either be listed in the include or in the exclude list.
+ * The two lists must not overlap.<br>
+ * This strict behaviour has been chosen on purpose.
+ * <p>
+ * ZipParanoia helps assureing a zip or jar really contains what we think it
+ * should contain.
+ *
+ * Example:
+ * <tt>
+ * &lt;zipparanoia includeFile="include1.txt,include2.txt" 
excludeFile="exclude1.txt,exclude2.txt" zipFile="xyz.jar"/&gt;
+ * </tt>
+ *
+ * @author [EMAIL PROTECTED]
+ */
+public class ZipParanoia extends Task {
+
+    final static String EOL = System.getProperty("line.separator");
+
+    Hashtable include = new Hashtable();
+    Hashtable exclude = new Hashtable();
+    File sourceZIP = null;
+    File destZIP = null;
+    Vector includeFile = new Vector();
+    Vector excludeFile = new Vector();
+    boolean paranoia = true;
+    boolean removeSource = false;
+    final StringBuffer error = new StringBuffer();
+
+    public void setZipfile(String name) {
+        sourceZIP = project.resolveFile(name);
+    }
+
+    public void setIncludefile(String value) throws BuildException {
+        if (value.length() == 0) {
+            throw new BuildException("IncludeFile name is empty.");
+        }
+        setFile(includeFile, value);
+    }
+
+    public void setExcludefile(String value) throws BuildException {
+        if (value.length() == 0) {
+            throw new BuildException("ExcludeFile name is empty.");
+        }
+        setFile(excludeFile, value);
+    }
+
+
+    public void setZipDestination(String name) throws BuildException {
+        if (name.length() == 0) {
+            throw new BuildException("ZipDestination name is empty.");
+        }
+        destZIP = project.resolveFile(name);
+    }
+
+
+    public void setParanoia(String value) {
+        paranoia = project.toBoolean(value);
+    }
+
+
+    protected void fillList(File file, Hashtable list)
+    throws IOException {
+        BufferedReader br = new BufferedReader(new FileReader(file));
+        try {
+            String line;
+            while ((line = br.readLine()) != null) {
+                if (line.length() > 0) {
+                    list.put(line, line);
+                }
+            }
+        }
+        finally {
+            br.close();
+        }
+    }
+
+
+
+
+    public void execute() throws BuildException {
+        createIncludeExclude();
+        createZIPs();
+
+        try {
+            ZipInputStream zin = new ZipInputStream(new 
FileInputStream(sourceZIP));
+            ZipOutputStream zos = new ZipOutputStream(new 
FileOutputStream(destZIP));
+            ZipEntry entry;
+            while ((entry = zin.getNextEntry()) != null) {
+                if (!entry.isDirectory()) {
+                    //System.out.println("Processing " + entry);
+                    if (exclude != null
+                            && exclude.remove(entry.getName()) != null) {
+                        continue;
+                    }
+                    if (include != null
+                            && include.remove(entry.getName()) == null) {
+                        error.append("ARCHIVE ")
+                             .append(entry).append(EOL);
+                    }
+                }
+                zos.putNextEntry(entry);
+                int c;  // TODO: use byte[] instead of single byte
+                while ((c = zin.read()) != -1) {
+                    zos.write(c);
+                }
+                zos.closeEntry();
+            }
+            zos.close();
+            zin.close();
+
+        } catch(IOException e) {
+            throw new BuildException(e);
+        }
+
+        if (include.size() > 0 || exclude.size() > 0) {
+            Enumeration list;
+            list = include.elements();
+            if (list.hasMoreElements()) {
+                while(list.hasMoreElements()) {
+                    error.append("INCLUDE ")
+                         .append(list.nextElement()).append(EOL);
+                }
+            }
+            list = exclude.elements();
+            if (list.hasMoreElements()) {
+                while(list.hasMoreElements()) {
+                    error.append("EXCLUDE ")
+                         .append(list.nextElement()).append(EOL);
+                }
+            }
+        }
+        if (error.length() > 0) {
+            if (paranoia) {
+                throw new BuildException("ERRORS IN INCLUDE/EXCLUDE" + EOL
+                    + error.toString());
+            }
+            System.out.println("ERRORS IN INCLUDE/EXCLUDE");
+            System.out.println(error);
+        }
+        // do not delete file if exception thrown to ease debugging
+        if (removeSource) {
+            sourceZIP.delete();
+        }
+    }
+
+
+    protected void createIncludeExclude() throws BuildException {
+         try {
+            if (includeFile.size() == 0) {
+                paranoia = false;
+            }
+            for (int i=0; i < includeFile.size(); i++) {
+                fillList((File)includeFile.elementAt(i), include);
+            }
+            for (int i=0; i < excludeFile.size(); i++) {
+                fillList((File)excludeFile.elementAt(i), exclude);
+            }
+        } catch (IOException e) {
+            throw new BuildException(e);
+        }
+        Enumeration list = exclude.elements();
+        Vector doubles = new Vector();
+        while (list.hasMoreElements()) {
+            Object e = include.get(list.nextElement());
+            if (e != null) {
+                doubles.addElement(e);
+            }
+        }
+        if (doubles.size() > 0) {
+            for (int i=0; i < doubles.size(); i++) {
+                error.append("INC-EXC ")
+                     .append(doubles.elementAt(i)).append(EOL);
+            }
+        }
+
+        if (paranoia && error.length() > 0) {
+            throw new BuildException(error.toString());
+        }
+    }
+
+    protected void createZIPs() throws BuildException {
+        if (!sourceZIP.exists() || !sourceZIP.isFile()) {
+            throw new BuildException("SourceFile is invalid: " + sourceZIP);
+        }
+
+        if (destZIP == null) {
+            destZIP = sourceZIP;
+            sourceZIP = new File(sourceZIP.getParentFile(),
+                sourceZIP.getName() + ".o");
+            sourceZIP.delete();
+            if (!destZIP.renameTo(sourceZIP)) {
+                throw new BuildException("Unable to create temp file: " + 
sourceZIP);
+            }
+            removeSource = true;
+        }
+    }
+
+    protected void setFile(Vector result, String value) throws BuildException {
+        final StringTokenizer tokenizer = new StringTokenizer(value, ",", 
false);
+        while (tokenizer.hasMoreTokens()) {
+            result.addElement(project.resolveFile(tokenizer.nextToken()));
+        }
+    }
+}

Reply via email to