2 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 package java.util.zip;
28 import java.util.Date;
31 * This class is used to represent a ZIP file entry.
33 * @author David Connelly
36 class ZipEntry implements ZipConstants, Cloneable {
37 String name; // entry name
38 long time = -1; // modification time (in DOS time)
39 long crc = -1; // crc-32 of entry data
40 long size = -1; // uncompressed size of entry data
41 long csize = -1; // compressed size of entry data
42 int method = -1; // compression method
43 int flag = 0; // general purpose flag
44 byte[] extra; // optional extra field data for entry
45 String comment; // optional comment string for entry
48 * Compression method for uncompressed entries.
50 public static final int STORED = 0;
53 * Compression method for compressed (deflated) entries.
55 public static final int DEFLATED = 8;
58 * Creates a new zip entry with the specified name.
60 * @param name the entry name
61 * @exception NullPointerException if the entry name is null
62 * @exception IllegalArgumentException if the entry name is longer than
65 public ZipEntry(String name) {
67 throw new NullPointerException();
69 if (name.length() > 0xFFFF) {
70 throw new IllegalArgumentException("entry name too long");
76 * Creates a new zip entry with fields taken from the specified
78 * @param e a zip Entry object
80 public ZipEntry(ZipEntry e) {
93 * Creates a new un-initialized zip entry
98 * Returns the name of the entry.
99 * @return the name of the entry
101 public String getName() {
106 * Sets the modification time of the entry.
107 * @param time the entry modification time in number of milliseconds
111 public void setTime(long time) {
112 this.time = javaToDosTime(time);
116 * Returns the modification time of the entry, or -1 if not specified.
117 * @return the modification time of the entry, or -1 if not specified
118 * @see #setTime(long)
120 public long getTime() {
121 return time != -1 ? dosToJavaTime(time) : -1;
125 * Sets the uncompressed size of the entry data.
126 * @param size the uncompressed size in bytes
127 * @exception IllegalArgumentException if the specified size is less
128 * than 0, is greater than 0xFFFFFFFF when
129 * <a href="package-summary.html#zip64">ZIP64 format</a> is not supported,
130 * or is less than 0 when ZIP64 is supported
133 public void setSize(long size) {
135 throw new IllegalArgumentException("invalid entry size");
141 * Returns the uncompressed size of the entry data, or -1 if not known.
142 * @return the uncompressed size of the entry data, or -1 if not known
143 * @see #setSize(long)
145 public long getSize() {
150 * Returns the size of the compressed entry data, or -1 if not known.
151 * In the case of a stored entry, the compressed size will be the same
152 * as the uncompressed size of the entry.
153 * @return the size of the compressed entry data, or -1 if not known
154 * @see #setCompressedSize(long)
156 public long getCompressedSize() {
161 * Sets the size of the compressed entry data.
162 * @param csize the compressed size to set to
163 * @see #getCompressedSize()
165 public void setCompressedSize(long csize) {
170 * Sets the CRC-32 checksum of the uncompressed entry data.
171 * @param crc the CRC-32 value
172 * @exception IllegalArgumentException if the specified CRC-32 value is
173 * less than 0 or greater than 0xFFFFFFFF
176 public void setCrc(long crc) {
177 if (crc < 0 || crc > 0xFFFFFFFFL) {
178 throw new IllegalArgumentException("invalid entry crc-32");
184 * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
186 * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
190 public long getCrc() {
195 * Sets the compression method for the entry.
196 * @param method the compression method, either STORED or DEFLATED
197 * @exception IllegalArgumentException if the specified compression
201 public void setMethod(int method) {
202 if (method != STORED && method != DEFLATED) {
203 throw new IllegalArgumentException("invalid compression method");
205 this.method = method;
209 * Returns the compression method of the entry, or -1 if not specified.
210 * @return the compression method of the entry, or -1 if not specified
211 * @see #setMethod(int)
213 public int getMethod() {
218 * Sets the optional extra field data for the entry.
219 * @param extra the extra field data bytes
220 * @exception IllegalArgumentException if the length of the specified
221 * extra field data is greater than 0xFFFF bytes
224 public void setExtra(byte[] extra) {
225 if (extra != null && extra.length > 0xFFFF) {
226 throw new IllegalArgumentException("invalid extra field length");
232 * Returns the extra field data for the entry, or null if none.
233 * @return the extra field data for the entry, or null if none
234 * @see #setExtra(byte[])
236 public byte[] getExtra() {
241 * Sets the optional comment string for the entry.
243 * <p>ZIP entry comments have maximum length of 0xffff. If the length of the
244 * specified comment string is greater than 0xFFFF bytes after encoding, only
245 * the first 0xFFFF bytes are output to the ZIP file entry.
247 * @param comment the comment string
251 public void setComment(String comment) {
252 this.comment = comment;
256 * Returns the comment string for the entry, or null if none.
257 * @return the comment string for the entry, or null if none
258 * @see #setComment(String)
260 public String getComment() {
265 * Returns true if this is a directory entry. A directory entry is
266 * defined to be one whose name ends with a '/'.
267 * @return true if this is a directory entry
269 public boolean isDirectory() {
270 return name.endsWith("/");
274 * Returns a string representation of the ZIP entry.
276 public String toString() {
281 * Converts DOS time to Java time (number of milliseconds since epoch).
283 private static long dosToJavaTime(long dtime) {
284 Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
285 (int)(((dtime >> 21) & 0x0f) - 1),
286 (int)((dtime >> 16) & 0x1f),
287 (int)((dtime >> 11) & 0x1f),
288 (int)((dtime >> 5) & 0x3f),
289 (int)((dtime << 1) & 0x3e));
294 * Converts Java time to DOS time.
296 private static long javaToDosTime(long time) {
297 Date d = new Date(time);
298 int year = d.getYear() + 1900;
300 return (1 << 21) | (1 << 16);
302 return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
303 d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
308 * Returns the hash code value for this entry.
310 public int hashCode() {
311 return name.hashCode();
315 * Returns a copy of this entry.
317 public Object clone() {
319 ZipEntry e = (ZipEntry)super.clone();
320 e.extra = (extra == null) ? null : extra.clone();
322 } catch (CloneNotSupportedException e) {
323 // This should never happen, since we are Cloneable
324 throw new InternalError();