org.apache.tools.ant.taskdefs

Class Zip

Implemented Interfaces:
SelectorContainer
Known Direct Subclasses:
Jar

public class Zip
extends MatchingTask

Create a Zip file.

Since:
Ant 1.1

Nested Class Summary

static class
Zip.ArchiveState
Holds the up-to-date status and the out-of-date resources of the original archive.
static class
Zip.Duplicate
Possible behaviors when a duplicate file is added: "add", "preserve" or "fail"
static class
Zip.WhenEmpty
Possible behaviors when there are no matching files for the task: "fail", "skip", or "create".

Field Summary

protected Hashtable
addedDirs
protected String
archiveType
protected boolean
doubleFilePass
protected String
duplicate
protected String
emptyBehavior
protected Hashtable
entries
protected boolean
skipWriting
protected File
zipFile

Fields inherited from class org.apache.tools.ant.taskdefs.MatchingTask

fileset

Fields inherited from class org.apache.tools.ant.Task

description, location, target, taskName, taskType, wrapper

Fields inherited from class org.apache.tools.ant.ProjectComponent

project

Method Summary

void
addFileset(FileSet set)
Adds a set of files.
protected void
addParentDirs(File baseDir, String entry, ZipOutputStream zOut, String prefix, int dirMode)
Ensure all parent dirs of a given entry have been added.
protected void
addResources(FileSet fileset, Resource resources, ZipOutputStream zOut)
Add the given resources.
void
addZipGroupFileset(FileSet set)
Adds a group of zip files.
void
addZipfileset(ZipFileSet set)
Adds a set of files that can be read from an archive and be given a prefix/fullpath.
protected void
cleanUp()
Do any clean up necessary to allow this instance to be used again.
protected boolean
createEmptyZip(File zipFile)
Create an empty zip file
void
execute()
validate and build
void
executeMain()
protected void
finalizeZipOutputStream(ZipOutputStream zOut)
method for subclasses to override
String
getComment()
Comment of the archive
File
getDestFile()
The file to create.
String
getEncoding()
Encoding to use for filenames.
protected Zip.ArchiveState
getResourcesToAdd(FileSet filesets, File zipFile, boolean needsUpdate)
Collect the resources that are newer than the corresponding entries (or missing) in the original archive.
protected Resource[][]
grabResources(FileSet filesets)
Fetch all included and not excluded resources from the sets.
protected void
initZipOutputStream(ZipOutputStream zOut)
method for subclasses to override
protected boolean
isAddingNewFiles()
Indicates if the task is adding new files into the archive as opposed to copying back unchanged files from the backup copy
boolean
isCompress()
Whether we want to compress the files or only store them;
protected static boolean
isEmpty(Resource r)
boolean
isInUpdateMode()
Are we updating an existing archive?
void
reset()
Makes this instance reset all attributes to their default values and forget all children.
protected Resource[]
selectFileResources(Resource orig)
Drops all non-file resources from the given array.
void
setBasedir(File baseDir)
Directory from which to archive files; optional.
void
setComment(String comment)
Comment to use for archive.
void
setCompress(boolean c)
Whether we want to compress the files or only store them; optional, default=true;
void
setDestFile(File destFile)
The file to create; required.
void
setDuplicate(Zip.Duplicate df)
Sets behavior for when a duplicate file is about to be added - one of keep, skip or overwrite.
void
setEncoding(String encoding)
Encoding to use for filenames, defaults to the platform's default encoding.
void
setFile(File file)
Deprecated. Use setDestFile(File) instead
void
setFilesonly(boolean f)
If true, emulate Sun's jar utility by not adding parent directories; optional, defaults to false.
void
setKeepCompression(boolean keep)
Whether the original compression of entries coming from a ZIP archive should be kept (for example when updating an archive).
void
setRoundUp(boolean r)
Whether the file modification times will be rounded up to the next even number of seconds.
void
setUpdate(boolean c)
If true, updates an existing file, otherwise overwrite any existing one; optional defaults to false.
void
setWhenempty(Zip.WhenEmpty we)
Sets behavior of the task when no files match.
void
setZipfile(File zipFile)
Deprecated. Use setDestFile(File) instead.
protected void
zipDir(File dir, ZipOutputStream zOut, String vPath, int mode)
protected void
zipDir(File dir, ZipOutputStream zOut, String vPath, int mode, ZipExtraField extra)
Add a directory to the zip stream.
protected void
zipFile(File file, ZipOutputStream zOut, String vPath, int mode)
Method that gets called when adding from java.io.File instances.
protected void
zipFile(InputStream in, ZipOutputStream zOut, String vPath, long lastModified, File fromArchive, int mode)
Adds a new entry to the archive, takes care of duplicates as well.

