Class ZipArchiveEntry

java.lang.Object
java.util.zip.ZipEntry
org.apache.commons.compress.archivers.zip.ZipArchiveEntry
All Implemented Interfaces:
Cloneable, ArchiveEntry, EntryStreamOffsets
Direct Known Subclasses:
JarArchiveEntry

public class ZipArchiveEntry extends ZipEntry implements ArchiveEntry, EntryStreamOffsets
Extension that adds better handling of extra fields and provides access to the internal and external file attributes.

The extra data is expected to follow the recommendation of APPNOTE.TXT:

  • the extra byte array consists of a sequence of extra fields
  • each extra fields starts by a two byte header id followed by a two byte sequence holding the length of the remainder of data.

Any extra data that cannot be parsed by the rules above will be consumed as "unparseable" extra data and treated differently by the methods of this class. Versions prior to Apache Commons Compress 1.1 would have thrown an exception if any attempt was made to read or write extra data not conforming to the recommendation.

This class is not thread-safe
  • Field Details Link icon

  • Constructor Details Link icon

    • ZipArchiveEntry Link icon

      protected ZipArchiveEntry()
    • ZipArchiveEntry Link icon

      public ZipArchiveEntry(File inputFile, String entryName)
      Creates a new ZIP entry taking some information from the given file and using the provided name.

      The name will be adjusted to end with a forward slash "/" if the file is a directory. If the file is not a directory a potential trailing forward slash will be stripped from the entry name.

      Parameters:
      inputFile - file to create the entry from
      entryName - name of the entry
    • ZipArchiveEntry Link icon

      public ZipArchiveEntry(Path inputPath, String entryName, LinkOption... options) throws IOException
      Creates a new ZIP entry taking some information from the given path and using the provided name.

      The name will be adjusted to end with a forward slash "/" if the file is a directory. If the file is not a directory a potential trailing forward slash will be stripped from the entry name.

      Parameters:
      inputPath - path to create the entry from.
      entryName - name of the entry.
      options - options indicating how symbolic links are handled.
      Throws:
      IOException - if an I/O error occurs.
      Since:
      1.21
    • ZipArchiveEntry Link icon

      public ZipArchiveEntry(String name)
      Creates a new ZIP entry with the specified name.

      Assumes the entry represents a directory if and only if the name ends with a forward slash "/".

      Parameters:
      name - the name of the entry
      Since:
      1.26.0
    • ZipArchiveEntry Link icon

      Creates a new ZIP entry with fields taken from the specified ZIP entry.

      Assumes the entry represents a directory if and only if the name ends with a forward slash "/".

      Parameters:
      entry - the entry to get fields from
      Throws:
      ZipException - on error
    • ZipArchiveEntry Link icon

      public ZipArchiveEntry(ZipEntry entry) throws ZipException
      Creates a new ZIP entry with fields taken from the specified ZIP entry.

      Assumes the entry represents a directory if and only if the name ends with a forward slash "/".

      Parameters:
      entry - the entry to get fields from
      Throws:
      ZipException - on error
  • Method Details Link icon

    • addAsFirstExtraField Link icon

      Adds an extra field - replacing an already present extra field of the same type.

      The new extra field will be the first one.

      Parameters:
      ze - an extra field
    • addExtraField Link icon

      public void addExtraField(ZipExtraField ze)
      Adds an extra field - replacing an already present extra field of the same type.

      If no extra field of the same type exists, the field will be added as last field.

      Parameters:
      ze - an extra field
    • clone Link icon

      public Object clone()
      Overwrite clone.
      Overrides:
      clone in class ZipEntry
      Returns:
      a cloned copy of this ZipArchiveEntry
    • equals Link icon

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getAlignment Link icon

      protected int getAlignment()
      Gets currently configured alignment.
      Returns:
      alignment for this entry.
      Since:
      1.14
    • getCentralDirectoryExtra Link icon

      public byte[] getCentralDirectoryExtra()
      Gets the extra data for the central directory.
      Returns:
      the central directory extra data
    • getCommentSource Link icon

      * Gets the source of the comment field value.
      Returns:
      source of the comment field value
      Since:
      1.16
    • getDataOffset Link icon

      public long getDataOffset()
      Description copied from interface: EntryStreamOffsets
      Gets the offset of data stream within the archive file,
      Specified by:
      getDataOffset in interface EntryStreamOffsets
      Returns:
      the offset of entry data stream, OFFSET_UNKNOWN if not known.
    • getDiskNumberStart Link icon

      public long getDiskNumberStart()
      Gets the number of the split segment this entry starts at.
      Returns:
      the number of the split segment this entry starts at.
      Since:
      1.20
    • getExternalAttributes Link icon

      public long getExternalAttributes()
      Gets the external file attributes.

      Note: ZipArchiveInputStream is unable to fill this field, you must use ZipFile if you want to read entries using this attribute.

      Returns:
      the external file attributes
    • getExtraField Link icon

      Gets an extra field by its header id.
      Parameters:
      type - the header id
      Returns:
      null if no such field exists.
    • getExtraFields Link icon

      Gets all extra fields that have been parsed successfully.

      Note: The set of extra fields may be incomplete when ZipArchiveInputStream has been used as some extra fields use the central directory to store additional information.

      Returns:
      an array of the extra fields
    • getExtraFields Link icon

      public ZipExtraField[] getExtraFields(boolean includeUnparseable)
      Gets extra fields.
      Parameters:
      includeUnparseable - whether to also return unparseable extra fields as UnparseableExtraFieldData if such data exists.
      Returns:
      an array of the extra fields
      Since:
      1.1
    • getExtraFields Link icon

      Gets extra fields.
      Parameters:
      parsingBehavior - controls parsing of extra fields.
      Returns:
      an array of the extra fields
      Throws:
      ZipException - if parsing fails, cannot happen if parsingBehavior is ZipArchiveEntry.ExtraFieldParsingMode.BEST_EFFORT.
      Since:
      1.19
    • getGeneralPurposeBit Link icon

      The "general purpose bit" field.
      Returns:
      the general purpose bit
      Since:
      1.1
    • getInternalAttributes Link icon

      public int getInternalAttributes()
      Gets the internal file attributes.

      Note: ZipArchiveInputStream is unable to fill this field, you must use ZipFile if you want to read entries using this attribute.

      Returns:
      the internal file attributes
    • getLastModifiedDate Link icon

      Wraps ZipEntry.getTime() with a Date as the entry's last modified date.

      Changes to the implementation of ZipEntry.getTime() leak through and the returned value may depend on your local time zone as well as your version of Java.

      Specified by:
      getLastModifiedDate in interface ArchiveEntry
      Returns:
      the last modified date of this entry.
    • getLocalFileDataExtra Link icon

      public byte[] getLocalFileDataExtra()
      Gets the extra data for the local file data.
      Returns:
      the extra data for local file
    • getLocalHeaderOffset Link icon

      public long getLocalHeaderOffset()
      Gets the local header offset.
      Returns:
      the local header offset.
      Since:
      1.24.0
    • getMethod Link icon

      public int getMethod()
      Gets the compression method of this entry, or -1 if the compression method has not been specified.
      Overrides:
      getMethod in class ZipEntry
      Returns:
      compression method
      Since:
      1.1
    • getName Link icon

      public String getName()
      Gets the name of the entry.

      This method returns the raw name as it is stored inside of the archive.

      Specified by:
      getName in interface ArchiveEntry
      Overrides:
      getName in class ZipEntry
      Returns:
      the entry name
    • getNameSource Link icon

      The source of the name field value.
      Returns:
      source of the name field value
      Since:
      1.16
    • getPlatform Link icon

      public int getPlatform()
      Platform specification to put into the "version made by" part of the central file header.
      Returns:
      PLATFORM_FAT unless setUnixMode has been called, in which case PLATFORM_UNIX will be returned.
    • getRawFlag Link icon

      public int getRawFlag()
      The content of the flags field.
      Returns:
      content of the flags field
      Since:
      1.11
    • getRawName Link icon

      public byte[] getRawName()
      Returns the raw bytes that made up the name before it has been converted using the configured or guessed encoding.

      This method will return null if this instance has not been read from an archive.

      Returns:
      the raw name bytes
      Since:
      1.2
    • getSize Link icon

      public long getSize()
      Gets the uncompressed size of the entry data.

      Note: ZipArchiveInputStream may create entries that return SIZE_UNKNOWN as long as the entry hasn't been read completely.

      Specified by:
      getSize in interface ArchiveEntry
      Overrides:
      getSize in class ZipEntry
      Returns:
      the entry size
    • getTime Link icon

      public long getTime()

      Override to work around bug JDK-8130914

      Overrides:
      getTime in class ZipEntry
      Returns:
      The last modification time of the entry in milliseconds since the epoch, or -1 if not specified
      See Also:
    • getUnixMode Link icon

      public int getUnixMode()
      Gets the Unix permission.
      Returns:
      the Unix permissions.
    • getUnparseableExtraFieldData Link icon

      Gets up extra field data that couldn't be parsed correctly.
      Returns:
      null if no such field exists.
      Since:
      1.1
    • getVersionMadeBy Link icon

      public int getVersionMadeBy()
      Gets the "version made by" field.
      Returns:
      "version made by" field
      Since:
      1.11
    • getVersionRequired Link icon

      public int getVersionRequired()
      Gets the "version required to expand" field.
      Returns:
      "version required to expand" field
      Since:
      1.11
    • hashCode Link icon

      public int hashCode()
      Gets the hash code of the entry. This uses the name as the hash code.
      Overrides:
      hashCode in class ZipEntry
      Returns:
      a hash code.
    • isDirectory Link icon

      public boolean isDirectory()
      Tests whether this entry is a directory.
      Specified by:
      isDirectory in interface ArchiveEntry
      Overrides:
      isDirectory in class ZipEntry
      Returns:
      true if the entry is a directory.
    • isStreamContiguous Link icon

      public boolean isStreamContiguous()
      Description copied from interface: EntryStreamOffsets
      Tests whether the stream is contiguous, that is, not split among several archive parts, interspersed with control blocks, and so on.
      Specified by:
      isStreamContiguous in interface EntryStreamOffsets
      Returns:
      true if stream is contiguous, false otherwise.
    • isUnixSymlink Link icon

      public boolean isUnixSymlink()
      Tests whether this entry represents a Unix symlink, in which case the entry's content contains the target path for the symlink.
      Returns:
      true if the entry represents a Unix symlink, false otherwise.
      Since:
      1.5
    • removeExtraField Link icon

      public void removeExtraField(ZipShort type)
      Remove an extra field.
      Parameters:
      type - the type of extra field to remove
    • removeUnparseableExtraFieldData Link icon

      Removes unparseable extra field data.
      Since:
      1.1
    • setAlignment Link icon

      public void setAlignment(int alignment)
      Sets alignment for this entry.
      Parameters:
      alignment - requested alignment, 0 for default.
      Since:
      1.14
    • setCentralDirectoryExtra Link icon

      public void setCentralDirectoryExtra(byte[] b)
      Sets the central directory part of extra fields.
      Parameters:
      b - an array of bytes to be parsed into extra fields
    • setCommentSource Link icon

      public void setCommentSource(ZipArchiveEntry.CommentSource commentSource)
      Sets the source of the comment field value.
      Parameters:
      commentSource - source of the comment field value
      Since:
      1.16
    • setCreationTime Link icon

      Overrides:
      setCreationTime in class ZipEntry
    • setDataOffset Link icon

      protected void setDataOffset(long dataOffset)
      Sets the data offset.
      Parameters:
      dataOffset - new value of data offset.
    • setDiskNumberStart Link icon

      public void setDiskNumberStart(long diskNumberStart)
      The number of the split segment this entry starts at.
      Parameters:
      diskNumberStart - the number of the split segment this entry starts at.
      Since:
      1.20
    • setExternalAttributes Link icon

      public void setExternalAttributes(long value)
      Sets the external file attributes.
      Parameters:
      value - an long value
    • setExtra Link icon

      protected void setExtra()
      Unfortunately ZipOutputStream seems to access the extra data directly, so overriding getExtra doesn't help - we need to modify super's data directly and on every update.
    • setExtra Link icon

      public void setExtra(byte[] extra) throws RuntimeException
      Parses the given bytes as extra field data and consumes any unparseable data as an UnparseableExtraFieldData instance.
      Overrides:
      setExtra in class ZipEntry
      Parameters:
      extra - an array of bytes to be parsed into extra fields
      Throws:
      RuntimeException - if the bytes cannot be parsed
      RuntimeException - on error
    • setExtraFields Link icon

      public void setExtraFields(ZipExtraField[] fields)
      Replaces all currently attached extra fields with the new array.
      Parameters:
      fields - an array of extra fields
    • setGeneralPurposeBit Link icon

      public void setGeneralPurposeBit(GeneralPurposeBit generalPurposeBit)
      Sets the "general purpose bit" field.
      Parameters:
      generalPurposeBit - the general purpose bit
      Since:
      1.1
    • setInternalAttributes Link icon

      public void setInternalAttributes(int internalAttributes)
      Sets the internal file attributes.
      Parameters:
      internalAttributes - an int value
    • setLastAccessTime Link icon

      public ZipEntry setLastAccessTime(FileTime fileTime)
      Overrides:
      setLastAccessTime in class ZipEntry
    • setLastModifiedTime Link icon

      Overrides:
      setLastModifiedTime in class ZipEntry
    • setLocalHeaderOffset Link icon

      protected void setLocalHeaderOffset(long localHeaderOffset)
      Sets the local header offset.
      Parameters:
      localHeaderOffset - the local header offset.
    • setMethod Link icon

      public void setMethod(int method)
      Sets the compression method of this entry.
      Overrides:
      setMethod in class ZipEntry
      Parameters:
      method - compression method
      Since:
      1.1
    • setName Link icon

      protected void setName(String name)
      Sets the name of the entry.
      Parameters:
      name - the name to use
    • setName Link icon

      protected void setName(String name, byte[] rawName)
      Sets the name using the raw bytes and the string created from it by guessing or using the configured encoding.
      Parameters:
      name - the name to use created from the raw bytes using the guessed or configured encoding
      rawName - the bytes originally read as name from the archive
      Since:
      1.2
    • setNameSource Link icon

      public void setNameSource(ZipArchiveEntry.NameSource nameSource)
      Sets the source of the name field value.
      Parameters:
      nameSource - source of the name field value
      Since:
      1.16
    • setPlatform Link icon

      protected void setPlatform(int platform)
      Sets the platform (Unix or FAT).
      Parameters:
      platform - an int value - 0 is FAT, 3 is Unix
    • setRawFlag Link icon

      public void setRawFlag(int rawFlag)
      Sets the content of the flags field.
      Parameters:
      rawFlag - content of the flags field
      Since:
      1.11
    • setSize Link icon

      public void setSize(long size)
      Sets the uncompressed size of the entry data.
      Overrides:
      setSize in class ZipEntry
      Parameters:
      size - the uncompressed size in bytes
      Throws:
      IllegalArgumentException - if the specified size is less than 0
    • setStreamContiguous Link icon

      protected void setStreamContiguous(boolean isStreamContiguous)
      Sets whether the stream is contiguous, that is, not split among several archive parts, interspersed with control blocks, and so on.
      Parameters:
      isStreamContiguous - whether the stream is contiguous
    • setTime Link icon

      public void setTime(FileTime fileTime)
      Sets the modification time of the entry.
      Parameters:
      fileTime - the entry modification time.
      Since:
      1.21
    • setTime Link icon

      public void setTime(long timeEpochMillis)

      Override to work around bug JDK-8130914

      Overrides:
      setTime in class ZipEntry
      Parameters:
      timeEpochMillis - The last modification time of the entry in milliseconds since the epoch.
      See Also:
    • setUnixMode Link icon

      public void setUnixMode(int mode)
      Sets Unix permissions in a way that is understood by Info-Zip's unzip command.
      Parameters:
      mode - an int value
    • setVersionMadeBy Link icon

      public void setVersionMadeBy(int versionMadeBy)
      Sets the "version made by" field.
      Parameters:
      versionMadeBy - "version made by" field
      Since:
      1.11
    • setVersionRequired Link icon

      public void setVersionRequired(int versionRequired)
      Sets the "version required to expand" field.
      Parameters:
      versionRequired - "version required to expand" field
      Since:
      1.11