Top Description Fields Constructors Methods
org.apache.tools.zip

public Class ZipEntry

extends ZipEntry
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Known Direct Subclasses
org.apache.tools.zip.ZipFile.Entry
Imports
java.io.File, java.util.ArrayList, .Arrays, .Date, .List, .NoSuchElementException, java.util.zip.ZipException

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:

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. Older versions would have thrown an exception if any attempt was made to read or write extra data not conforming to the recommendation.

Field Summary

Modifier and TypeField and Description
public static final int
private static final byte[]
private long
private ZipExtraField[]
private GeneralPurposeBit
private int
private int
method

Hides java.util.zip.ZipEntry.method.

The java.util.zip.ZipEntry base class only supports the compression methods STORED and DEFLATED.
private String
private static final ZipExtraField[]
private int
public static final int
public static final int
private byte[]
private static final int
private static final int
private long
size

Hides java.util.zip.ZipEntry.size.

The java.util.zip.ZipEntry#setSize method in the base class throws an IllegalArgumentException if the size is bigger than 2GB for Java versions < 7.
private UnparseableExtraFieldData
Inherited from java.util.zip.ZipEntry:
DEFLATEDSTORED

Constructor Summary

AccessConstructor and Description
public
ZipEntry(final String
the name of the entry
name
)

Creates a new zip entry with the specified name.

public
ZipEntry(final ZipEntry
the entry to get fields from
entry
)

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

public
ZipEntry(final ZipEntry
the entry to get fields from
entry
)

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

protected
public
ZipEntry(final File
File
inputFile
,
final String
String
entryName
)

Creates a new zip entry taking some information from the given file and using the provided name.

Method Summary

Modifier and TypeMethod and Description
public void
addAsFirstExtraField(final ZipExtraField
an extra field
ze
)

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

public void
addExtraField(final ZipExtraField
an extra field
ze
)

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

public Object

Returns:

a cloned copy of this ZipEntry
clone
()

Overrides java.util.zip.ZipEntry.clone.

Override clone.
private ZipExtraField[]
private ZipExtraField[]
copyOf(ZipExtraField[] src, int length)

public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
private ZipExtraField[]
private ZipExtraField[]

Returns:

An array of all extra fields. Not necessarily a copy of internal data structures, hence private method
getAllExtraFieldsNoCopy
()

Get all extra fields, including unparseable ones.

public byte[]

Returns:

the central directory extra data
getCentralDirectoryExtra
()

Retrieves the extra data for the central directory.

public long

Returns:

the external file attributes
getExternalAttributes
()

Retrieves the external file attributes.

public ZipExtraField

Returns:

null if no such field exists.
getExtraField
(final ZipShort
ZipShort
type
)

Looks up an extra field by its header id.

public ZipExtraField[]

Returns:

an array of the extra fields
getExtraFields
()

Retrieves all extra fields that have been parsed successfully.

public ZipExtraField[]

Returns:

an array of the extra fields
getExtraFields
(final boolean
whether to also return unparseable extra fields as UnparseableExtraFieldData if such data exists.
includeUnparseable
)

Retrieves extra fields.

public GeneralPurposeBit

Returns:

GeneralPurposeBit
getGeneralPurposeBit
()

The "general purpose bit" field.

public int

Returns:

the internal file attributes
getInternalAttributes
()

Retrieves the internal file attributes.

public Date

Returns:

Date
getLastModifiedDate
()

Get last modified time as Date.

public byte[]

Returns:

the extra data for local file
getLocalFileDataExtra
()

Retrieves the extra data for the local file data.

private ZipExtraField[]
public int

Returns:

compression method
getMethod
()

Overrides java.util.zip.ZipEntry.getMethod.

Returns the compression method of this entry, or -1 if the compression method has not been specified.
public String

Returns:

the entry name
getName
()

Overrides java.util.zip.ZipEntry.getName.

Get the name of the entry.
private ZipExtraField[]
private ZipExtraField[]
public int

Returns:

PLATFORM_FAT unless setUnixMode has been called, in which case PLATFORM_UNIX will be returned.
getPlatform
()

Platform specification to put into the "version made by" part of the central file header.

public byte[]

Returns:

byte[]
getRawName
()

Returns the raw bytes that made up the name before it has been converted using the configured or guessed encoding.

public long

Returns:

the entry size
getSize
()

Overrides java.util.zip.ZipEntry.getSize.

Gets the uncompressed size of the entry data.
public int

Returns:

the unix permissions
getUnixMode
()

Unix permission.

public UnparseableExtraFieldData

Returns:

null if no such field exists.
getUnparseableExtraFieldData
()

