Top Description Inners Fields Constructors Methods
org.apache.tools.ant.taskdefs

public Class Zip

extends MatchingTask
Class Inheritance
Known Direct Subclasses
org.apache.tools.ant.taskdefs.Jar
Imports
java.io.BufferedInputStream, .File, .IOException, .InputStream, .OutputStream, java.nio.file.Files, java.text.ParseException, java.util.ArrayList, .Arrays, .Comparator, .HashMap, .Hashtable, .List, .Map, .Stack, .Vector, java.util.stream.Stream, java.util.zip.CRC32, org.apache.tools.ant.BuildException, .DirectoryScanner, .FileScanner, .Project, org.apache.tools.ant.types.ArchiveFileSet, .EnumeratedAttribute, .FileSet, .Resource, .ResourceCollection, .ZipFileSet, .ZipScanner, org.apache.tools.ant.types.resources.ArchiveResource, .FileProvider, .FileResource, .Union, .ZipResource, org.apache.tools.ant.types.resources.selectors.ResourceSelector, org.apache.tools.ant.util.DateUtils, .FileNameMapper, .FileUtils, .GlobPatternMapper, .IdentityMapper, .MergingMapper, .ResourceUtils, org.apache.tools.zip.UnixStat, .Zip64Mode, .ZipEntry, .ZipExtraField, .ZipFile, .ZipOutputStream, .ZipOutputStream.UnicodeExtraFieldPolicy

Create a Zip file.
Since
Ant 1.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Zip.ArchiveState

Holds the up-to-date status and the out-of-date resources of the original archive.

public static class
Zip.Duplicate

Possible behaviors when a duplicate file is added: "add", "preserve" or "fail"

public static class
Zip.UnicodeExtraField

Policy for creation of Unicode extra fields: never, always or not-encodeable.

public static class
Zip.WhenEmpty

Possible behaviors when there are no matching files for the task: "fail", "skip", or "create".

public static class
Zip.Zip64ModeAttribute

The choices for Zip64 extensions.

Field Summary

Modifier and TypeField and Description
protected Hashtable<String, String>
private final List<String>
private boolean
addingNewFiles

true when we are adding new files into the Zip file, as opposed to adding back the unchanged files

protected String
private File
private static final int
private String
comment

Comment for the archive.

private Zip.UnicodeExtraField
createUnicodeExtraFields

Whether to add unicode extra fields.

private static final ThreadLocal<ZipExtraField[]>
private boolean
private boolean
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.

private boolean
protected String
private static final long
protected String
private String
encoding

Encoding to use for filenames, defaults to the platform's default encoding.

protected Hashtable<String, String>
private boolean
fallBackToUTF8

Whether to fall back to UTF-8 if a name cannot be encoded using the specified encoding.

private static final FileUtils
private final List<ZipFileSet>
private String
private final List<FileSet>
private static final ThreadLocal<Boolean>
private boolean
keepCompression

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

private int
private static final ResourceUtils.ResourceSelectorProvider
private static final ResourceSelector
private long
private boolean
preserve0Permissions

Assume 0 Unix mode is intentional.

private final List<ResourceCollection>
private boolean
roundUp

Whether the file modification times will be rounded up to the next even number of seconds.

private static final int
private boolean
protected boolean
skipWriting

whether the methods should just perform some sort of dry-run.

private boolean
private boolean
useLanguageEncodingFlag

Whether to set the language encoding flag when creating the archive.

private Zip.Zip64ModeAttribute
zip64Mode

Whether to enable Zip64 extensions.

private static final int
ZIP_FILE_TIMESTAMP_GRANULARITY

The granularity of timestamps inside a ZIP archive.

protected File
private ZipScanner
Inherited from org.apache.tools.ant.taskdefs.MatchingTask:
fileset

Constructor Summary

AccessConstructor and Description
public
Zip()

Method Summary

Modifier and TypeMethod and Description
public void
add(final ResourceCollection
the resources to archive
a
)

Add a collection of resources to be archived.

private void
addDirectoryResource(final Resource r, String name, final String prefix, final File base, final ZipOutputStream zOut, final int defaultDirMode, final int thisDirMode)

Add a directory entry to the archive using a specified Unix-mode and the default mode for its parent directories (if necessary).

