0N/A/*
3909N/A * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.zip;
0N/A
0N/Aimport java.util.Date;
0N/A
0N/A/**
0N/A * This class is used to represent a ZIP file entry.
0N/A *
0N/A * @author David Connelly
0N/A */
0N/Apublic
0N/Aclass ZipEntry implements ZipConstants, Cloneable {
0N/A String name; // entry name
0N/A long time = -1; // modification time (in DOS time)
0N/A long crc = -1; // crc-32 of entry data
0N/A long size = -1; // uncompressed size of entry data
0N/A long csize = -1; // compressed size of entry data
0N/A int method = -1; // compression method
1107N/A int flag = 0; // general purpose flag
0N/A byte[] extra; // optional extra field data for entry
0N/A String comment; // optional comment string for entry
0N/A
0N/A /**
0N/A * Compression method for uncompressed entries.
0N/A */
0N/A public static final int STORED = 0;
0N/A
0N/A /**
0N/A * Compression method for compressed (deflated) entries.
0N/A */
0N/A public static final int DEFLATED = 8;
0N/A
0N/A /**
0N/A * Creates a new zip entry with the specified name.
0N/A *
0N/A * @param name the entry name
0N/A * @exception NullPointerException if the entry name is null
0N/A * @exception IllegalArgumentException if the entry name is longer than
0N/A * 0xFFFF bytes
0N/A */
0N/A public ZipEntry(String name) {
0N/A if (name == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (name.length() > 0xFFFF) {
0N/A throw new IllegalArgumentException("entry name too long");
0N/A }
0N/A this.name = name;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new zip entry with fields taken from the specified
0N/A * zip entry.
0N/A * @param e a zip Entry object
0N/A */
0N/A public ZipEntry(ZipEntry e) {
0N/A name = e.name;
0N/A time = e.time;
0N/A crc = e.crc;
0N/A size = e.size;
0N/A csize = e.csize;
0N/A method = e.method;
1107N/A flag = e.flag;
0N/A extra = e.extra;
0N/A comment = e.comment;
0N/A }
0N/A
0N/A /*
1107N/A * Creates a new un-initialized zip entry
0N/A */
1107N/A ZipEntry() {}
0N/A
0N/A /**
0N/A * Returns the name of the entry.
0N/A * @return the name of the entry
0N/A */
0N/A public String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Sets the modification time of the entry.
0N/A * @param time the entry modification time in number of milliseconds
0N/A * since the epoch
0N/A * @see #getTime()
0N/A */
0N/A public void setTime(long time) {
3510N/A this.time = javaToDosTime(time);
0N/A }
0N/A
0N/A /**
0N/A * Returns the modification time of the entry, or -1 if not specified.
0N/A * @return the modification time of the entry, or -1 if not specified
0N/A * @see #setTime(long)
0N/A */
0N/A public long getTime() {
0N/A return time != -1 ? dosToJavaTime(time) : -1;
0N/A }
0N/A
0N/A /**
0N/A * Sets the uncompressed size of the entry data.
0N/A * @param size the uncompressed size in bytes
0N/A * @exception IllegalArgumentException if the specified size is less
1032N/A * than 0, is greater than 0xFFFFFFFF when
1032N/A * <a href="package-summary.html#zip64">ZIP64 format</a> is not supported,
1032N/A * or is less than 0 when ZIP64 is supported
0N/A * @see #getSize()
0N/A */
0N/A public void setSize(long size) {
1032N/A if (size < 0) {
0N/A throw new IllegalArgumentException("invalid entry size");
0N/A }
0N/A this.size = size;
0N/A }
0N/A
0N/A /**
0N/A * Returns the uncompressed size of the entry data, or -1 if not known.
0N/A * @return the uncompressed size of the entry data, or -1 if not known
0N/A * @see #setSize(long)
0N/A */
0N/A public long getSize() {
0N/A return size;
0N/A }
0N/A
0N/A /**
0N/A * Returns the size of the compressed entry data, or -1 if not known.
0N/A * In the case of a stored entry, the compressed size will be the same
0N/A * as the uncompressed size of the entry.
0N/A * @return the size of the compressed entry data, or -1 if not known
0N/A * @see #setCompressedSize(long)
0N/A */
0N/A public long getCompressedSize() {
0N/A return csize;
0N/A }
0N/A
0N/A /**
0N/A * Sets the size of the compressed entry data.
0N/A * @param csize the compressed size to set to
0N/A * @see #getCompressedSize()
0N/A */
0N/A public void setCompressedSize(long csize) {
0N/A this.csize = csize;
0N/A }
0N/A
0N/A /**
0N/A * Sets the CRC-32 checksum of the uncompressed entry data.
0N/A * @param crc the CRC-32 value
0N/A * @exception IllegalArgumentException if the specified CRC-32 value is
0N/A * less than 0 or greater than 0xFFFFFFFF
0N/A * @see #getCrc()
0N/A */
0N/A public void setCrc(long crc) {
0N/A if (crc < 0 || crc > 0xFFFFFFFFL) {
0N/A throw new IllegalArgumentException("invalid entry crc-32");
0N/A }
0N/A this.crc = crc;
0N/A }
0N/A
0N/A /**
0N/A * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
0N/A * not known.
0N/A * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
0N/A * not known
0N/A * @see #setCrc(long)
0N/A */
0N/A public long getCrc() {
0N/A return crc;
0N/A }
0N/A
0N/A /**
0N/A * Sets the compression method for the entry.
0N/A * @param method the compression method, either STORED or DEFLATED
0N/A * @exception IllegalArgumentException if the specified compression
0N/A * method is invalid
0N/A * @see #getMethod()
0N/A */
0N/A public void setMethod(int method) {
0N/A if (method != STORED && method != DEFLATED) {
0N/A throw new IllegalArgumentException("invalid compression method");
0N/A }
0N/A this.method = method;
0N/A }
0N/A
0N/A /**
0N/A * Returns the compression method of the entry, or -1 if not specified.
0N/A * @return the compression method of the entry, or -1 if not specified
0N/A * @see #setMethod(int)
0N/A */
0N/A public int getMethod() {
0N/A return method;
0N/A }
0N/A
0N/A /**
0N/A * Sets the optional extra field data for the entry.
0N/A * @param extra the extra field data bytes
0N/A * @exception IllegalArgumentException if the length of the specified
0N/A * extra field data is greater than 0xFFFF bytes
0N/A * @see #getExtra()
0N/A */
0N/A public void setExtra(byte[] extra) {
0N/A if (extra != null && extra.length > 0xFFFF) {
0N/A throw new IllegalArgumentException("invalid extra field length");
0N/A }
0N/A this.extra = extra;
0N/A }
0N/A
0N/A /**
0N/A * Returns the extra field data for the entry, or null if none.
0N/A * @return the extra field data for the entry, or null if none
0N/A * @see #setExtra(byte[])
0N/A */
0N/A public byte[] getExtra() {
0N/A return extra;
0N/A }
0N/A
0N/A /**
0N/A * Sets the optional comment string for the entry.
1107N/A *
1107N/A * <p>ZIP entry comments have maximum length of 0xffff. If the length of the
1107N/A * specified comment string is greater than 0xFFFF bytes after encoding, only
1107N/A * the first 0xFFFF bytes are output to the ZIP file entry.
1107N/A *
0N/A * @param comment the comment string
1655N/A *
0N/A * @see #getComment()
0N/A */
0N/A public void setComment(String comment) {
0N/A this.comment = comment;
0N/A }
0N/A
0N/A /**
0N/A * Returns the comment string for the entry, or null if none.
0N/A * @return the comment string for the entry, or null if none
0N/A * @see #setComment(String)
0N/A */
0N/A public String getComment() {
0N/A return comment;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this is a directory entry. A directory entry is
0N/A * defined to be one whose name ends with a '/'.
0N/A * @return true if this is a directory entry
0N/A */
0N/A public boolean isDirectory() {
0N/A return name.endsWith("/");
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of the ZIP entry.
0N/A */
0N/A public String toString() {
0N/A return getName();
0N/A }
0N/A
0N/A /*
0N/A * Converts DOS time to Java time (number of milliseconds since epoch).
0N/A */
0N/A private static long dosToJavaTime(long dtime) {
0N/A Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
0N/A (int)(((dtime >> 21) & 0x0f) - 1),
0N/A (int)((dtime >> 16) & 0x1f),
0N/A (int)((dtime >> 11) & 0x1f),
0N/A (int)((dtime >> 5) & 0x3f),
0N/A (int)((dtime << 1) & 0x3e));
0N/A return d.getTime();
0N/A }
0N/A
0N/A /*
0N/A * Converts Java time to DOS time.
0N/A */
0N/A private static long javaToDosTime(long time) {
0N/A Date d = new Date(time);
0N/A int year = d.getYear() + 1900;
0N/A if (year < 1980) {
0N/A return (1 << 21) | (1 << 16);
0N/A }
0N/A return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
0N/A d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
0N/A d.getSeconds() >> 1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this entry.
0N/A */
0N/A public int hashCode() {
0N/A return name.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of this entry.
0N/A */
0N/A public Object clone() {
0N/A try {
0N/A ZipEntry e = (ZipEntry)super.clone();
0N/A e.extra = (extra == null) ? null : extra.clone();
0N/A return e;
0N/A } catch (CloneNotSupportedException e) {
0N/A // This should never happen, since we are Cloneable
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A}