Looks up extra field data that couldn't be parsed correctly.

private ZipExtraField[]
public int

Returns:

a hashcode.
hashCode
()

Overrides java.util.zip.ZipEntry.hashCode.

Get the hashCode of the entry.
public boolean

Returns:

true if the entry is a directory
isDirectory
()

Overrides java.util.zip.ZipEntry.isDirectory.

Is this entry a directory?
private void
mergeExtraFields(final ZipExtraField[]
the extra fields to merge
f
,
final boolean
whether the new fields originate from local data
local
)

If there are no extra fields, use the given fields as new extra data - otherwise merge the fields assuming the existing fields and the new fields stem from different locations inside the archive.

public void
removeExtraField(final ZipShort
the type of extra field to remove
type
)

Remove an extra field.

public void
removeUnparseableExtraFieldData()

Removes unparseable extra field data.

public void
setCentralDirectoryExtra(final byte[]
boolean
b
)

Sets the central directory part of extra fields.

public void
setComprSize(final long
the size to use
size
)

Deprecated since 1.7. Use setCompressedSize(long) directly.
Make this class work in JDK 1.1 like a 1.2 class.
public void
setExternalAttributes(final long
an long value
value
)

Sets the external file attributes.

public void
setExtra(final byte[]
an array of bytes to be parsed into extra fields
extra
)

Overrides java.util.zip.ZipEntry.setExtra.

Parses the given bytes as extra field data and consumes any unparseable data as an UnparseableExtraFieldData instance.
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.

public void
setExtraFields(final ZipExtraField[]
an array of extra fields
fields
)

Replaces all currently attached extra fields with the new array.

public void
setGeneralPurposeBit(final GeneralPurposeBit
GeneralPurposeBit
b
)

The "general purpose bit" field.

public void
setInternalAttributes(final int
an int value
value
)

Sets the internal file attributes.

public void
setMethod(final int
compression method
method
)

Overrides java.util.zip.ZipEntry.setMethod.

Sets the compression method of this entry.
protected void
setName(String
the name to use
name
)

Set the name of the entry.

protected void
setName(final String
the name to use created from the raw bytes using the guessed or configured encoding
name
,
final byte[]
the bytes originally read as name from the archive
rawName
)

Sets the name using the raw bytes and the string created from it by guessing or using the configured encoding.

protected void
setPlatform(final int
an int value - 0 is FAT, 3 is UNIX
platform
)

Set the platform (UNIX or FAT).

public void
setSize(final long
the uncompressed size in bytes
size
)

Overrides java.util.zip.ZipEntry.setSize.

Sets the uncompressed size of the entry data.
public void
setUnixMode(final int
an int value
mode
)

Sets Unix permissions in a way that is understood by Info-Zip's unzip command.

Inherited from java.util.zip.ZipEntry:
getCommentgetCompressedSizegetCrcgetCreationTimegetExtragetLastAccessTimegetLastModifiedTimegetTimegetTimeLocalsetCommentsetCompressedSizesetCrcsetCreationTimesetLastAccessTimesetLastModifiedTimesetTimesetTimeLocaltoString

Field Detail

CRC_UNKNOWNback to summary
public static final int CRC_UNKNOWN
EMPTYback to summary
private static final byte[] EMPTY
externalAttributesback to summary
private long externalAttributes
extraFieldsback to summary
private ZipExtraField[] extraFields
gpbback to summary
private GeneralPurposeBit gpb
internalAttributesback to summary
private int internalAttributes
methodback to summary
private int method

Hides java.util.zip.ZipEntry.method.

The java.util.zip.ZipEntry base class only supports the compression methods STORED and DEFLATED. We override the field so that any compression methods can be used.

The default value -1 means that the method has not been specified.

nameback to summary
private String name

Hides java.util.zip.ZipEntry.name.

noExtraFieldsback to summary
private static final ZipExtraField[] noExtraFields
platformback to summary
private int platform
PLATFORM_FATback to summary
public static final int PLATFORM_FAT
PLATFORM_UNIXback to summary
public static final int PLATFORM_UNIX
rawNameback to summary
private byte[] rawName
SHORT_MASKback to summary
private static final int SHORT_MASK
SHORT_SHIFTback to summary
private static final int SHORT_SHIFT
sizeback to summary
private long size

Hides java.util.zip.ZipEntry.size.

The java.util.zip.ZipEntry#setSize method in the base class throws an IllegalArgumentException if the size is bigger than 2GB for Java versions < 7. Need to keep our own size information for Zip64 support.

unparseableExtraback to summary
private UnparseableExtraFieldData unparseableExtra

Constructor Detail