public void
addFileset(final FileSet
the fileset to add
set
)

Adds a set of files.

protected final void
addParentDirs(final File
the base directory to use (may be null)
baseDir
,
final String
the entry name to create directories from
entry
,
final ZipOutputStream
the stream to write to
zOut
,
final String
a prefix to place on the created entries
prefix
,
final int
the directory mode
dirMode
)

Ensure all parent dirs of a given entry have been added.

private void
addResource(final Resource r, final String name, final String prefix, final ZipOutputStream zOut, final int mode, final ZipFile zf, final File fromArchive)

Add a file entry.

protected final void
addResources(final FileSet
may give additional information like fullpath or permissions.
fileset
,
final Resource[]
the resources to add
resources
,
final ZipOutputStream
the stream to write to
zOut
)

Add the given resources.

protected final void
addResources(final ResourceCollection
may give additional information like fullpath or permissions.
rc
,
final Resource[]
the resources to add
resources
,
final ZipOutputStream
the stream to write to
zOut
)

Add the given resources.

public void
addZipfileset(final ZipFileSet
the zipfileset to add
set
)

Adds a set of files that can be read from an archive and be given a prefix/fullpath.

public void
addZipGroupFileset(final FileSet
the group (a fileset) to add
set
)

Adds a group of zip files.

private void
checkAttributesAndElements()

Check the attributes and elements

protected void
cleanUp()

Do any clean up necessary to allow this instance to be used again.

private void
closeZout(final ZipOutputStream zOut, final boolean success)

Close zout

protected boolean

Returns:

true for historic reasons
createEmptyZip
(final File
the zip file
zipFile
)

Create an empty zip file

public void
execute()

Overrides org.apache.tools.ant.Task.execute.

validate and build
public void
executeMain()

Build the zip file.

protected void
finalizeZipOutputStream(final ZipOutputStream
the zip output stream
zOut
)

method for subclasses to override

public String

Returns:

Comment of the archive.
getComment
()

Comment of the archive

public Zip.UnicodeExtraField

Returns:

boolean
getCreateUnicodeExtraFields
()

Whether Unicode extra fields will be created.

protected final ZipExtraField[]

Returns:

ZipExtraField[]
getCurrentExtraFields
()

Provides the extra fields for the zip entry currently being added to the archive - if any.

public File

Returns:

the destination file
getDestFile
()

The file to create.

public String

Returns:

the name of the encoding to use
getEncoding
()

Encoding to use for filenames.

public boolean

Returns:

boolean
getFallBackToUTF8
()

Whether to fall back to UTF-8 if a name cannot be encoded using the specified encoding.

public int

Returns:

compression level.
getLevel
()

Get the compression level.

public String

Returns:

String
getModificationtime
()

The file modification time previously provided to setModificationtime(String) or null if unset.

protected Zip.ArchiveState

Returns:

an array of resources to add for each fileset passed in as well as a flag that indicates whether the archive is uptodate.
getNonFileSetResourcesToAdd
(final ResourceCollection[]
The filesets to grab resources from
rcs
,
final File
intended archive file (may or may not exist)
zipFile
,
boolean
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.
needsUpdate
)

Collect the resources that are newer than the corresponding entries (or missing) in the original archive.

public boolean

Returns:

boolean
getPreserve0Permissions
()

Assume 0 Unix mode is intentional.

protected Zip.ArchiveState

Returns:

an array of resources to add for each fileset passed in as well as a flag that indicates whether the archive is uptodate.
getResourcesToAdd
(final ResourceCollection[]
The resource collections to grab resources from
rcs
,
final File
intended archive file (may or may not exist)
zipFile
,
final boolean
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.
needsUpdate
)

Collect the resources that are newer than the corresponding entries (or missing) in the original archive.

protected Zip.ArchiveState

Returns:

an array of resources to add for each fileset passed in as well as a flag that indicates whether the archive is uptodate.
getResourcesToAdd
(final FileSet[]
The filesets to grab resources from
filesets
,
final File
intended archive file (may or may not exist)
zipFile
,
boolean
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.
needsUpdate
)

Collect the resources that are newer than the corresponding entries (or missing) in the original archive.

