Cari di Apache Ant 
    Apache Ant User Manual
Daftar Isi
(Sebelumnya) XSLT/StyleBorlandEJBTasks (Berikutnya)
Apache Ant Tasks

Zip

Zip

Description

Creates a zipfile.

The basedir attribute is the reference directory from where to zip.

Note that file permissions will not be stored in the resulting zipfile.

It is possible to refine the set of files that are being zipped. This can bedone with the includes, includesfile, excludes, excludesfile and defaultexcludesattributes. With the includes or includesfile attribute you specify the files you want tohave included by using patterns. The exclude or excludesfile attribute is used to specifythe files you want to have excluded. This is also done with patterns. Andfinally with the defaultexcludes attribute, you can specify whether youwant to use default exclusions or not. See the section on directory based tasks, on how theinclusion/exclusion of files works, and how to write patterns.

This task forms an implicit FileSet andsupports most attributes of <fileset>(dir becomes basedir) as well as the nested<include>, <exclude> and<patternset> elements.

Or, you may place within it nested file sets, or references to file sets.In this case basedir is optional; the implicit file set is only usedif basedir is set. You may use any mixture of the implicit file set(with basedir set, and optional attributes like includesand optional subelements like <include>); explicit nested<fileset> elements so long as at least one fileset total is specified. The ZIP file willonly reflect the relative paths of files within each fileset. The Zip task and its derivatives know a special form of a fileset named zipfileset that has additional attributes (described below).

The Zip task also supports the merging of multiple zip files into the zip file. This is possible through either the src attribute of any nested filesets or by using the special nested fileset zipgroupfileset.

The update parameter controls what happens if the ZIPfile already exists. When set to yes, the ZIP file isupdated with the files specified. (New files are added; old files arereplaced with the new versions.) When set to no (thedefault) the ZIP file is overwritten if any of the files that would beadded to the archive are newer than the entries inside the archive.Please note that ZIP files store file modification times with agranularity of two seconds. If a file is less than two seconds newerthan the entry in the archive, Apache Ant will not consider it newer.

The whenempty parameter controls what happens when no files match.If skip (the default), the ZIP is not created and a warning is issued.If fail, the ZIP is not created and the build is halted with an error.If create, an empty ZIP file (explicitly zero entries) is created,which should be recognized as such by compliant ZIP manipulation tools.

This task will now use the platform's default character encodingfor filenames - this is consistent with the command line ZIP tools,but causes problems if you try to open them from within Java and yourfilenames contain non US-ASCII characters. Use the encoding attributeand set it to UTF8 to create zip files that can safely be read byJava. For a more complete discussion,see below

Starting with Ant 1.5.2, <zip> can store Unix permissionsinside the archive (see description of the filemode and dirmodeattributes for <zipfileset>).Unfortunately there is no portable way to store these permissions.Ant uses the algorithm used by Info-Zip'simplementation of the zip and unzip commands - these are the defaultversions of zip and unzip for many Unix and Unix-like systems.

Please note that the zip format allows multiple files of the samefully-qualified name to exist within a single archive. This has beendocumented as causing various problems for unsuspecting users. If you wishto avoid this behavior you must set the duplicate attributeto a value other than its default, "add".

Please also note that different ZIP tools handle timestampsdifferently when it comes to applying timezone offset calculations offiles. Some ZIP libraries will store the timestamps as they've beenread from the filesystem while others will modify the timestamps bothwhen reading and writing the files to make all timestamps use the sametimezone. A ZIP archive created by one library may extract files with"wrong timestamps" when extracted by another library.

Ant's ZIP classes use the same algorithm as the InfoZIP tools andzlib (timestamps get adjusted), Windows' "compressed folders" functionand WinZIP don't change the timestamps. This means that using theunzip task on files created by Windows' compressed folders functionmay create files with timestamps that are "wrong", the same is true ifyou use Windows' functions to extract an Ant generated ZIParchive.

Parameters