ZipEntryback to summary
public ZipEntry(final 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:String

the name of the entry

Since
1.1
ZipEntryback to summary
public ZipEntry(final 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:ZipEntry

the entry to get fields from

Exceptions
ZipException:
on error
Since
1.1
ZipEntryback to summary
public ZipEntry(final 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:ZipEntry

the entry to get fields from

Exceptions
ZipException:
on error
Since
1.1
ZipEntryback to summary
protected ZipEntry()
Since
1.9
ZipEntryback to summary
public ZipEntry(final File inputFile, final 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

File

entryName:String

String

Method Detail

addAsFirstExtraFieldback to summary
public void addAsFirstExtraField(final 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:ZipExtraField

an extra field

Since
1.1
addExtraFieldback to summary
public void addExtraField(final 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:ZipExtraField

an extra field

Since
1.1
cloneback to summary
public Object clone()

Overrides java.util.zip.ZipEntry.clone.

Override clone.

Returns:Object

a cloned copy of this ZipEntry

Annotations
@Override
Since
1.1
copyOfback to summary
private ZipExtraField[] copyOf(ZipExtraField[] src)
copyOfback to summary
private ZipExtraField[] copyOf(ZipExtraField[] src, int length)
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getAllExtraFieldsback to summary
private ZipExtraField[] getAllExtraFields()
getAllExtraFieldsNoCopyback to summary
private ZipExtraField[] getAllExtraFieldsNoCopy()

Get all extra fields, including unparseable ones.

Returns:ZipExtraField[]

An array of all extra fields. Not necessarily a copy of internal data structures, hence private method

getCentralDirectoryExtraback to summary
public byte[] getCentralDirectoryExtra()

Retrieves the extra data for the central directory.

Returns:byte[]

the central directory extra data

Since
1.1
getExternalAttributesback to summary
public long getExternalAttributes()

Retrieves the external file attributes.

Returns:long

the external file attributes

Since
1.1
getExtraFieldback to summary
public ZipExtraField getExtraField(final ZipShort type)

Looks up an extra field by its header id.

Parameters
type:ZipShort

ZipShort

Returns:ZipExtraField

null if no such field exists.

getExtraFieldsback to summary
public ZipExtraField[] getExtraFields()

Retrieves all extra fields that have been parsed successfully.

Returns:ZipExtraField[]

an array of the extra fields

getExtraFieldsback to summary
public ZipExtraField[] getExtraFields(final boolean includeUnparseable)

Retrieves extra fields.

Parameters
includeUnparseable:boolean

whether to also return unparseable extra fields as UnparseableExtraFieldData if such data exists.

Returns:ZipExtraField[]

an array of the extra fields

Since
1.1
getGeneralPurposeBitback to summary
public GeneralPurposeBit getGeneralPurposeBit()

The "general purpose bit" field.

Returns:GeneralPurposeBit

GeneralPurposeBit

getInternalAttributesback to summary
public int getInternalAttributes()

Retrieves the internal file attributes.

Returns:int

the internal file attributes

Since
1.1
getLastModifiedDateback to summary
public Date getLastModifiedDate()

Get last modified time as Date.

Returns:Date

Date

getLocalFileDataExtraback to summary
public byte[] getLocalFileDataExtra()

Retrieves the extra data for the local file data.

Returns:byte[]

the extra data for local file

Since
1.1
getMergedFieldsback to summary
private ZipExtraField[] getMergedFields()
getMethodback to summary
public int getMethod()

Overrides java.util.zip.ZipEntry.getMethod.

Returns the compression method of this entry, or -1 if the compression method has not been specified.

Returns:int

compression method

Annotations
@Override
getNameback to summary
public String getName()

Overrides java.util.zip.ZipEntry.getName.

Get the name of the entry.

Returns:String

the entry name

Annotations
@Override
Since
1.9
getParseableExtraFieldsback to summary
private ZipExtraField[] getParseableExtraFields()
getParseableExtraFieldsNoCopyback to summary
private ZipExtraField[] getParseableExtraFieldsNoCopy()
getPlatformback to summary
public int getPlatform()

Platform specification to put into the "version made by" part of the central file header.

Returns:int

PLATFORM_FAT unless setUnixMode has been called, in which case PLATFORM_UNIX will be returned.

Since
Ant 1.5.2
getRawNameback to summary
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:byte[]

byte[]

getSizeback to summary
public long getSize()

Overrides java.util.zip.ZipEntry.getSize.

Gets the uncompressed size of the entry data.

Returns:long

the entry size

Annotations
@Override
getUnixModeback to summary
public int getUnixMode()

Unix permission.

Returns:int

the unix permissions

Since
Ant 1.6
getUnparseableExtraFieldDataback to summary
public UnparseableExtraFieldData getUnparseableExtraFieldData()

Looks up extra field data that couldn't be parsed correctly.

Returns:UnparseableExtraFieldData

null if no such field exists.

getUnparseableOnlyback to summary
private ZipExtraField[] getUnparseableOnly()
hashCodeback to summary
public int hashCode()

Overrides java.util.zip.ZipEntry.hashCode.

Get the hashCode of the entry. This uses the name as the hashcode.

Returns:int

a hashcode.

Annotations
@Override
Since
Ant 1.7
isDirectoryback to summary
public boolean isDirectory()

Overrides java.util.zip.ZipEntry.isDirectory.

Is this entry a directory?

Returns:boolean

true if the entry is a directory

Annotations
@Override
Since
1.10
mergeExtraFieldsback to summary
private void mergeExtraFields(final ZipExtraField[] f, final boolean local) throws ZipException

If there are no extra fields, use the given fields as new extra data - otherwise merge the fields assuming the existing fields and the new fields stem from different locations inside the archive.

Parameters
f:ZipExtraField[]

the extra fields to merge

local:boolean

whether the new fields originate from local data

removeExtraFieldback to summary
public void removeExtraField(final ZipShort type)

Remove an extra field.

Parameters
type:ZipShort

the type of extra field to remove

Since
1.1
removeUnparseableExtraFieldDataback to summary
public void removeUnparseableExtraFieldData()

Removes unparseable extra field data.

setCentralDirectoryExtraback to summary
public void setCentralDirectoryExtra(final byte[] b)

Sets the central directory part of extra fields.

Parameters
b:byte[]

boolean

setComprSizeback to summary
public void setComprSize(final long size)

Deprecated

since 1.7. Use setCompressedSize(long) directly.

Make this class work in JDK 1.1 like a 1.2 class.

This either stores the size for later usage or invokes setCompressedSize via reflection.

Parameters
size:long

the size to use

Annotations
@Deprecated
Since
1.2
setExternalAttributesback to summary
public void setExternalAttributes(final long value)

Sets the external file attributes.

Parameters
value:long

an long value

Since
1.1
setExtraback to summary
public void setExtra(final byte[] extra) throws RuntimeException

Overrides java.util.zip.ZipEntry.setExtra.

Parses the given bytes as extra field data and consumes any unparseable data as an UnparseableExtraFieldData instance.

Parameters
extra:byte[]

an array of bytes to be parsed into extra fields

Annotations
@Override
Exceptions
RuntimeException:
  • if the bytes cannot be parsed
  • on error
Since
1.1
setExtraback to summary
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.

Since
1.1
setExtraFieldsback to summary
public void setExtraFields(final ZipExtraField[] fields)

Replaces all currently attached extra fields with the new array.

Parameters
fields:ZipExtraField[]

an array of extra fields

Since
1.1
setGeneralPurposeBitback to summary
public void setGeneralPurposeBit(final GeneralPurposeBit b)

The "general purpose bit" field.

Parameters
b:GeneralPurposeBit

GeneralPurposeBit

setInternalAttributesback to summary
public void setInternalAttributes(final int value)

Sets the internal file attributes.

Parameters
value:int

an int value

Since
1.1
setMethodback to summary
public void setMethod(final int method)

Overrides java.util.zip.ZipEntry.setMethod.

Sets the compression method of this entry.

Parameters
method:int

compression method

Annotations
@Override
setNameback to summary
protected void setName(String name)

Set the name of the entry.

Parameters
name:String

the name to use

setNameback to summary
protected void setName(final String name, final byte[] rawName)

Sets the name using the raw bytes and the string created from it by guessing or using the configured encoding.

Parameters
name:String

the name to use created from the raw bytes using the guessed or configured encoding

rawName:byte[]

the bytes originally read as name from the archive

setPlatformback to summary
protected void setPlatform(final int platform)

Set the platform (UNIX or FAT).

Parameters
platform:int

an int value - 0 is FAT, 3 is UNIX

Since
1.9
setSizeback to summary
public void setSize(final long size)

Overrides java.util.zip.ZipEntry.setSize.

Sets the uncompressed size of the entry data.

Parameters
size:long

the uncompressed size in bytes

Annotations
@Override
Exceptions
IllegalArgumentException:
if the specified size is less than 0
setUnixModeback to summary
public void setUnixMode(final int mode)

Sets Unix permissions in a way that is understood by Info-Zip's unzip command.

Parameters
mode:int

an int value

Since
Ant 1.5.2