diff --git a/WHATSNEW b/WHATSNEW index 0821e41f7..6a6720983 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -58,7 +58,9 @@ Changes that could break older environments: passed in a null or empty InputStream to read from. Bugzilla Report 32200 - * will now fail on archives with an empty central directory. + * and will now fail on empty archives (or ZIP + archives with an empty central directory). + set failOnEmptyArchive to false to restore the old behavior. Bugzilla report 35000. Fixed bugs: diff --git a/docs/manual/CoreTasks/unzip.html b/docs/manual/CoreTasks/unzip.html index 5979ba29a..323d9072d 100644 --- a/docs/manual/CoreTasks/unzip.html +++ b/docs/manual/CoreTasks/unzip.html @@ -110,7 +110,12 @@ archive.

encoding. No - + + failOnEmptyArchive + whether trying to extract an empty archive is an + error. since Ant 1.8.0 + No, defaults to true +

Examples

diff --git a/src/main/org/apache/tools/ant/taskdefs/Expand.java b/src/main/org/apache/tools/ant/taskdefs/Expand.java
index ff3c79ef8..0f94bddad 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Expand.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Expand.java
@@ -66,6 +66,7 @@ public class Expand extends Task {
     private Vector patternsets = new Vector();
     private Union resources = new Union();
     private boolean resourcesSpecified = false;
+    private boolean failOnEmptyArchive = true;
 
     private static final String NATIVE_ENCODING = "native-encoding";
 
@@ -75,6 +76,24 @@ public class Expand extends Task {
 
     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
 
+    /**
+     * Whether try ing to expand an empty archive would be an error.
+     *
+     * @since Ant 1.8.0
+     */
+    public void setFailOnEmptyArchive(boolean b) {
+        failOnEmptyArchive = b;
+    }
+
+    /**
+     * Whether try ing to expand an empty archive would be an error.
+     *
+     * @since Ant 1.8.0
+     */
+    public boolean getFailOnEmptyArchive() {
+        return failOnEmptyArchive;
+    }
+
     /**
      * Do the work.
      *
@@ -140,7 +159,7 @@ public class Expand extends Task {
                     getLocation());
         }
         try {
-            zf = new ZipFile(srcF, encoding);
+            zf = new ZipFile(srcF, encoding, failOnEmptyArchive);
             Enumeration e = zf.getEntries();
             while (e.hasMoreElements()) {
                 ZipEntry ze = (ZipEntry) e.nextElement();
diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java
index 9f603067a..5e3499076 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Untar.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Untar.java
@@ -142,12 +142,17 @@ public class Untar extends Expand {
                                                           new BufferedInputStream(stream)));
             log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
             TarEntry te = null;
+            boolean empty = true;
             FileNameMapper mapper = getMapper();
             while ((te = tis.getNextEntry()) != null) {
+                empty = false;
                 extractFile(FileUtils.getFileUtils(), null, dir, tis,
                             te.getName(), te.getModTime(),
                             te.isDirectory(), mapper);
             }
+            if (empty && getFailOnEmptyArchive()) {
+                throw new BuildException("archive is empty");
+            }
             log("expand complete", Project.MSG_VERBOSE);
         } finally {
             FileUtils.close(tis);
diff --git a/src/main/org/apache/tools/zip/ZipFile.java b/src/main/org/apache/tools/zip/ZipFile.java
index f15f4aa7d..a74b7fe0c 100644
--- a/src/main/org/apache/tools/zip/ZipFile.java
+++ b/src/main/org/apache/tools/zip/ZipFile.java
@@ -146,10 +146,78 @@ public class ZipFile {
      * @throws IOException if an error occurs while reading the file.
      */
     public ZipFile(File f, String encoding) throws IOException {
+        this(f, encoding, false);
+    }
+
+    /**
+     * Opens the given file for reading, assuming the platform's
+     * native encoding for file names.
+     *
+     * @param f the archive.
+     * @param mustNotBeEmpty whether an empty central directory should
+     * case an error
+     *
+     * @throws IOException if an error occurs while reading the file.
+     *
+     * @since Ant 1.8.0
+     */
+    public ZipFile(File f, boolean mustNotBeEmpty) throws IOException {
+        this(f, null, mustNotBeEmpty);
+    }
+
+    /**
+     * Opens the given file for reading, assuming the platform's
+     * native encoding for file names.
+     *
+     * @param name name of the archive.
+     * @param mustNotBeEmpty whether an empty central directory should
+     * case an error
+     *
+     * @throws IOException if an error occurs while reading the file.
+     *
+     * @since Ant 1.8.0
+     */
+    public ZipFile(String name, boolean mustNotBeEmpty) throws IOException {
+        this(new File(name), null, mustNotBeEmpty);
+    }
+
+    /**
+     * Opens the given file for reading, assuming the specified
+     * encoding for file names.
+     *
+     * @param name name of the archive.
+     * @param encoding the encoding to use for file names
+     * @param mustNotBeEmpty whether an empty central directory should
+     * case an error
+     *
+     * @throws IOException if an error occurs while reading the file.
+     *
+     * @since Ant 1.8.0
+     */
+    public ZipFile(String name, String encoding,
+                   boolean mustNotBeEmpty) throws IOException {
+        this(new File(name), encoding, mustNotBeEmpty);
+    }
+
+    /**
+     * Opens the given file for reading, assuming the specified
+     * encoding for file names.
+     *
+     * @param f the archive.
+     * @param encoding the encoding to use for file names
+     * @param mustNotBeEmpty whether an empty central directory should
+     * case an error
+     *
+     * @throws IOException if an error occurs while reading the file.
+     *
+     * @since Ant 1.8.0
+     */
+    public ZipFile(File f, String encoding,
+                   boolean mustNotBeEmpty) throws IOException {
         this.encoding = encoding;
         archive = new RandomAccessFile(f, "r");
         try {
-            populateFromCentralDirectory();
+            populateFromCentralDirectory(mustNotBeEmpty);
             resolveLocalFileHeaderData();
         } catch (IOException e) {
             try {
@@ -266,7 +334,7 @@ public class ZipFile {
      * the central directory alone, but not the data that requires the
      * local file header or additional data to be read.

*/ - private void populateFromCentralDirectory() + private void populateFromCentralDirectory(boolean mustNotBeEmpty) throws IOException { positionAtCentralDirectory(); @@ -276,7 +344,7 @@ public class ZipFile { archive.readFully(signatureBytes); long sig = ZipLong.getValue(signatureBytes); final long cfhSig = ZipLong.getValue(ZipOutputStream.CFH_SIG); - if (sig != cfhSig) { + if (mustNotBeEmpty && sig != cfhSig) { throw new IOException("central directory is empty, can't expand" + " archive."); }