Attribute Description Required
destfile the zip-file to create. Exactly one of the two.
zipfile the deprecated old name of destfile.
basedir the directory from which to zip the files. No
compress Not only store data but also compress them, defaults to true. Unless you set the keepcompression attribute to false, this will apply to the entire archive, not only the files you've added while updating. No
keepcompression For entries coming from existing archives (like nested zipfilesets or while updating the archive), keep the compression as it has been originally instead of using the compress attribute. Defaults false. Since Ant 1.6 No
encoding The character encoding to use for filenames inside the zip file. For a list of possible values see http://download.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html. Defaults to the platform's default character encoding.
See also the discussion below
No
filesonly Store only file entries, defaults to false No
includes comma- or space-separated list of patterns of files that must be included. All files are included when omitted. No
includesfile the name of a file. Each line of this file is taken to be an include pattern No
excludes comma- or space-separated list of patterns of files that must be excluded. No files (except default excludes) are excluded when omitted. No
excludesfile the name of a file. Each line of this file is taken to be an exclude pattern No
defaultexcludes indicates whether default excludes should be used or not ("yes"/"no"). Default excludes are used when omitted. No
update indicates whether to update or overwrite the destination file if it already exists. Default is "false". No
whenempty behavior when no files match. Valid values are "fail", "skip", and "create". Default is "skip". No
duplicate behavior when a duplicate file is found. Valid values are "add", "preserve", and "fail". The default value is "add". No
roundup 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.
Defaults to true. Since Ant 1.6.2
No
comment Comment to store in the archive. Since Ant 1.6.3 No
level Non-default level at which file compression should be performed. Valid values range from 0 (no compression/fastest) to 9 (maximum compression/slowest). Since Ant 1.7 No
preserve0permissions when updating an archive or adding entries from a different archive Ant will assume that a Unix permissions value of 0 (nobody is allowed to do anything to the file/directory) means that the permissions haven't been stored at all rather than real permissions and will instead apply its own default values.
Set this attribute to true if you really want to preserve the original permission field.since Ant 1.8.0
No, default is false
useLanguageEncodingFlag Whether to set the language encoding flag if the encoding is UTF-8. This setting doesn't have any effect if the encoding is not UTF-8. Since Ant 1.8.0.
See also the discussion below
No, default is true
createUnicodeExtraFields Whether to create unicode extra fields to store the file names a second time inside the entry's metadata.
Possible values are "never", "always" and "not-encodable" which will only add Unicode extra fields if the file name cannot be encoded using the specified encoding. Since Ant 1.8.0.
See also the discussion below
No, default is "never"
fallbacktoUTF8 Whether to use UTF-8 and the language encoding flag instead of the specified encoding if a file name cannot be encoded using the specified encoding. Since Ant 1.8.0.
See also the discussion below
No, default is false

Encoding of File Names

Traditionally the ZIP archive format uses CodePage 437 as encoding for file name, which is not sufficient for many international character sets.

Over time different archivers have chosen different ways to work around the limitation - the java.util.zip packages simply uses UTF-8 as its encoding for example.

Ant has been offering the encoding attribute of the zip and unzip task as a way to explicitly specify the encoding to use (or expect) since Ant 1.4. It defaults to the platform's default encoding for zip and UTF-8 for jar and other jar-like tasks (war, ear, ...) as well as the unzip family of tasks.

More recent versions of the ZIP specification introduce something called the "language encoding flag" which can be used to signal that a file name has been encoded using UTF-8. Starting with Ant 1.8.0 all zip-/jar- and similar archives written by Ant will set this flag, if the encoding has been set to UTF-8. Our interoperabilty tests with existing archivers didn't show any ill effects (in fact, most archivers ignore the flag to date), but you can turn off the "language encoding flag" by setting the attribute useLanguageEncodingFlag to false on the zip-task if you should encounter problems.

The unzip (and similar tasks) -task will recognize the language encoding flag and ignore the encoding set on the task if it has been found.

The InfoZIP developers have introduced new ZIP extra fields that can be used to add an additional UTF-8 encoded file name to the entry's metadata. Most archivers ignore these extra fields. The zip family of tasks support an option createUnicodeExtraFields since Ant 1.8.0 which makes Ant write these extra fields either for all entries ("always") or only those whose name cannot be encoded using the spcified encoding (not-encodeable), it defaults to "never" since the extra fields create bigger archives.

The fallbackToUTF8 attribute of zip can be used to create archives that use the specified encoding in the majority of cases but UTF-8 and the language encoding flag for filenames that cannot be encoded using the specified encoding.

The unzip-task will recognize the unicode extra fields by default and read the file name information from them, unless you set the optional attribute scanForUnicodeExtraFields to false.

Recommendations for Interoperability

The optimal setting of flags depends on the archivers you expect as consumers/producers of the ZIP archives. Below are some test results which may be superseeded with later versions of each tool.

  • The java.util.zip package used by the jar executable or to read jars from your CLASSPATH reads and writes UTF-8 names, it doesn't set or recognize any flags or unicode extra fields.
  • Starting with Java7 java.util.zip writes UTF-8 by default and uses the language encoding flag. It is possible to specify a different encoding when reading/writing ZIPs via new constructors. The package now recognizes the language encoding flag when reading and ignores the Unicode extra fields.
  • 7Zip writes CodePage 437 by default but uses UTF-8 and the language encoding flag when writing entries that cannot be encoded as CodePage 437 (similar to the zip task with fallbacktoUTF8 set to true). It recognizes the language encoding flag when reading and ignores the unicode extra fields.
  • WinZIP writes CodePage 437 and uses unicode extra fields by default. It recognizes the unicode extra field and the language encoding flag when reading.
  • Windows' "compressed folder" feature doesn't recognize any flag or extra field and creates archives using the platforms default encoding - and expects archives to be in that encoding when reading them.
  • InfoZIP based tools can recognize and write both, it is a compile time option and depends on the platform so your mileage may vary.
  • PKWARE zip tools recognize both and prefer the language encoding flag. They create archives using CodePage 437 if possible and UTF-8 plus the language encoding flag for file names that cannot be encoded as CodePage 437.

