Class Zip

All Implemented Interfaces:
Cloneable, SelectorContainer
Direct Known Subclasses:
Jar

public class Zip extends MatchingTask
Create a Zip file.
Since:
Ant 1.1
  • Field Details

    • zipFile

      protected File zipFile
    • entries

      protected Hashtable<String,String> entries
    • duplicate

      protected String duplicate
    • archiveType

      protected String archiveType
    • emptyBehavior

      protected String emptyBehavior
    • addedDirs

      protected Hashtable<String,String> addedDirs
    • doubleFilePass

      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.

      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".

    • skipWriting

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

      Will only ever be true in the first pass if the task performs two passes because doubleFilePass is true.

  • Constructor Details

    • Zip

      public Zip()
  • Method Details

    • isFirstPass

      protected final boolean isFirstPass()
      Whether this is the first time the archive building methods are invoked.
      Returns:
      true if either doubleFilePass is false or skipWriting is true.
      Since:
      Ant 1.8.0
    • setZipfile

      @Deprecated public void setZipfile(File zipFile)
      Deprecated.
      since 1.5.x. Use setDestFile(File) instead.
      This is the name/location of where to create the .zip file.
      Parameters:
      zipFile - the path of the zipFile
    • setFile

      @Deprecated public void setFile(File file)
      Deprecated.
      since 1.5.x. Use setDestFile(File) instead.
      This is the name/location of where to create the file.
      Parameters:
      file - the path of the zipFile
      Since:
      Ant 1.5
    • setDestFile

      public void setDestFile(File destFile)
      The file to create; required.
      Parameters:
      destFile - The new destination File
      Since:
      Ant 1.5
    • getDestFile

      public File getDestFile()
      The file to create.
      Returns:
      the destination file
      Since:
      Ant 1.5.2
    • setBasedir

      public void setBasedir(File baseDir)
      Directory from which to archive files; optional.
      Parameters:
      baseDir - the base directory
    • setCompress

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

      public boolean isCompress()
      Whether we want to compress the files or only store them;
      Returns:
      true if the files are to be compressed
      Since:
      Ant 1.5.2
    • setFilesonly

      public void setFilesonly(boolean f)
      If true, emulate Sun's jar utility by not adding parent directories; optional, defaults to false.
      Parameters:
      f - if true, emulate sun's jar by not adding parent directories
    • setUpdate

      public void setUpdate(boolean c)
      If true, updates an existing file, otherwise overwrite any existing one; optional defaults to false.
      Parameters:
      c - if true, updates an existing zip file
    • isInUpdateMode

      public boolean isInUpdateMode()
      Are we updating an existing archive?
      Returns:
      true if updating an existing archive
    • addFileset

      public void addFileset(FileSet set)
      Adds a set of files.
      Parameters:
      set - the fileset to add
    • addZipfileset

      public void addZipfileset(ZipFileSet set)
      Adds a set of files that can be read from an archive and be given a prefix/fullpath.
      Parameters:
      set - the zipfileset to add
    • add

      public void add(ResourceCollection a)
      Add a collection of resources to be archived.
      Parameters:
      a - the resources to archive
      Since:
      Ant 1.7
    • addZipGroupFileset

      public void addZipGroupFileset(FileSet set)
      Adds a group of zip files.
      Parameters:
      set - the group (a fileset) to add
    • setDuplicate

      public void setDuplicate(Zip.Duplicate df)
      Sets behavior for when a duplicate file is about to be added - one of 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
      Parameters:
      df - a Duplicate enumerated value
    • 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.
      Parameters:
      we - a WhenEmpty enumerated value
    • setEncoding

      public void setEncoding(String encoding)
      Encoding to use for filenames, defaults to the platform's default encoding.

      For a list of possible values see https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html.

      Parameters:
      encoding - the encoding name
    • getEncoding

      public String getEncoding()
      Encoding to use for filenames.
      Returns:
      the name of the encoding to use
      Since:
      Ant 1.5.2
    • 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). Default is false.
      Parameters:
      keep - if true, keep the original compression
      Since:
      Ant 1.6
    • setComment

      public void setComment(String comment)
      Comment to use for archive.
      Parameters:
      comment - The content of the comment.
      Since:
      Ant 1.6.3
    • getComment

      public String getComment()
      Comment of the archive
      Returns:
      Comment of the archive.
      Since:
      Ant 1.6.3
    • setLevel

      public void setLevel(int level)
      Set the compression level to use. Default is ZipOutputStream.DEFAULT_COMPRESSION.
      Parameters:
      level - compression level.
      Since:
      Ant 1.7
    • getLevel

      public int getLevel()
      Get the compression level.
      Returns:
      compression level.
      Since:
      Ant 1.7
    • 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.

      Parameters:
      r - a boolean value
      Since:
      Ant 1.6.2
    • setPreserve0Permissions

      public void setPreserve0Permissions(boolean b)
      Assume 0 Unix mode is intentional.
      Parameters:
      b - boolean
      Since:
      Ant 1.8.0
    • getPreserve0Permissions

      public boolean getPreserve0Permissions()
      Assume 0 Unix mode is intentional.
      Returns:
      boolean
      Since:
      Ant 1.8.0
    • setUseLanguageEncodingFlag

      public void setUseLanguageEncodingFlag(boolean b)
      Whether to set the language encoding flag.
      Parameters:
      b - boolean
      Since:
      Ant 1.8.0
    • getUseLanguageEnodingFlag

      public boolean getUseLanguageEnodingFlag()
      Whether the language encoding flag will be used.
      Returns:
      boolean
      Since:
      Ant 1.8.0
    • setCreateUnicodeExtraFields

      public void setCreateUnicodeExtraFields(Zip.UnicodeExtraField b)
      Whether Unicode extra fields will be created.
      Parameters:
      b - boolean
      Since:
      Ant 1.8.0
    • getCreateUnicodeExtraFields

      public Zip.UnicodeExtraField getCreateUnicodeExtraFields()
      Whether Unicode extra fields will be created.
      Returns:
      boolean
      Since:
      Ant 1.8.0
    • setFallBackToUTF8

      public void setFallBackToUTF8(boolean b)
      Whether to fall back to UTF-8 if a name cannot be encoded using the specified encoding.

      Defaults to false.

      Parameters:
      b - boolean
      Since:
      Ant 1.8.0
    • getFallBackToUTF8

      public boolean getFallBackToUTF8()
      Whether to fall back to UTF-8 if a name cannot be encoded using the specified encoding.
      Returns:
      boolean
      Since:
      Ant 1.8.0
    • setZip64Mode

      public void setZip64Mode(Zip.Zip64ModeAttribute b)
      Whether Zip64 extensions should be used.
      Parameters:
      b - boolean
      Since:
      Ant 1.9.1
    • getZip64Mode

      public Zip.Zip64ModeAttribute getZip64Mode()
      Whether Zip64 extensions will be used.
      Returns:
      boolean
      Since:
      Ant 1.9.1
    • setModificationtime

      public void setModificationtime(String time)
      Set all stored file modification times to time.
      Parameters:
      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.
      Since:
      Ant 1.10.2
    • getModificationtime

      public String getModificationtime()
      The file modification time previously provided to setModificationtime(String) or null if unset.
      Returns:
      String
      Since:
      Ant 1.10.2
    • execute

      public void execute() throws BuildException
      validate and build
      Overrides:
      execute in class Task
      Throws:
      BuildException - on error
    • hasUpdatedFile

      protected boolean hasUpdatedFile()
      Get the value of the updatedFile attribute. This should only be called after executeMain has been called.
      Returns:
      true if executeMain has written to the zip file.
    • executeMain

      public void executeMain() throws BuildException
      Build the zip file. This is called twice if doubleFilePass is true.
      Throws:
      BuildException - on error
    • 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
      Returns:
      true if adding new files
    • 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
      Throws:
      IOException - on error
      Since:
      Ant 1.5.2
    • addResources

      protected final void addResources(ResourceCollection rc, Resource[] resources, ZipOutputStream zOut) throws IOException
      Add the given resources.
      Parameters:
      rc - may give additional information like fullpath or permissions.
      resources - the resources to add
      zOut - the stream to write to
      Throws:
      IOException - on error
      Since:
      Ant 1.7
    • initZipOutputStream

      protected void initZipOutputStream(ZipOutputStream zOut) throws IOException, BuildException
      method for subclasses to override
      Parameters:
      zOut - the zip output stream
      Throws:
      IOException - on output error
      BuildException - on other errors
    • finalizeZipOutputStream

      protected void finalizeZipOutputStream(ZipOutputStream zOut) throws IOException, BuildException
      method for subclasses to override
      Parameters:
      zOut - the zip output stream
      Throws:
      IOException - on output error
      BuildException - on other errors
    • createEmptyZip

      protected boolean createEmptyZip(File zipFile) throws BuildException
      Create an empty zip file
      Parameters:
      zipFile - the zip file
      Returns:
      true for historic reasons
      Throws:
      BuildException - on error
    • getResourcesToAdd

      protected Zip.ArchiveState getResourcesToAdd(ResourceCollection[] rcs, 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.

      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.

      Parameters:
      rcs - The resource collections 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
      Since:
      Ant 1.7
    • 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
    • getNonFileSetResourcesToAdd

      protected Zip.ArchiveState getNonFileSetResourcesToAdd(ResourceCollection[] rcs, 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:
      rcs - 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.

      Parameters:
      filesets - an array of filesets
      Returns:
      the resources included
      Since:
      Ant 1.5.2
    • grabNonFileSetResources

      protected Resource[][] grabNonFileSetResources(ResourceCollection[] rcs)
      Fetch all included and not excluded resources from the collections.

      Included directories will precede included files.

      Parameters:
      rcs - an array of resource collections
      Returns:
      the resources included
      Since:
      Ant 1.7
    • zipDir

      protected void zipDir(File dir, ZipOutputStream zOut, String vPath, int mode) throws IOException
      Add a directory to the zip stream.
      Parameters:
      dir - the directory 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.
      Throws:
      IOException - on error
      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 directory 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
      Throws:
      IOException - on error
      Since:
      Ant 1.6.3
    • zipDir

      protected void zipDir(Resource dir, ZipOutputStream zOut, String vPath, int mode, ZipExtraField[] extra) throws IOException
      Add a directory to the zip stream.
      Parameters:
      dir - the directory 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
      Throws:
      IOException - on error
      Since:
      Ant 1.8.0
    • getCurrentExtraFields

      protected final ZipExtraField[] getCurrentExtraFields()
      Provides the extra fields for the zip entry currently being added to the archive - if any.
      Returns:
      ZipExtraField[]
      Since:
      Ant 1.8.0
    • setCurrentExtraFields

      protected final void setCurrentExtraFields(ZipExtraField[] extra)
      Sets the extra fields for the zip entry currently being added to the archive - if any.
      Parameters:
      extra - ZipExtraField[]
      Since:
      Ant 1.8.0
    • 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. 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.
      Throws:
      IOException - on error
      Since:
      Ant 1.5.2
    • zipFile

      protected final void zipFile(InputStream in, ZipOutputStream zOut, String vPath, long lastModified, File fromArchive, int mode, ZipExtraField[] extra) 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. 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 add
      Throws:
      IOException - on error
      Since:
      Ant 1.8.0
    • 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.
      Throws:
      IOException - on error
      Since:
      Ant 1.5.2
    • 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.
      Parameters:
      baseDir - the base directory to use (may be null)
      entry - the entry name to create directories from
      zOut - the stream to write to
      prefix - a prefix to place on the created entries
      dirMode - the directory mode
      Throws:
      IOException - on error
      Since:
      Ant 1.5.2
    • 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

      public void reset()
      Makes this instance reset all attributes to their default values and forget all children.
      Since:
      Ant 1.5
      See Also:
    • isEmpty

      protected static final boolean isEmpty(Resource[][] r)
      Check is the resource arrays are empty.
      Parameters:
      r - the arrays to check
      Returns:
      true if all individual arrays are empty
      Since:
      Ant 1.5.2
    • selectFileResources

      protected Resource[] selectFileResources(Resource[] orig)
      Drops all non-file resources from the given array.
      Parameters:
      orig - the resources to filter
      Returns:
      the filters resources
      Since:
      Ant 1.6
    • selectDirectoryResources

      protected Resource[] selectDirectoryResources(Resource[] orig)
      Drops all non-directory resources from the given array.
      Parameters:
      orig - the resources to filter
      Returns:
      the filters resources
      Since:
      Ant 1.8.0
    • selectResources

      protected Resource[] selectResources(Resource[] orig, ResourceSelector selector)
      Drops all resources from the given array that are not selected
      Parameters:
      orig - the resources to filter
      selector - ResourceSelector
      Returns:
      the filters resources
      Since:
      Ant 1.8.0
    • logWhenWriting

      protected void logWhenWriting(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.
      Parameters:
      msg - String
      level - int
      Since:
      Ant 1.8.0