Author: bodewig
Date: Thu Jul 17 07:23:44 2008
New Revision: 677592

URL: http://svn.apache.org/viewvc?rev=677592&view=rev
Log:
an empty central directory is fine for an empty archive

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/unzip.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java
    ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java

Modified: ant/core/trunk/WHATSNEW
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=677592&r1=677591&r2=677592&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Thu Jul 17 07:23:44 2008
@@ -58,7 +58,9 @@
    passed in a null or empty InputStream to read from.
    Bugzilla Report 32200
 
- * <unzip> will now fail on archives with an empty central directory.
+ * <unzip> and <untar> 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:

Modified: ant/core/trunk/docs/manual/CoreTasks/unzip.html
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/unzip.html?rev=677592&r1=677591&r2=677592&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/unzip.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/unzip.html Thu Jul 17 07:23:44 2008
@@ -110,7 +110,12 @@
     encoding.</td>
     <td align="center" valign="top">No</td>
   </tr>
-
+  <tr>
+    <td valign="top">failOnEmptyArchive</td>
+    <td valign="top">whether trying to extract an empty archive is an
+      error. <em>since Ant 1.8.0</em></td>
+    <td valign="top" align="center">No, defaults to true</td>
+  </tr>
 </table>
 <h3>Examples</h3>
 <pre>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java?rev=677592&r1=677591&r2=677592&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java Thu Jul 
17 07:23:44 2008
@@ -66,6 +66,7 @@
     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";
 
@@ -76,6 +77,24 @@
     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.
      *
      * @exception BuildException Thrown in unrecoverable error.
@@ -140,7 +159,7 @@
                     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();

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java?rev=677592&r1=677591&r2=677592&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java Thu Jul 17 
07:23:44 2008
@@ -142,12 +142,17 @@
                                                           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);

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java?rev=677592&r1=677591&r2=677592&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java Thu Jul 17 
07:23:44 2008
@@ -146,10 +146,78 @@
      * @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 @@
      * the central directory alone, but not the data that requires the
      * local file header or additional data to be read.</p>
      */
-    private void populateFromCentralDirectory()
+    private void populateFromCentralDirectory(boolean mustNotBeEmpty)
         throws IOException {
         positionAtCentralDirectory();
 
@@ -276,7 +344,7 @@
         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.");
         }


Reply via email to