jaroslav@609: /* jaroslav@609: * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@609: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@609: * jaroslav@609: * This code is free software; you can redistribute it and/or modify it jaroslav@609: * under the terms of the GNU General Public License version 2 only, as jaroslav@609: * published by the Free Software Foundation. Oracle designates this jaroslav@609: * particular file as subject to the "Classpath" exception as provided jaroslav@609: * by Oracle in the LICENSE file that accompanied this code. jaroslav@609: * jaroslav@609: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@609: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@609: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@609: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@609: * accompanied this code). jaroslav@609: * jaroslav@609: * You should have received a copy of the GNU General Public License version jaroslav@609: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@609: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@609: * jaroslav@609: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@609: * or visit www.oracle.com if you need additional information or have any jaroslav@609: * questions. jaroslav@609: */ jaroslav@609: jaroslav@609: package java.util.zip; jaroslav@609: jaroslav@609: /** jaroslav@609: * This class is used to represent a ZIP file entry. jaroslav@609: * jaroslav@609: * @author David Connelly jaroslav@609: */ jaroslav@609: public jaroslav@609: class ZipEntry implements ZipConstants, Cloneable { jaroslav@609: String name; // entry name jaroslav@609: long time = -1; // modification time (in DOS time) jaroslav@609: long crc = -1; // crc-32 of entry data jaroslav@609: long size = -1; // uncompressed size of entry data jaroslav@609: long csize = -1; // compressed size of entry data jaroslav@609: int method = -1; // compression method jaroslav@609: int flag = 0; // general purpose flag jaroslav@609: byte[] extra; // optional extra field data for entry jaroslav@609: String comment; // optional comment string for entry jaroslav@609: jaroslav@609: /** jaroslav@609: * Compression method for uncompressed entries. jaroslav@609: */ jaroslav@609: public static final int STORED = 0; jaroslav@609: jaroslav@609: /** jaroslav@609: * Compression method for compressed (deflated) entries. jaroslav@609: */ jaroslav@609: public static final int DEFLATED = 8; jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new zip entry with the specified name. jaroslav@609: * jaroslav@609: * @param name the entry name jaroslav@609: * @exception NullPointerException if the entry name is null jaroslav@609: * @exception IllegalArgumentException if the entry name is longer than jaroslav@609: * 0xFFFF bytes jaroslav@609: */ jaroslav@609: public ZipEntry(String name) { jaroslav@609: if (name == null) { jaroslav@609: throw new NullPointerException(); jaroslav@609: } jaroslav@609: if (name.length() > 0xFFFF) { jaroslav@609: throw new IllegalArgumentException("entry name too long"); jaroslav@609: } jaroslav@609: this.name = name; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new zip entry with fields taken from the specified jaroslav@609: * zip entry. jaroslav@609: * @param e a zip Entry object jaroslav@609: */ jaroslav@609: public ZipEntry(ZipEntry e) { jaroslav@609: name = e.name; jaroslav@609: time = e.time; jaroslav@609: crc = e.crc; jaroslav@609: size = e.size; jaroslav@609: csize = e.csize; jaroslav@609: method = e.method; jaroslav@609: flag = e.flag; jaroslav@609: extra = e.extra; jaroslav@609: comment = e.comment; jaroslav@609: } jaroslav@609: jaroslav@609: /* jaroslav@609: * Creates a new un-initialized zip entry jaroslav@609: */ jaroslav@609: ZipEntry() {} jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the name of the entry. jaroslav@609: * @return the name of the entry jaroslav@609: */ jaroslav@609: public String getName() { jaroslav@609: return name; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Sets the modification time of the entry. jaroslav@609: * @param time the entry modification time in number of milliseconds jaroslav@609: * since the epoch jaroslav@609: * @see #getTime() jaroslav@609: */ jaroslav@609: public void setTime(long time) { jaroslav@609: this.time = javaToDosTime(time); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the modification time of the entry, or -1 if not specified. jaroslav@609: * @return the modification time of the entry, or -1 if not specified jaroslav@609: * @see #setTime(long) jaroslav@609: */ jaroslav@609: public long getTime() { jaroslav@609: return time != -1 ? dosToJavaTime(time) : -1; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Sets the uncompressed size of the entry data. jaroslav@609: * @param size the uncompressed size in bytes jaroslav@609: * @exception IllegalArgumentException if the specified size is less jaroslav@609: * than 0, is greater than 0xFFFFFFFF when jaroslav@609: * ZIP64 format is not supported, jaroslav@609: * or is less than 0 when ZIP64 is supported jaroslav@609: * @see #getSize() jaroslav@609: */ jaroslav@609: public void setSize(long size) { jaroslav@609: if (size < 0) { jaroslav@609: throw new IllegalArgumentException("invalid entry size"); jaroslav@609: } jaroslav@609: this.size = size; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the uncompressed size of the entry data, or -1 if not known. jaroslav@609: * @return the uncompressed size of the entry data, or -1 if not known jaroslav@609: * @see #setSize(long) jaroslav@609: */ jaroslav@609: public long getSize() { jaroslav@609: return size; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the size of the compressed entry data, or -1 if not known. jaroslav@609: * In the case of a stored entry, the compressed size will be the same jaroslav@609: * as the uncompressed size of the entry. jaroslav@609: * @return the size of the compressed entry data, or -1 if not known jaroslav@609: * @see #setCompressedSize(long) jaroslav@609: */ jaroslav@609: public long getCompressedSize() { jaroslav@609: return csize; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Sets the size of the compressed entry data. jaroslav@609: * @param csize the compressed size to set to jaroslav@609: * @see #getCompressedSize() jaroslav@609: */ jaroslav@609: public void setCompressedSize(long csize) { jaroslav@609: this.csize = csize; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Sets the CRC-32 checksum of the uncompressed entry data. jaroslav@609: * @param crc the CRC-32 value jaroslav@609: * @exception IllegalArgumentException if the specified CRC-32 value is jaroslav@609: * less than 0 or greater than 0xFFFFFFFF jaroslav@609: * @see #getCrc() jaroslav@609: */ jaroslav@609: public void setCrc(long crc) { jaroslav@609: if (crc < 0 || crc > 0xFFFFFFFFL) { jaroslav@609: throw new IllegalArgumentException("invalid entry crc-32"); jaroslav@609: } jaroslav@609: this.crc = crc; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if jaroslav@609: * not known. jaroslav@609: * @return the CRC-32 checksum of the uncompressed entry data, or -1 if jaroslav@609: * not known jaroslav@609: * @see #setCrc(long) jaroslav@609: */ jaroslav@609: public long getCrc() { jaroslav@609: return crc; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Sets the compression method for the entry. jaroslav@609: * @param method the compression method, either STORED or DEFLATED jaroslav@609: * @exception IllegalArgumentException if the specified compression jaroslav@609: * method is invalid jaroslav@609: * @see #getMethod() jaroslav@609: */ jaroslav@609: public void setMethod(int method) { jaroslav@609: if (method != STORED && method != DEFLATED) { jaroslav@609: throw new IllegalArgumentException("invalid compression method"); jaroslav@609: } jaroslav@609: this.method = method; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the compression method of the entry, or -1 if not specified. jaroslav@609: * @return the compression method of the entry, or -1 if not specified jaroslav@609: * @see #setMethod(int) jaroslav@609: */ jaroslav@609: public int getMethod() { jaroslav@609: return method; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Sets the optional extra field data for the entry. jaroslav@609: * @param extra the extra field data bytes jaroslav@609: * @exception IllegalArgumentException if the length of the specified jaroslav@609: * extra field data is greater than 0xFFFF bytes jaroslav@609: * @see #getExtra() jaroslav@609: */ jaroslav@609: public void setExtra(byte[] extra) { jaroslav@609: if (extra != null && extra.length > 0xFFFF) { jaroslav@609: throw new IllegalArgumentException("invalid extra field length"); jaroslav@609: } jaroslav@609: this.extra = extra; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the extra field data for the entry, or null if none. jaroslav@609: * @return the extra field data for the entry, or null if none jaroslav@609: * @see #setExtra(byte[]) jaroslav@609: */ jaroslav@609: public byte[] getExtra() { jaroslav@609: return extra; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Sets the optional comment string for the entry. jaroslav@609: * jaroslav@609: *

ZIP entry comments have maximum length of 0xffff. If the length of the jaroslav@609: * specified comment string is greater than 0xFFFF bytes after encoding, only jaroslav@609: * the first 0xFFFF bytes are output to the ZIP file entry. jaroslav@609: * jaroslav@609: * @param comment the comment string jaroslav@609: * jaroslav@609: * @see #getComment() jaroslav@609: */ jaroslav@609: public void setComment(String comment) { jaroslav@609: this.comment = comment; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the comment string for the entry, or null if none. jaroslav@609: * @return the comment string for the entry, or null if none jaroslav@609: * @see #setComment(String) jaroslav@609: */ jaroslav@609: public String getComment() { jaroslav@609: return comment; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns true if this is a directory entry. A directory entry is jaroslav@609: * defined to be one whose name ends with a '/'. jaroslav@609: * @return true if this is a directory entry jaroslav@609: */ jaroslav@609: public boolean isDirectory() { jaroslav@609: return name.endsWith("/"); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns a string representation of the ZIP entry. jaroslav@609: */ jaroslav@609: public String toString() { jaroslav@609: return getName(); jaroslav@609: } jaroslav@609: jaroslav@609: /* jaroslav@609: * Converts DOS time to Java time (number of milliseconds since epoch). jaroslav@609: */ jaroslav@609: private static long dosToJavaTime(long dtime) { jaroslav@611: return dtime; jaroslav@611: /* XXX: jaroslav@609: Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80), jaroslav@609: (int)(((dtime >> 21) & 0x0f) - 1), jaroslav@609: (int)((dtime >> 16) & 0x1f), jaroslav@609: (int)((dtime >> 11) & 0x1f), jaroslav@609: (int)((dtime >> 5) & 0x3f), jaroslav@609: (int)((dtime << 1) & 0x3e)); jaroslav@609: return d.getTime(); jaroslav@611: */ jaroslav@609: } jaroslav@609: jaroslav@609: /* jaroslav@609: * Converts Java time to DOS time. jaroslav@609: */ jaroslav@609: private static long javaToDosTime(long time) { jaroslav@611: return time; jaroslav@611: /* XXX: jaroslav@609: Date d = new Date(time); jaroslav@609: int year = d.getYear() + 1900; jaroslav@609: if (year < 1980) { jaroslav@609: return (1 << 21) | (1 << 16); jaroslav@609: } jaroslav@609: return (year - 1980) << 25 | (d.getMonth() + 1) << 21 | jaroslav@609: d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 | jaroslav@609: d.getSeconds() >> 1; jaroslav@611: */ jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns the hash code value for this entry. jaroslav@609: */ jaroslav@609: public int hashCode() { jaroslav@609: return name.hashCode(); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns a copy of this entry. jaroslav@609: */ jaroslav@609: public Object clone() { jaroslav@609: try { jaroslav@609: ZipEntry e = (ZipEntry)super.clone(); jaroslav@609: e.extra = (extra == null) ? null : extra.clone(); jaroslav@609: return e; jaroslav@609: } catch (CloneNotSupportedException e) { jaroslav@609: // This should never happen, since we are Cloneable jaroslav@611: throw new IllegalStateException(); jaroslav@609: } jaroslav@609: } jaroslav@609: }