Methods inherited from class org.apache.tools.ant.taskdefs.MatchingTask

XsetIgnore, XsetItems, add, addAnd, addContains, addContainsRegexp, addCustom, addDate, addDepend, addDepth, addDifferent, addFilename, addMajority, addModified, addNone, addNot, addOr, addPresent, addSelector, addSize, addType, appendSelector, createExclude, createExcludesFile, createInclude, createIncludesFile, createPatternSet, getDirectoryScanner, getImplicitFileSet, getSelectors, hasSelectors, selectorCount, selectorElements, setCaseSensitive, setDefaultexcludes, setExcludes, setExcludesfile, setFollowSymlinks, setIncludes, setIncludesfile, setProject

Methods inherited from class org.apache.tools.ant.Task

execute, getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, reconfigure, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType

Methods inherited from class org.apache.tools.ant.ProjectComponent

getProject, log, log, setProject

Field Details

addedDirs

protected Hashtable addedDirs


archiveType

protected String archiveType


doubleFilePass

protected boolean doubleFilePass


duplicate

protected String duplicate


emptyBehavior

protected String emptyBehavior


entries

protected Hashtable entries


skipWriting

protected boolean skipWriting


zipFile

protected File zipFile

Method Details

addFileset

public void addFileset(FileSet set)
Adds a set of files.


addParentDirs

protected final void addParentDirs(File baseDir,
                                   String entry,
                                   ZipOutputStream zOut,
                                   String prefix,
                                   int dirMode)
            throws IOException
Ensure all parent dirs of a given entry have been added.

Since:
Ant 1.5.2


addResources

protected final void addResources(FileSet fileset,
                                  Resource resources,
                                  ZipOutputStream zOut)
            throws IOException
Add the given resources.

Parameters:
fileset - may give additional information like fullpath or permissions.
resources - the resources to add
zOut - the stream to write to

Since:
Ant 1.5.2


addZipGroupFileset

public void addZipGroupFileset(FileSet set)
Adds a group of zip files.


addZipfileset

public void addZipfileset(ZipFileSet set)
Adds a set of files that can be read from an archive and be given a prefix/fullpath.


cleanUp

protected void cleanUp()
Do any clean up necessary to allow this instance to be used again.

When we get here, the Zip file has been closed and all we need to do is to reset some globals.

This method will only reset globals that have been changed during execute(), it will not alter the attributes or nested child elements. If you want to reset the instance so that you can later zip a completely different set of files, you must use the reset method.

See Also:
reset()


createEmptyZip

protected boolean createEmptyZip(File zipFile)
            throws BuildException
Create an empty zip file

Returns:
true for historic reasons


execute

public void execute()
            throws BuildException
validate and build
Overrides:
execute in interface Task


executeMain

public void executeMain()
            throws BuildException


finalizeZipOutputStream

protected void finalizeZipOutputStream(ZipOutputStream zOut)
            throws IOException,
                   BuildException
method for subclasses to override


getComment

public String getComment()
Comment of the archive

Returns:
Comment of the archive.

Since:
Ant 1.6.3


getDestFile

public File getDestFile()
The file to create.

Since:
Ant 1.5.2


getEncoding

public String getEncoding()
Encoding to use for filenames.

Since:
Ant 1.5.2


getResourcesToAdd

protected Zip.ArchiveState getResourcesToAdd(FileSet filesets,
                                             File zipFile,
                                             boolean needsUpdate)
            throws BuildException
Collect the resources that are newer than the corresponding entries (or missing) in the original archive.

If we are going to recreate the archive instead of updating it, all resources should be considered as new, if a single one is. Because of this, subclasses overriding this method must call super.getResourcesToAdd and indicate with the third arg if they already know that the archive is out-of-date.

Parameters:
filesets - The filesets to grab resources from
zipFile - intended archive file (may or may not exist)
needsUpdate - whether we already know that the archive is out-of-date. Subclasses overriding this method are supposed to set this value correctly in their call to super.getResourcesToAdd.

Returns:
an array of resources to add for each fileset passed in as well as a flag that indicates whether the archive is uptodate.

Throws:
BuildException - if it likes


grabResources

protected Resource[][] grabResources(FileSet filesets)
Fetch all included and not excluded resources from the sets.

Included directories will precede included files.

Since:
Ant 1.5.2


initZipOutputStream

protected void initZipOutputStream(ZipOutputStream zOut)
            throws IOException,
                   BuildException
method for subclasses to override


isAddingNewFiles

protected final boolean isAddingNewFiles()
Indicates if the task is adding new files into the archive as opposed to copying back unchanged files from the backup copy