private int
getUnixMode(final Resource r, final ZipFile zf, final int defaultMode)

Determine a Resource's Unix mode or return the given default value if not available.

public boolean

Returns:

boolean
getUseLanguageEnodingFlag
()

Whether the language encoding flag will be used.

public Zip.Zip64ModeAttribute

Returns:

boolean
getZip64Mode
()

Whether Zip64 extensions will be used.

private synchronized ZipScanner
protected Resource[][]

Returns:

the resources included
grabNonFileSetResources
(final ResourceCollection[]
an array of resource collections
rcs
)

Fetch all included and not excluded resources from the collections.

protected Resource[][]

Returns:

the resources included
grabResources
(final FileSet[]
an array of filesets
filesets
)

Fetch all included and not excluded resources from the sets.

protected boolean

Returns:

true if executeMain has written to the zip file.
hasUpdatedFile
()

Get the value of the updatedFile attribute.

protected void
initZipOutputStream(final ZipOutputStream
the zip output stream
zOut
)

method for subclasses to override

protected final boolean

Returns:

true if adding new files
isAddingNewFiles
()

Indicates if the task is adding new files into the archive as opposed to copying back unchanged files from the backup copy

public boolean

Returns:

true if the files are to be compressed
isCompress
()

Whether we want to compress the files or only store them;

protected static final boolean

Returns:

true if all individual arrays are empty
isEmpty
(final Resource[][]
the arrays to check
r
)

Check is the resource arrays are empty.

protected final boolean

Returns:

true if either doubleFilePass is false or skipWriting is true.
isFirstPass
()

Whether this is the first time the archive building methods are invoked.

public boolean

Returns:

true if updating an existing archive
isInUpdateMode
()

Are we updating an existing archive?

protected void
logWhenWriting(final String
String
msg
,
final int
int
level
)

Logs a message at the given output level, but only if this is the pass that will actually create the archive.

private void
processDoUpdate()

Process doupdate

private void
processGroupFilesets()

Process groupfilesets

private File
renameFile()

rename the zip file.

public void
reset()

Makes this instance reset all attributes to their default values and forget all children.

protected Resource[]

Returns:

the filters resources
selectDirectoryResources
(final Resource[]
the resources to filter
orig
)

Drops all non-directory resources from the given array.

protected Resource[]

Returns:

the filters resources
selectFileResources
(final Resource[]
the resources to filter
orig
)

Drops all non-file resources from the given array.

private Resource[]
selectOutOfDateResources(final Resource[] initial, final FileNameMapper mapper)

protected Resource[]

Returns:

the filters resources
selectResources
(final Resource[]
the resources to filter
orig
,
final ResourceSelector
ResourceSelector
selector
)

Drops all resources from the given array that are not selected

public void
setBasedir(final File
the base directory
baseDir
)

Directory from which to archive files; optional.

public void
setComment(final String
The content of the comment.
comment
)

Comment to use for archive.

public void
setCompress(final boolean
if true, compress the files
c
)

Whether we want to compress the files or only store them; optional, default=true;

public void
setCreateUnicodeExtraFields(final Zip.UnicodeExtraField
boolean
b
)

Whether Unicode extra fields will be created.

protected final void
setCurrentExtraFields(final ZipExtraField[]
ZipExtraField[]
extra
)

Sets the extra fields for the zip entry currently being added to the archive - if any.

public void
setDestFile(final File
The new destination File
destFile
)

The file to create; required.

public void
setDuplicate(final Zip.Duplicate
a Duplicate enumerated value
df
)

Sets behavior for when a duplicate file is about to be added - one of add, preserve or fail.

public void
setEncoding(final String
the encoding name
encoding
)

Encoding to use for filenames, defaults to the platform's default encoding.

public void
setFallBackToUTF8(final boolean
boolean
b
)

Whether to fall back to UTF-8 if a name cannot be encoded using the specified encoding.

public void
setFile(final File
the path of the zipFile
file
)

Deprecated since 1.5.x. Use setDestFile(File) instead.
This is the name/location of where to create the file.
public void
setFilesonly(final boolean
if true, emulate sun's jar by not adding parent directories
f
)

If true, emulate Sun's jar utility by not adding parent directories; optional, defaults to false.

