Class ZipArchiveEntry

  • All Implemented Interfaces:
    Cloneable, ArchiveEntry
    Direct Known Subclasses:
    JarArchiveEntry


    public class ZipArchiveEntry
    extends ZipEntry
    implements ArchiveEntry
    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.

    • Constructor Detail

      • ZipArchiveEntry

        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
      • ZipArchiveEntry

        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
      • ZipArchiveEntry

        public ZipArchiveEntry(ZipArchiveEntry 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
      • ZipArchiveEntry

        protected ZipArchiveEntry()
      • ZipArchiveEntry

        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.

    • Method Detail

      • clone

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

        public int getMethod()
        Returns 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
      • setMethod

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

        public int getInternalAttributes()
        Retrieves the internal file attributes.
        Returns:
        the internal file attributes
      • setInternalAttributes

        public void setInternalAttributes(int value)
        Sets the internal file attributes.
        Parameters:
        value - an int value
      • getExternalAttributes

        public long getExternalAttributes()
        Retrieves the external file attributes.
        Returns:
        the external file attributes
      • setExternalAttributes

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

        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
      • getUnixMode

        public int getUnixMode()
        Unix permission.
        Returns:
        the unix permissions
      • isUnixSymlink

        public boolean isUnixSymlink()
        Returns true if 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
      • getPlatform

        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.
      • setPlatform

        protected void setPlatform(int platform)
        Set the platform (UNIX or FAT).
        Parameters:
        platform - an int value - 0 is FAT, 3 is UNIX
      • setExtraFields

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

        public ZipExtraField[] getExtraFields()
        Retrieves all extra fields that have been parsed successfully.
        Returns:
        an array of the extra fields
      • getExtraFields

        public ZipExtraField[] getExtraFields(boolean includeUnparseable)
        Retrieves 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
      • addExtraField

        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
      • addAsFirstExtraField

        public void addAsFirstExtraField(ZipExtraField ze)
        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
      • removeExtraField

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

        public void removeUnparseableExtraFieldData()
        Removes unparseable extra field data.
        Since:
        1.1
      • getExtraField

        public ZipExtraField getExtraField(ZipShort type)
        Looks up an extra field by its header id.
        Returns:
        null if no such field exists.
      • getUnparseableExtraFieldData

        public UnparseableExtraFieldData getUnparseableExtraFieldData()
        Looks up extra field data that couldn't be parsed correctly.
        Returns:
        null if no such field exists.
        Since:
        1.1
      • setExtra

        protected void setExtra()
        Unfortunately java.util.zip.ZipOutputStream seems to access the extra data directly, so overriding getExtra doesn't help - we need to modify super's data directly.
      • setCentralDirectoryExtra

        public void setCentralDirectoryExtra(byte[] b)
        Sets the central directory part of extra fields.
      • getLocalFileDataExtra

        public byte[] getLocalFileDataExtra()
        Retrieves the extra data for the local file data.
        Returns:
        the extra data for local file
      • getCentralDirectoryExtra

        public byte[] getCentralDirectoryExtra()
        Retrieves the extra data for the central directory.
        Returns:
        the central directory extra data
      • isDirectory

        public boolean isDirectory()
        Is this entry a directory?
        Specified by:
        isDirectory in interface  ArchiveEntry
        Overrides:
        isDirectory in class  ZipEntry
        Returns:
        true if the entry is a directory
      • setName

        protected void setName(String name)
        Set the name of the entry.
        Parameters:
        name - the name to use
      • getSize

        public long getSize()
        Gets the uncompressed size of the entry data.
        Specified by:
        getSize in interface  ArchiveEntry
        Overrides:
        getSize in class  ZipEntry
        Returns:
        the entry size
      • setSize

        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
      • setName

        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
      • getRawName

        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.

        Since:
        1.2
      • hashCode

        public int hashCode()
        Get the hashCode of the entry. This uses the name as the hashcode.
        Overrides:
        hashCode in class  ZipEntry
        Returns:
        a hashcode.
      • getGeneralPurposeBit

        public GeneralPurposeBit getGeneralPurposeBit()
        The "general purpose bit" field.
        Since:
        1.1
      • setGeneralPurposeBit

        public void setGeneralPurposeBit(GeneralPurposeBit b)
        The "general purpose bit" field.
        Since:
        1.1
      • getLastModifiedDate

        public Date getLastModifiedDate()
        Description copied from interface: ArchiveEntry
        Gets the last modified date of this entry.
        Specified by:
        getLastModifiedDate in interface  ArchiveEntry
        Returns:
        the last modified date of this entry.
      • equals

        public boolean equals(Object obj)