Java Mailing List Archive

http://www.ant-tasks.com/

Apache Ant Archive

» Ant Users List
» Ant Developers List
Creating a Zip File Programmatically

Creating a Zip File Programmatically

2007-06-20       - By Dempsey K Bullock

I'm fairly new to extending Ant using custom tasks.  I need to create a
Zip Task programmatically in order to package a set of directories.  So
far, I've come up with the following.  Basically, I'm creating a Zip Task.
Then I'm adding ZipFileSets to the Zip Task.  Everything blows up then I
try to execute.

Can anyone see any mistakes or point me in the right direction?  Thanks in
advance!



Zip zip = new Zip();
zip.setDestFile(new File("C:/archive_temp/" + moduleFolderName
               + "App.zip"));

Iterator zipIterator = dependencySet.iterator();

DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(new File("C:/copy_temp"));

while (zipIterator.hasNext()) {
       String modulePath = workspacePath + "\\" + zipIterator.next();
       log("Adding Dependency Folder: " + modulePath);

       ZipFileSet zipFileSet = new ZipFileSet();
       zipFileSet.setDir(new File(modulePath));
       zipFileSet.setDirMode("true");
       zipFileSet.setupDirectoryScanner(ds, this.getProject());
     log(String.valueOf(zipFileSet.hasDirModeBeenSet()));

       zip.addZipfileset(zipFileSet);
}

try {
       zip.executeMain();
} catch (RuntimeException e) {
       e.printStackTrace();
}

The following is the resulting error stack:

java.lang.NullPointerException
at org.apache.tools.ant.types.AbstractFileSet.setupDirectoryScanner(
AbstractFileSet.java:382)
at org.apache.tools.ant.types.AbstractFileSet.getDirectoryScanner(
AbstractFileSet.java:356)
at org.apache.tools.ant.types.ZipFileSet.getDirectoryScanner(
ZipFileSet.java:192)
at org.apache.tools.ant.taskdefs.Zip.grabResources(Zip.java:949)
at org.apache.tools.ant.taskdefs.Zip.getResourcesToAdd(Zip.java:799)
at org.apache.tools.ant.taskdefs.Zip.executeMain(Zip.java:425)
at gov.dcgsa.build.util.ScaDependantUtil.execute(ScaDependantUtil.java:174
)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(
DefaultExecutor.java:40)
at
org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(
EclipseDefaultExecutor.java:32)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(
InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(
InternalAntRunner.java:137)

©2008 ant-tasks.com - Jax Systems, LLC, U.S.A.