public void
setKeepCompression(final boolean
if true, keep the original compression
keep
)

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

public void
setLevel(final int
compression level.
level
)

Set the compression level to use.

public void
setModificationtime(String
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.
time
)

Set all stored file modification times to time.

public void
setPreserve0Permissions(final boolean
boolean
b
)

Assume 0 Unix mode is intentional.

public void
setRoundUp(final boolean
a boolean value
r
)

Whether the file modification times will be rounded up to the next even number of seconds.

public void
setUpdate(final boolean
if true, updates an existing zip file
c
)

If true, updates an existing file, otherwise overwrite any existing one; optional defaults to false.

public void
setUseLanguageEncodingFlag(final boolean
boolean
b
)

Whether to set the language encoding flag.

public void
setWhenempty(final Zip.WhenEmpty
a WhenEmpty enumerated value
we
)

Sets behavior of the task when no files match.

public void
setZip64Mode(final Zip.Zip64ModeAttribute
boolean
b
)

Whether Zip64 extensions should be used.

public void
setZipfile(final File
the path of the zipFile
zipFile
)

Deprecated since 1.5.x. Use setDestFile(File) instead.
This is the name/location of where to create the .zip file.
protected void
zipDir(final File
the directory to add to the archive
dir
,
final ZipOutputStream
the stream to write to
zOut
,
final String
the name this entry shall have in the archive
vPath
,
final int
the Unix permissions to set.
mode
)

Add a directory to the zip stream.

protected void
zipDir(final File
the directory to add to the archive
dir
,
final ZipOutputStream
the stream to write to
zOut
,
final String
the name this entry shall have in the archive
vPath
,
final int
the Unix permissions to set.
mode
,
final ZipExtraField[]
ZipExtraFields to add
extra
)

Add a directory to the zip stream.

protected void
zipDir(final Resource
the directory to add to the archive
dir
,
final ZipOutputStream
the stream to write to
zOut
,
final String
the name this entry shall have in the archive
vPath
,
final int
the Unix permissions to set.
mode
,
final ZipExtraField[]
ZipExtraFields to add
extra
)

Add a directory to the zip stream.

protected void
zipFile(final InputStream
the stream to read data for the entry from. The caller of the method is responsible for closing the stream.
in
,
final ZipOutputStream
the stream to write to.
zOut
,
final String
the name this entry shall have in the archive.
vPath
,
final long
last modification time for the entry.
lastModified
,
final File
the original archive we are copying this entry from, will be null if we are not copying from an archive.
fromArchive
,
final int
the Unix permissions to set.
mode
)

Adds a new entry to the archive, takes care of duplicates as well.

protected final void
zipFile(final InputStream
the stream to read data for the entry from. The caller of the method is responsible for closing the stream.
in
,
final ZipOutputStream
the stream to write to.
zOut
,
final String
the name this entry shall have in the archive.
vPath
,
final long
last modification time for the entry.
lastModified
,
final File
the original archive we are copying this entry from, will be null if we are not copying from an archive.
fromArchive
,
final int
the Unix permissions to set.
mode
,
final ZipExtraField[]
ZipExtraFields to add
extra
)

Adds a new entry to the archive, takes care of duplicates as well.

protected void
zipFile(final File
the file to add to the archive
file
,
final ZipOutputStream
the stream to write to
zOut
,
final String
the name this entry shall have in the archive
vPath
,
final int
the Unix permissions to set.
mode
)

Method that gets called when adding from java.io.File instances.

Inherited from org.apache.tools.ant.taskdefs.MatchingTask:
addaddAndaddContainsaddContainsRegexpaddCustomaddDateaddDependaddDepthaddDifferentaddFilenameaddMajorityaddModifiedaddNoneaddNotaddOraddPresentaddSelectoraddSizeaddTypeappendSelectorcreateExcludecreateExcludesFilecreateIncludecreateIncludesFilecreatePatternSetgetDirectoryScannergetImplicitFileSetgetSelectorshasSelectorsselectorCountselectorElementssetCaseSensitivesetDefaultexcludessetExcludessetExcludesfilesetFollowSymlinkssetIncludessetIncludesfilesetProjectXsetIgnoreXsetItems