From 54b84962a5cf180a8c3cd20236acea0a790619a8 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Tue, 16 Jan 2001 13:16:46 +0000 Subject: [PATCH] Addition of ZipFileset facilities. Descibed by the author --- With these patches, Zip (and derivative tasks such as Jar and War) can merge the entries of multiple zip files into a single output zip file. The contents of an input zip file may be selectively extracted based on include/exclude patterns. An included zip file is specified using a with a "src" attribute, as in: In this example, a subset of the "weblogic/utils" directory is extracted from weblogic.jar, into utils.jar. The fileset may also contain "prefix" and "fullpath" attributes (the functionality of PrefixedFileSet has been retained in the new class ZipFileSet). Prefixes apply to directory-based and zip-based filesets. The fullpath attributes applies only to a single file in a directory-based fileset. The War task may extract entries from a zip file for all of its filesets (including the files in "classes" and "lib"). The motivation for this change is: 1) There is significant overlap between "jlink" and "zip", and it seemed better to combine them. 2) "jlink" does not support include/exclude patterns which are extremely useful for writing packaging-type tasks such as Zip/Jar/War. This was my main motivation. 3) By adding this functionality to the base task, it can also be used in derivative tasks such as Jar and War. --- Submitted By: Don Ferguson git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268458 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Jar.java | 7 +- .../org/apache/tools/ant/taskdefs/War.java | 20 ++--- .../org/apache/tools/ant/taskdefs/Zip.java | 80 +++++++++---------- 3 files changed, 53 insertions(+), 54 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index c652e3e6d..6d1dcd4f9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -55,6 +55,7 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.*; +import org.apache.tools.ant.types.ZipFileSet; import java.io.*; import java.util.zip.*; @@ -85,12 +86,12 @@ public class Jar extends Zip { if (!manifest.exists()) throw new BuildException("Manifest file: " + manifest + " does not exist."); - // Create a PrefixedFileSet for this file, and pass it up. - PrefixedFileSet fs = new PrefixedFileSet(); + // Create a ZipFileSet for this file, and pass it up. + ZipFileSet fs = new ZipFileSet(); fs.setDir(new File(manifest.getParent())); fs.setIncludes(manifest.getName()); fs.setFullpath("META-INF/MANIFEST.MF"); - super.addPrefixedfileset(fs); + super.addFileset(fs); } diff --git a/src/main/org/apache/tools/ant/taskdefs/War.java b/src/main/org/apache/tools/ant/taskdefs/War.java index 7f8b1cc5b..98f23bcde 100644 --- a/src/main/org/apache/tools/ant/taskdefs/War.java +++ b/src/main/org/apache/tools/ant/taskdefs/War.java @@ -55,7 +55,7 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.*; -import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.ZipFileSet; import java.io.*; import java.util.Vector; @@ -86,30 +86,30 @@ public class War extends Jar { if (!deploymentDescriptor.exists()) throw new BuildException("Deployment descriptor: " + deploymentDescriptor + " does not exist."); - // Create a PrefixedFileSet for this file, and pass it up. - PrefixedFileSet fs = new PrefixedFileSet(); + // Create a ZipFileSet for this file, and pass it up. + ZipFileSet fs = new ZipFileSet(); fs.setDir(new File(deploymentDescriptor.getParent())); fs.setIncludes(deploymentDescriptor.getName()); fs.setFullpath("WEB-INF/web.xml"); - super.addPrefixedfileset(fs); + super.addFileset(fs); } - public void addLib(PrefixedFileSet fs) { + public void addLib(ZipFileSet fs) { // We just set the prefix for this fileset, and pass it up. fs.setPrefix("WEB-INF/lib/"); - super.addPrefixedfileset(fs); + super.addFileset(fs); } - public void addClasses(PrefixedFileSet fs) { + public void addClasses(ZipFileSet fs) { // We just set the prefix for this fileset, and pass it up. fs.setPrefix("WEB-INF/classes/"); - super.addPrefixedfileset(fs); + super.addFileset(fs); } - public void addWebinf(PrefixedFileSet fs) { + public void addWebinf(ZipFileSet fs) { // We just set the prefix for this fileset, and pass it up. fs.setPrefix("WEB-INF/"); - super.addPrefixedfileset(fs); + super.addFileset(fs); } protected void initZipOutputStream(ZipOutputStream zOut) diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index 6f5d03753..fc66571c4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -109,40 +109,18 @@ public class Zip extends MatchingTask { /** * Adds a set of files (nested fileset attribute). */ - public void addFileset(FileSet set) { + public void addFileset(ZipFileSet set) { filesets.addElement(set); } /** - * Adds a set of files (nested fileset attribute). + * @deprecated addPrefixedfileset is deprecated; replaced by ZipFileSet */ - public void addPrefixedfileset(PrefixedFileSet set) { - addFileset(set); + public void addPrefixedfileset(ZipFileSet set) { + log("WARNING: PrefixedFileSets are deprecated; use the fileset tag instead."); + filesets.addElement(set); } - /** - * FileSet with an additional prefix attribute to specify the - * location we want to move the files to (inside the archive). - * Or, if this FileSet represents only a single file, then the - * fullpath attribute can be set, which specifies the full path - * that the file should have when it is placed in the archive. - */ - public static class PrefixedFileSet extends FileSet { - private String prefix = ""; - private String fullpath = ""; - - public void setPrefix(String loc) { - prefix = loc; - } - - public String getPrefix() {return prefix;} - - public void setFullpath(String loc) { - fullpath = loc; - } - - public String getFullpath() {return fullpath;} - } /** * Sets behavior of the task when no files match. @@ -164,7 +142,7 @@ public class Zip extends MatchingTask { public void execute() throws BuildException { if (baseDir == null && filesets.size() == 0 && "zip".equals(archiveType)) { throw new BuildException( "basedir attribute must be set, or at least " + - "one fileset or prefixedfileset must be given!" ); + "one fileset must be given!" ); } if (zipFile == null) { @@ -191,7 +169,8 @@ public class Zip extends MatchingTask { try { boolean success = false; - ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile)); + ZipOutputStream zOut = + new ZipOutputStream(new FileOutputStream(zipFile)); try { if (doCompress) { zOut.setMethod(ZipOutputStream.DEFLATED); @@ -283,6 +262,26 @@ public class Zip extends MatchingTask { } } + protected void addZipEntries(ZipFileSet fs, DirectoryScanner ds, + ZipOutputStream zOut, String prefix) + throws IOException + { + ZipScanner zipScanner = (ZipScanner) ds; + String zipSrc = fs.getSrc(); + + ZipEntry entry; + ZipInputStream in = new ZipInputStream(new FileInputStream(new File(zipSrc))); + while ((entry = in.getNextEntry()) != null) { + String vPath = entry.getName(); + if (zipScanner.match(vPath)) { + addParentDirs(null, vPath, zOut, prefix); + if (! entry.isDirectory()) { + zipFile(in, zOut, prefix+vPath, entry.getTime()); + } + } + } + } + protected void initZipOutputStream(ZipOutputStream zOut) throws IOException, BuildException { @@ -499,28 +498,22 @@ public class Zip extends MatchingTask { } /** - * Iterate over the given Vector of (prefixed)filesets and add + * Iterate over the given Vector of zipfilesets and add * all files to the ZipOutputStream using the given prefix. */ protected void addFiles(Vector filesets, ZipOutputStream zOut) throws IOException { // Add each fileset in the Vector. for (int i = 0; i 0 && !prefix.endsWith("/") && !prefix.endsWith("\\")) { prefix += "/"; } + String fullpath = fs.getFullpath(); // Need to manually add either fullpath's parent directory, or // the prefix directory, to the archive. if (prefix.length() > 0) { @@ -529,8 +522,13 @@ public class Zip extends MatchingTask { } else if (fullpath.length() > 0) { addParentDirs(null, fullpath, zOut, ""); } - // Add the fileset. - addFiles(ds, zOut, prefix, fullpath); + + if (fs.getSrc() != null) { + addZipEntries(fs, ds, zOut, prefix); + } else { + // Add the fileset. + addFiles(ds, zOut, prefix, fullpath); + } } }