From bca3e675c3bb2dedb5b3f0bae20a816f12187e32 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 9 Dec 2000 00:33:50 +0000 Subject: [PATCH] Fixed a number of bugs with new Zip modifications. Submitted By: "Rosen, Alex" git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268334 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Zip.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index c0818269c..67ead21ec 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -150,9 +150,11 @@ public class Zip extends MatchingTask { } public void execute() throws BuildException { - if (baseDir == null && filesets.size() == 0 && "zip".equals(archiveType)) + if (baseDir == null && filesets.size() == 0 && + locFileSets.size() == 0 && "zip".equals(archiveType)) { throw new BuildException( "basedir attribute must be set, or at least " + - "one fileset must be given!" ); + "one fileset or prefixedfileset must be given!" ); + } if (zipFile == null) { throw new BuildException("You must specify the " + archiveType + " file to create!"); @@ -178,6 +180,7 @@ public class Zip extends MatchingTask { log("Building "+ archiveType +": "+ zipFile.getAbsolutePath()); try { + boolean success = false; ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile)); try { if (doCompress) { @@ -191,9 +194,24 @@ public class Zip extends MatchingTask { for (int j = 0; j < dssSize; j++) { addFiles(scanners[j], zOut, ""); + + success = true; } } finally { - zOut.close (); + // Close the output stream. + try { + if (zOut != null) + zOut.close (); + } catch(IOException ex) { + // If we're in this finally clause because of an exception, we don't + // really care if there's an exception when closing the stream. E.g. if it + // throws "ZIP file must have at least one entry", because an exception happened + // before we added any files, then we must swallow this exception. Otherwise, + // the error that's reported will be the close() error, which is not the real + // cause of the problem. + if (success) + throw ex; + } } } catch (IOException ioe) { String msg = "Problem creating " + archiveType + ": " + ioe.getMessage(); @@ -473,7 +491,7 @@ public class Zip extends MatchingTask { } /** - * Iterate over the given Vector of relocatablefilesets and add + * Iterate over the given Vector of prefixedfilesets and add * all files to the ZipOutputStream using the given prefix. */ protected void addPrefixedFiles(Vector v, ZipOutputStream zOut) @@ -487,7 +505,10 @@ public class Zip extends MatchingTask { && !prefix.endsWith("\\")) { prefix += "/"; } - zipDir(null, zOut, prefix); + if (prefix.length() > 0) { + addParentDirs(null, prefix, zOut, ""); + zipDir(null, zOut, prefix); + } addFiles(ds, zOut, prefix); } }