isCompress

public boolean isCompress()
Whether we want to compress the files or only store them;

Since:
Ant 1.5.2


isEmpty

protected static final boolean isEmpty(Resource r)

Returns:
true if all individual arrays are empty

Since:
Ant 1.5.2


isInUpdateMode

public boolean isInUpdateMode()
Are we updating an existing archive?


reset

public void reset()
Makes this instance reset all attributes to their default values and forget all children.

Since:
Ant 1.5

See Also:
cleanUp()


selectFileResources

protected Resource[] selectFileResources(Resource orig)
Drops all non-file resources from the given array.

Since:
Ant 1.6


setBasedir

public void setBasedir(File baseDir)
Directory from which to archive files; optional.


setComment

public void setComment(String comment)
Comment to use for archive.

Parameters:
comment - The content of the comment.

Since:
Ant 1.6.3


setCompress

public void setCompress(boolean c)
Whether we want to compress the files or only store them; optional, default=true;


setDestFile

public void setDestFile(File destFile)
The file to create; required.

Parameters:
destFile - The new destination File

Since:
Ant 1.5


setDuplicate

public void setDuplicate(Zip.Duplicate df)
Sets behavior for when a duplicate file is about to be added - one of keep, skip or overwrite. Possible values are: keep (keep both of the files); skip (keep the first version of the file found); overwrite overwrite the file with the new file Default for zip tasks is keep


setEncoding

public void setEncoding(String encoding)


setFile

public void setFile(File file)

Deprecated. Use setDestFile(File) instead

This is the name/location of where to create the file.

Since:
Ant 1.5


setFilesonly

public void setFilesonly(boolean f)
If true, emulate Sun's jar utility by not adding parent directories; optional, defaults to false.


setKeepCompression

public void setKeepCompression(boolean keep)
Whether the original compression of entries coming from a ZIP archive should be kept (for example when updating an archive).

Since:
Ant 1.6


setRoundUp

public void setRoundUp(boolean r)
Whether the file modification times will be rounded up to the next even number of seconds.

Zip archives store file modification times with a granularity of two seconds, so the times will either be rounded up or down. If you round down, the archive will always seem out-of-date when you rerun the task, so the default is to round up. Rounding up may lead to a different type of problems like JSPs inside a web archive that seem to be slightly more recent than precompiled pages, rendering precompilation useless.

Since:
Ant 1.6.2


setUpdate

public void setUpdate(boolean c)
If true, updates an existing file, otherwise overwrite any existing one; optional defaults to false.


setWhenempty

public void setWhenempty(Zip.WhenEmpty we)
Sets behavior of the task when no files match. Possible values are: fail (throw an exception and halt the build); skip (do not create any archive, but issue a warning); create (make an archive with no entries). Default for zip tasks is skip; for jar tasks, create.


setZipfile

public void setZipfile(File zipFile)

Deprecated. Use setDestFile(File) instead.

This is the name/location of where to create the .zip file.


zipDir

protected void zipDir(File dir,
                      ZipOutputStream zOut,
                      String vPath,
                      int mode)
            throws IOException

Since:
Ant 1.5.2


zipDir

protected void zipDir(File dir,
                      ZipOutputStream zOut,
                      String vPath,
                      int mode,
                      ZipExtraField extra)
            throws IOException
Add a directory to the zip stream.

Parameters:
dir - the directort to add to the archive
zOut - the stream to write to
vPath - the name this entry shall have in the archive
mode - the Unix permissions to set.
extra - ZipExtraFields to add

Since:
Ant 1.6.3


zipFile

protected void zipFile(File file,
                       ZipOutputStream zOut,
                       String vPath,
                       int mode)
            throws IOException
Method that gets called when adding from java.io.File instances.

This implementation delegates to the six-arg version.

Parameters:
file - the file to add to the archive
zOut - the stream to write to
vPath - the name this entry shall have in the archive
mode - the Unix permissions to set.

Since:
Ant 1.5.2


zipFile

protected void zipFile(InputStream in,
                       ZipOutputStream zOut,
                       String vPath,
                       long lastModified,
                       File fromArchive,
                       int mode)
            throws IOException
Adds a new entry to the archive, takes care of duplicates as well.

Parameters:
in - the stream to read data for the entry from.
zOut - the stream to write to.
vPath - the name this entry shall have in the archive.
lastModified - last modification time for the entry.
fromArchive - the original archive we are copying this entry from, will be null if we are not copying from an archive.
mode - the Unix permissions to set.

Since:
Ant 1.5.2


Copyright B) 2000-2005 Apache Software Foundation. All Rights Reserved.