FileDocCategorySizeDatePackage
ZipEntry.javaAPI DocApache Ant 1.7010870Wed Dec 13 06:16:18 GMT 2006org.apache.tools.zip

ZipEntry

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

Fields Summary
private static final int
PLATFORM_UNIX
private static final int
PLATFORM_FAT
private int
internalAttributes
private int
platform
private long
externalAttributes
private Vector
extraFields
private String
name
Constructors Summary
public ZipEntry(String name)
Creates a new zip entry with the specified name.

param
name the name of the entry
since
1.1


                           
       
        super(name);
    
public ZipEntry(ZipEntry entry)
Creates a new zip entry with fields taken from the specified zip entry.

param
entry the entry to get fields from
since
1.1
throws
ZipException on error

        super(entry);
        byte[] extra = entry.getExtra();
        if (extra != null) {
            setExtraFields(ExtraFieldUtils.parse(extra));
        } else {
            // initializes extra data to an empty byte array
            setExtra();
        }
    
public ZipEntry(ZipEntry entry)
Creates a new zip entry with fields taken from the specified zip entry.

param
entry the entry to get fields from
throws
ZipException on error
since
1.1

        this((java.util.zip.ZipEntry) entry);
        setInternalAttributes(entry.getInternalAttributes());
        setExternalAttributes(entry.getExternalAttributes());
        setExtraFields(entry.getExtraFields());
    
protected ZipEntry()

since
1.9

        super("");
    
Methods Summary
public voidaddExtraField(ZipExtraField ze)
Adds an extra fields - replacing an already present extra field of the same type.

param
ze an extra field
since
1.1

        if (extraFields == null) {
            extraFields = new Vector();
        }
        ZipShort type = ze.getHeaderId();
        boolean done = false;
        for (int i = 0, fieldsSize = extraFields.size(); !done && i < fieldsSize; i++) {
            if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) {
                extraFields.setElementAt(ze, i);
                done = true;
            }
        }
        if (!done) {
            extraFields.addElement(ze);
        }
        setExtra();
    
public java.lang.Objectclone()
Overwrite clone.

return
a cloned copy of this ZipEntry
since
1.1

        ZipEntry e = (ZipEntry) super.clone();

        e.extraFields = extraFields != null ? (Vector) extraFields.clone() : null;
        e.setInternalAttributes(getInternalAttributes());
        e.setExternalAttributes(getExternalAttributes());
        e.setExtraFields(getExtraFields());
        return e;
    
public booleanequals(java.lang.Object o)
The equality method. In this case, the implementation returns 'this == o' which is basically the equals method of the Object class.

param
o the object to compare to
return
true if this object is the same as o
since
Ant 1.7

        return (this == o);
    
public byte[]getCentralDirectoryExtra()
Retrieves the extra data for the central directory.

return
the central directory extra data
since
1.1

        return ExtraFieldUtils.mergeCentralDirectoryData(getExtraFields());
    
public longgetExternalAttributes()
Retrieves the external file attributes.

return
the external file attributes
since
1.1

        return externalAttributes;
    
public ZipExtraField[]getExtraFields()
Retrieves extra fields.

return
an array of the extra fields
since
1.1

        if (extraFields == null) {
            return new ZipExtraField[0];
        }
        ZipExtraField[] result = new ZipExtraField[extraFields.size()];
        extraFields.copyInto(result);
        return result;
    
public intgetInternalAttributes()
Retrieves the internal file attributes.

return
the internal file attributes
since
1.1

        return internalAttributes;
    
public byte[]getLocalFileDataExtra()
Retrieves the extra data for the local file data.

return
the extra data for local file
since
1.1

        byte[] extra = getExtra();
        return extra != null ? extra : new byte[0];
    
public java.lang.StringgetName()
Get the name of the entry.

return
the entry name
since
1.9

        return name == null ? super.getName() : name;
    
public intgetPlatform()
Platform specification to put into the "version made by" part of the central file header.

return
0 (MS-DOS FAT) unless {@link #setUnixMode setUnixMode} has been called, in which case 3 (Unix) will be returned.
since
Ant 1.5.2

        return platform;
    
public intgetUnixMode()
Unix permission.

return
the unix permissions
since
Ant 1.6

        return (int) ((getExternalAttributes() >> 16) & 0xFFFF);
    
public inthashCode()
Get the hashCode of the entry. This uses the name as the hashcode.

return
a hashcode.
since
Ant 1.7

        // this method has severe consequences on performance. We cannot rely
        // on the super.hashCode() method since super.getName() always return
        // the empty string in the current implemention (there's no setter)
        // so it is basically draining the performance of a hashmap lookup
        return getName().hashCode();
    
public booleanisDirectory()
Is this entry a directory?

return
true if the entry is a directory
since
1.10

        return getName().endsWith("/");
    
public voidremoveExtraField(ZipShort type)
Remove an extra fields.

param
type the type of extra field to remove
since
1.1

        if (extraFields == null) {
            extraFields = new Vector();
        }
        boolean done = false;
        for (int i = 0, fieldsSize = extraFields.size(); !done && i < fieldsSize; i++) {
            if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) {
                extraFields.removeElementAt(i);
                done = true;
            }
        }
        if (!done) {
            throw new java.util.NoSuchElementException();
        }
        setExtra();
    
public voidsetComprSize(long size)
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.

param
size the size to use
deprecated
since 1.7. Use setCompressedSize directly.
since
1.2

        setCompressedSize(size);
    
public voidsetExternalAttributes(long value)
Sets the external file attributes.

param
value an long value
since
1.1

        externalAttributes = value;
    
public voidsetExtra(byte[] extra)
Throws an Exception if extra data cannot be parsed into extra fields.

param
extra an array of bytes to be parsed into extra fields
throws
RuntimeException if the bytes cannot be parsed
since
1.1
throws
RuntimeException on error

        try {
            setExtraFields(ExtraFieldUtils.parse(extra));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    
protected voidsetExtra()
Unfortunately {@link java.util.zip.ZipOutputStream 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

        super.setExtra(ExtraFieldUtils.mergeLocalFileDataData(getExtraFields()));
    
public voidsetExtraFields(ZipExtraField[] fields)
Replaces all currently attached extra fields with the new array.

param
fields an array of extra fields
since
1.1

        extraFields = new Vector();
        for (int i = 0; i < fields.length; i++) {
            extraFields.addElement(fields[i]);
        }
        setExtra();
    
public voidsetInternalAttributes(int value)
Sets the internal file attributes.

param
value an int value
since
1.1

        internalAttributes = value;
    
protected voidsetName(java.lang.String name)
Set the name of the entry.

param
name the name to use

        this.name = name;
    
protected voidsetPlatform(int platform)
Set the platform (UNIX or FAT).

param
platform an int value - 0 is FAT, 3 is UNIX
since
1.9

        this.platform = platform;
    
public voidsetUnixMode(int mode)
Sets Unix permissions in a way that is understood by Info-Zip's unzip command.

param
mode an int value
since
Ant 1.5.2

        setExternalAttributes((mode << 16)
                              // MS-DOS read-only attribute
                              | ((mode & 0200) == 0 ? 1 : 0)
                              // MS-DOS directory flag
                              | (isDirectory() ? 0x10 : 0));
        platform = PLATFORM_UNIX;