public class Zip extends MatchingTask
Modifier and Type | Class and Description |
---|---|
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.UnicodeExtraField
Policy for creation of Unicode extra fields: never, always or
not-encodeable.
|
static class |
Zip.WhenEmpty
Possible behaviors when there are no matching files for the task:
"fail", "skip", or "create".
|
static class |
Zip.Zip64ModeAttribute
The choices for Zip64 extensions.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Hashtable<java.lang.String,java.lang.String> |
addedDirs |
protected java.lang.String |
archiveType |
protected boolean |
doubleFilePass
If this flag is true, execute() will run most operations twice,
the first time with
skipWriting set to
true and the second time with setting it to false. |
protected java.lang.String |
duplicate |
protected java.lang.String |
emptyBehavior |
protected java.util.Hashtable<java.lang.String,java.lang.String> |
entries |
protected boolean |
skipWriting
whether the methods should just perform some sort of dry-run.
|
protected java.io.File |
zipFile |
fileset
description, location, project
Constructor and Description |
---|
Zip() |
Modifier and Type | Method and Description |
---|---|
void |
add(ResourceCollection a)
Add a collection of resources to be archived.
|
void |
addFileset(FileSet set)
Adds a set of files.
|
protected void |
addParentDirs(java.io.File baseDir,
java.lang.String entry,
ZipOutputStream zOut,
java.lang.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.
|
protected void |
addResources(ResourceCollection rc,
Resource[] resources,
ZipOutputStream zOut)
Add the given resources.
|
void |
addZipfileset(ZipFileSet set)
Adds a set of files that can be
read from an archive and be given a prefix/fullpath.
|
void |
addZipGroupFileset(FileSet set)
Adds a group of zip files.
|
protected void |
cleanUp()
Do any clean up necessary to allow this instance to be used again.
|
protected boolean |
createEmptyZip(java.io.File zipFile)
Create an empty zip file
|
void |
execute()
validate and build
|
void |
executeMain()
Build the zip file.
|
protected void |
finalizeZipOutputStream(ZipOutputStream zOut)
method for subclasses to override
|
java.lang.String |
getComment()
Comment of the archive
|
Zip.UnicodeExtraField |
getCreateUnicodeExtraFields()
Whether Unicode extra fields will be created.
|
protected ZipExtraField[] |
getCurrentExtraFields()
Provides the extra fields for the zip entry currently being
added to the archive - if any.
|
java.io.File |
getDestFile()
The file to create.
|
java.lang.String |
getEncoding()
Encoding to use for filenames.
|
boolean |
getFallBackToUTF8()
Whether to fall back to UTF-8 if a name cannot be encoded using
the specified encoding.
|
int |
getLevel()
Get the compression level.
|
java.lang.String |
getModificationtime()
The file modification time previously provided to
setModificationtime(String) or null if unset. |
protected Zip.ArchiveState |
getNonFileSetResourcesToAdd(ResourceCollection[] rcs,
java.io.File zipFile,
boolean needsUpdate)
Collect the resources that are newer than the corresponding
entries (or missing) in the original archive.
|
boolean |
getPreserve0Permissions()
Assume 0 Unix mode is intentional.
|
protected Zip.ArchiveState |
getResourcesToAdd(FileSet[] filesets,
java.io.File zipFile,
boolean needsUpdate)
Collect the resources that are newer than the corresponding
entries (or missing) in the original archive.
|
protected Zip.ArchiveState |
getResourcesToAdd(ResourceCollection[] rcs,
java.io.File zipFile,
boolean needsUpdate)
Collect the resources that are newer than the corresponding
entries (or missing) in the original archive.
|
boolean |
getUseLanguageEnodingFlag()
Whether the language encoding flag will be used.
|
Zip.Zip64ModeAttribute |
getZip64Mode()
Whether Zip64 extensions will be used.
|
protected Resource[][] |
grabNonFileSetResources(ResourceCollection[] rcs)
Fetch all included and not excluded resources from the collections.
|
protected Resource[][] |
grabResources(FileSet[] filesets)
Fetch all included and not excluded resources from the sets.
|
protected boolean |
hasUpdatedFile()
Get the value of the updatedFile attribute.
|
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)
Check is the resource arrays are empty.
|
protected boolean |
isFirstPass()
Whether this is the first time the archive building methods are invoked.
|
boolean |
isInUpdateMode()
Are we updating an existing archive?
|
protected void |
logWhenWriting(java.lang.String msg,
int level)
Logs a message at the given output level, but only if this is
the pass that will actually create the archive.
|
void |
reset()
Makes this instance reset all attributes to their default
values and forget all children.
|
protected Resource[] |
selectDirectoryResources(Resource[] orig)
Drops all non-directory resources from the given array.
|
protected Resource[] |
selectFileResources(Resource[] orig)
Drops all non-file resources from the given array.
|
protected Resource[] |
selectResources(Resource[] orig,
ResourceSelector selector)
Drops all resources from the given array that are not selected
|
void |
setBasedir(java.io.File baseDir)
Directory from which to archive files; optional.
|
void |
setComment(java.lang.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 |
setCreateUnicodeExtraFields(Zip.UnicodeExtraField b)
Whether Unicode extra fields will be created.
|
protected void |
setCurrentExtraFields(ZipExtraField[] extra)
Sets the extra fields for the zip entry currently being
added to the archive - if any.
|
void |
setDestFile(java.io.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
add , preserve or fail . |
void |
setEncoding(java.lang.String encoding)
Encoding to use for filenames, defaults to the platform's
default encoding.
|
void |
setFallBackToUTF8(boolean b)
Whether to fall back to UTF-8 if a name cannot be encoded using
the specified encoding.
|
void |
setFile(java.io.File file)
Deprecated.
since 1.5.x.
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 |
setLevel(int level)
Set the compression level to use.
|
void |
setModificationtime(java.lang.String time)
Set all stored file modification times to
time . |
void |
setPreserve0Permissions(boolean b)
Assume 0 Unix mode is intentional.
|
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 |
setUseLanguageEncodingFlag(boolean b)
Whether to set the language encoding flag.
|
void |
setWhenempty(Zip.WhenEmpty we)
Sets behavior of the task when no files match.
|
void |
setZip64Mode(Zip.Zip64ModeAttribute b)
Whether Zip64 extensions should be used.
|
void |
setZipfile(java.io.File zipFile)
Deprecated.
since 1.5.x.
Use setDestFile(File) instead.
|
protected void |
zipDir(java.io.File dir,
ZipOutputStream zOut,
java.lang.String vPath,
int mode)
Add a directory to the zip stream.
|
protected void |
zipDir(java.io.File dir,
ZipOutputStream zOut,
java.lang.String vPath,
int mode,
ZipExtraField[] extra)
Add a directory to the zip stream.
|
protected void |
zipDir(Resource dir,
ZipOutputStream zOut,
java.lang.String vPath,
int mode,
ZipExtraField[] extra)
Add a directory to the zip stream.
|
protected void |
zipFile(java.io.File file,
ZipOutputStream zOut,
java.lang.String vPath,
int mode)
Method that gets called when adding from
java.io.File instances. |
protected void |
zipFile(java.io.InputStream in,
ZipOutputStream zOut,
java.lang.String vPath,
long lastModified,
java.io.File fromArchive,
int mode)
Adds a new entry to the archive, takes care of duplicates as well.
|
protected void |
zipFile(java.io.InputStream in,
ZipOutputStream zOut,
java.lang.String vPath,
long lastModified,
java.io.File fromArchive,
int mode,
ZipExtraField[] extra)
Adds a new entry to the archive, takes care of duplicates as well.
|
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, XsetIgnore, XsetItems
bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
clone, getDescription, getLocation, getProject, setDescription, setLocation
protected java.io.File zipFile
protected java.util.Hashtable<java.lang.String,java.lang.String> entries
protected java.lang.String duplicate
protected java.lang.String archiveType
protected java.lang.String emptyBehavior
protected java.util.Hashtable<java.lang.String,java.lang.String> addedDirs
protected boolean doubleFilePass
skipWriting
set to
true and the second time with setting it to false.
The only situation in Ant's current code base where this is ever going to be true is if the jar task has been configured with a filesetmanifest other than "skip".
protected boolean skipWriting
Will only ever be true in the first pass if the task
performs two passes because doubleFilePass
is true.
protected final boolean isFirstPass()
doubleFilePass
is false or skipWriting
is true.@Deprecated public void setZipfile(java.io.File zipFile)
zipFile
- the path of the zipFile@Deprecated public void setFile(java.io.File file)
file
- the path of the zipFilepublic void setDestFile(java.io.File destFile)
destFile
- The new destination Filepublic java.io.File getDestFile()
public void setBasedir(java.io.File baseDir)
baseDir
- the base directorypublic void setCompress(boolean c)
c
- if true, compress the filespublic boolean isCompress()
public void setFilesonly(boolean f)
f
- if true, emulate sun's jar by not adding parent directoriespublic void setUpdate(boolean c)
c
- if true, updates an existing zip filepublic boolean isInUpdateMode()
public void addFileset(FileSet set)
set
- the fileset to addpublic void addZipfileset(ZipFileSet set)
set
- the zipfileset to addpublic void add(ResourceCollection a)
a
- the resources to archivepublic void addZipGroupFileset(FileSet set)
set
- the group (a fileset) to addpublic void setDuplicate(Zip.Duplicate df)
add
, preserve
or fail
.
Possible values are: add
(keep both
of the files); preserve
(keep the first version
of the file found); fail
halt a problem
Default for zip tasks is add
df
- a Duplicate
enumerated valuepublic void setWhenempty(Zip.WhenEmpty we)
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
.we
- a WhenEmpty
enumerated valuepublic void setEncoding(java.lang.String encoding)
For a list of possible values see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html.
encoding
- the encoding namepublic java.lang.String getEncoding()
public void setKeepCompression(boolean keep)
keep
- if true, keep the original compressionpublic void setComment(java.lang.String comment)
comment
- The content of the comment.public java.lang.String getComment()
public void setLevel(int level)
level
- compression level.public int getLevel()
public void setRoundUp(boolean r)
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.
r
- a boolean
valuepublic void setPreserve0Permissions(boolean b)
b
- booleanpublic boolean getPreserve0Permissions()
public void setUseLanguageEncodingFlag(boolean b)
b
- booleanpublic boolean getUseLanguageEnodingFlag()
public void setCreateUnicodeExtraFields(Zip.UnicodeExtraField b)
b
- booleanpublic Zip.UnicodeExtraField getCreateUnicodeExtraFields()
public void setFallBackToUTF8(boolean b)
Defaults to false.
b
- booleanpublic boolean getFallBackToUTF8()
public void setZip64Mode(Zip.Zip64ModeAttribute b)
b
- booleanpublic Zip.Zip64ModeAttribute getZip64Mode()
public void setModificationtime(java.lang.String time)
time
.time
- Milliseconds since 1970-01-01 00:00, or
YYYY-MM-DD{T/ }HH:MM[:SS[.SSS]][ ][±ZZ[[:]ZZ]]
, or
MM/DD/YYYY HH:MM[:SS] {AM/PM}
, where {a/b} indicates
that you must choose one of a or b, and [c] indicates that you
may use or omit c. ±ZZZZ is the timezone offset, and may be
literally "Z" to mean GMT.public java.lang.String getModificationtime()
setModificationtime(String)
or null
if unset.public void execute() throws BuildException
execute
in class Task
BuildException
- on errorprotected boolean hasUpdatedFile()
public void executeMain() throws BuildException
BuildException
- on errorprotected final boolean isAddingNewFiles()
protected final void addResources(FileSet fileset, Resource[] resources, ZipOutputStream zOut) throws java.io.IOException
fileset
- may give additional information like fullpath or
permissions.resources
- the resources to addzOut
- the stream to write tojava.io.IOException
- on errorprotected final void addResources(ResourceCollection rc, Resource[] resources, ZipOutputStream zOut) throws java.io.IOException
rc
- may give additional information like fullpath or
permissions.resources
- the resources to addzOut
- the stream to write tojava.io.IOException
- on errorprotected void initZipOutputStream(ZipOutputStream zOut) throws java.io.IOException, BuildException
zOut
- the zip output streamjava.io.IOException
- on output errorBuildException
- on other errorsprotected void finalizeZipOutputStream(ZipOutputStream zOut) throws java.io.IOException, BuildException
zOut
- the zip output streamjava.io.IOException
- on output errorBuildException
- on other errorsprotected boolean createEmptyZip(java.io.File zipFile) throws BuildException
zipFile
- the zip fileBuildException
- on errorprotected Zip.ArchiveState getResourcesToAdd(ResourceCollection[] rcs, java.io.File zipFile, boolean needsUpdate) throws BuildException
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.
This method first delegates to getNonFileSetResourcesToAdd and then invokes the FileSet-arg version. All this to keep backwards compatibility for subclasses that don't know how to deal with non-FileSet ResourceCollections.
rcs
- The resource collections to grab resources fromzipFile
- 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
.BuildException
- if it likesprotected Zip.ArchiveState getResourcesToAdd(FileSet[] filesets, java.io.File zipFile, boolean needsUpdate) throws BuildException
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.
filesets
- The filesets to grab resources fromzipFile
- 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
.BuildException
- if it likesprotected Zip.ArchiveState getNonFileSetResourcesToAdd(ResourceCollection[] rcs, java.io.File zipFile, boolean needsUpdate) throws BuildException
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.
rcs
- The filesets to grab resources fromzipFile
- 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
.BuildException
- if it likesprotected Resource[][] grabResources(FileSet[] filesets)
Included directories will precede included files.
filesets
- an array of filesetsprotected Resource[][] grabNonFileSetResources(ResourceCollection[] rcs)
Included directories will precede included files.
rcs
- an array of resource collectionsprotected void zipDir(java.io.File dir, ZipOutputStream zOut, java.lang.String vPath, int mode) throws java.io.IOException
dir
- the directory to add to the archivezOut
- the stream to write tovPath
- the name this entry shall have in the archivemode
- the Unix permissions to set.java.io.IOException
- on errorprotected void zipDir(java.io.File dir, ZipOutputStream zOut, java.lang.String vPath, int mode, ZipExtraField[] extra) throws java.io.IOException
dir
- the directory to add to the archivezOut
- the stream to write tovPath
- the name this entry shall have in the archivemode
- the Unix permissions to set.extra
- ZipExtraFields to addjava.io.IOException
- on errorprotected void zipDir(Resource dir, ZipOutputStream zOut, java.lang.String vPath, int mode, ZipExtraField[] extra) throws java.io.IOException
dir
- the directory to add to the archivezOut
- the stream to write tovPath
- the name this entry shall have in the archivemode
- the Unix permissions to set.extra
- ZipExtraFields to addjava.io.IOException
- on errorprotected final ZipExtraField[] getCurrentExtraFields()
protected final void setCurrentExtraFields(ZipExtraField[] extra)
extra
- ZipExtraField[]protected void zipFile(java.io.InputStream in, ZipOutputStream zOut, java.lang.String vPath, long lastModified, java.io.File fromArchive, int mode) throws java.io.IOException
in
- the stream to read data for the entry from. The
caller of the method is responsible for closing the stream.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.java.io.IOException
- on errorprotected final void zipFile(java.io.InputStream in, ZipOutputStream zOut, java.lang.String vPath, long lastModified, java.io.File fromArchive, int mode, ZipExtraField[] extra) throws java.io.IOException
in
- the stream to read data for the entry from. The
caller of the method is responsible for closing the stream.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.extra
- ZipExtraFields to addjava.io.IOException
- on errorprotected void zipFile(java.io.File file, ZipOutputStream zOut, java.lang.String vPath, int mode) throws java.io.IOException
java.io.File
instances.
This implementation delegates to the six-arg version.
file
- the file to add to the archivezOut
- the stream to write tovPath
- the name this entry shall have in the archivemode
- the Unix permissions to set.java.io.IOException
- on errorprotected final void addParentDirs(java.io.File baseDir, java.lang.String entry, ZipOutputStream zOut, java.lang.String prefix, int dirMode) throws java.io.IOException
baseDir
- the base directory to use (may be null)entry
- the entry name to create directories fromzOut
- the stream to write toprefix
- a prefix to place on the created entriesdirMode
- the directory modejava.io.IOException
- on errorprotected void cleanUp()
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.
reset()
public void reset()
cleanUp()
protected static final boolean isEmpty(Resource[][] r)
r
- the arrays to checkprotected Resource[] selectFileResources(Resource[] orig)
orig
- the resources to filterprotected Resource[] selectDirectoryResources(Resource[] orig)
orig
- the resources to filterprotected Resource[] selectResources(Resource[] orig, ResourceSelector selector)
orig
- the resources to filterselector
- ResourceSelectorprotected void logWhenWriting(java.lang.String msg, int level)
msg
- Stringlevel
- int