So, what to do?

If you are creating jars, then java.util.zip is your main consumer. We recommend you set the encoding to UTF-8 and keep the language encoding flag enabled. The flag won't help or hurt java.util.zip prior to Java7 but archivers that support it will show the correct file names.

For maximum interop it is probably best to set the encoding to UTF-8, enable the language encoding flag and create unicode extra fields when writing ZIPs. Such archives should be extracted correctly by java.util.zip, 7Zip, WinZIP, PKWARE tools and most likely InfoZIP tools. They will be unusable with Windows' "compressed folders" feature and bigger than archives without the unicode extra fields, though.

If Windows' "compressed folders" is your primary consumer, then your best option is to explicitly set the encoding to the target platform. You may want to enable creation of unicode extra fields so the tools that support them will extract the file names correctly.

Parameters specified as nested elements

any resource collection

ResourceCollections are used to select groups of files to archive.

Prior to Ant 1.7 only <fileset> and<zipfileset> have been supported as nested elements.

zipgroupfileset

A <zipgroupfileset> allows for multiple zip files to be merged into the archive. Each file found in this fileset is added to the archive the same way that zipfileset src files are added.

<zipgroupfileset> is a fileset and supports all of its attributes and nested elements.

Examples

  <zip destfile="${dist}/manual.zip"   basedir="htdocs/manual"  />

zips all files in the htdocs/manual directory into a file called manual.zipin the ${dist} directory.

  <zip destfile="${dist}/manual.zip"   basedir="htdocs/manual"   update="true"  />

zips all files in the htdocs/manual directory into a file called manual.zipin the ${dist} directory. If manual.zipdoesn't exist, it is created; otherwise it is updated with thenew/changed files.

  <zip destfile="${dist}/manual.zip"   basedir="htdocs/manual"   excludes="mydocs/**, **/todo.html"  />

zips all files in the htdocs/manual directory. Files in the directory mydocs,or files with the name todo.html are excluded.

  <zip destfile="${dist}/manual.zip"   basedir="htdocs/manual"   includes="api/**/*.html"   excludes="**/todo.html"  />

zips all files in the htdocs/manual directory. Only html files under the directory apiare zipped, and files with the name todo.html are excluded.

  <zip destfile="${dist}/manual.zip"> <fileset dir="htdocs/manual"/> <fileset dir="." includes="ChangeLog.txt"/>  </zip>

zips all files in the htdocs/manual directory, and also adds the file ChangeLog.txt in thecurrent directory. ChangeLog.txt will be added to the top of the ZIP file, just as ifit had been located at htdocs/manual/ChangeLog.txt.

  <zip destfile="${dist}/manual.zip"> <zipfileset dir="htdocs/manual" prefix="docs/user-guide"/> <zipfileset dir="." includes="ChangeLog27.txt" fullpath="docs/ChangeLog.txt"/> <zipfileset src="examples.zip" includes="**/*.html" prefix="docs/examples"/>  </zip>

zips all files in the htdocs/manual directory into the docs/user-guide directoryin the archive, adds the file ChangeLog27.txt in thecurrent directory as docs/ChangeLog.txt, and includes all the html files in examples.zip under docs/examples. The archive might end up containing the files:

 docs/user-guide/html/index.html docs/ChangeLog.txt docs/examples/index.html

The code

  <zip destfile="${dist}/manual.zip"> <zipfileset dir="htdocs/manual" prefix="docs/user-guide"/> <zipgroupfileset dir="." includes="examples*.zip"/>  </zip>

zips all files in the htdocs/manual directory into the docs/user-guide directory in the archive and includes all the files in any file that maches examples*.zip, such as all files within examples1.zip or examples_for_brian.zip.The same can be achieved with

  <zip destfile="${dist}/manual.zip"> <mappedresources>  <fileset dir="htdocs/manual"/>  <globmapper from="*" to="docs/user-guide/*"/> </mappedresources> <archives>  <zips> <fileset dir="." includes="examples*.zip"/>  </zips> </archives>  </zip>
The next example
<zip dest="release.zip">  <tarfileset src="release.tar"/></zip>

re-packages a TAR archive as a ZIP archive. If Unix filepermissions have been stored as part of the TAR file, they will beretained in the resulting ZIP archive.

(Sebelumnya) XSLT/StyleBorlandEJBTasks (Berikutnya)