author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8552 | f36470084574 |
child 10419 | 12c063b39232 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8552
diff
changeset
|
2 |
* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
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). |
|
16 |
* |
|
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. |
|
20 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
27 |
||
28 |
import java.util.Date; |
|
29 |
||
30 |
/** |
|
31 |
* This class is used to represent a ZIP file entry. |
|
32 |
* |
|
33 |
* @author David Connelly |
|
34 |
*/ |
|
35 |
public |
|
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 |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
43 |
int flag = 0; // general purpose flag |
2 | 44 |
byte[] extra; // optional extra field data for entry |
45 |
String comment; // optional comment string for entry |
|
46 |
||
47 |
/** |
|
48 |
* Compression method for uncompressed entries. |
|
49 |
*/ |
|
50 |
public static final int STORED = 0; |
|
51 |
||
52 |
/** |
|
53 |
* Compression method for compressed (deflated) entries. |
|
54 |
*/ |
|
55 |
public static final int DEFLATED = 8; |
|
56 |
||
57 |
/** |
|
58 |
* Creates a new zip entry with the specified name. |
|
59 |
* |
|
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 |
|
63 |
* 0xFFFF bytes |
|
64 |
*/ |
|
65 |
public ZipEntry(String name) { |
|
66 |
if (name == null) { |
|
67 |
throw new NullPointerException(); |
|
68 |
} |
|
69 |
if (name.length() > 0xFFFF) { |
|
70 |
throw new IllegalArgumentException("entry name too long"); |
|
71 |
} |
|
72 |
this.name = name; |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Creates a new zip entry with fields taken from the specified |
|
77 |
* zip entry. |
|
78 |
* @param e a zip Entry object |
|
79 |
*/ |
|
80 |
public ZipEntry(ZipEntry e) { |
|
81 |
name = e.name; |
|
82 |
time = e.time; |
|
83 |
crc = e.crc; |
|
84 |
size = e.size; |
|
85 |
csize = e.csize; |
|
86 |
method = e.method; |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
87 |
flag = e.flag; |
2 | 88 |
extra = e.extra; |
89 |
comment = e.comment; |
|
90 |
} |
|
91 |
||
92 |
/* |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
93 |
* Creates a new un-initialized zip entry |
2 | 94 |
*/ |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
95 |
ZipEntry() {} |
2 | 96 |
|
97 |
/** |
|
98 |
* Returns the name of the entry. |
|
99 |
* @return the name of the entry |
|
100 |
*/ |
|
101 |
public String getName() { |
|
102 |
return name; |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Sets the modification time of the entry. |
|
107 |
* @param time the entry modification time in number of milliseconds |
|
108 |
* since the epoch |
|
109 |
* @see #getTime() |
|
110 |
*/ |
|
111 |
public void setTime(long time) { |
|
8197 | 112 |
this.time = javaToDosTime(time); |
2 | 113 |
} |
114 |
||
115 |
/** |
|
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) |
|
119 |
*/ |
|
120 |
public long getTime() { |
|
121 |
return time != -1 ? dosToJavaTime(time) : -1; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
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 |
|
2438 | 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 |
|
2 | 131 |
* @see #getSize() |
132 |
*/ |
|
133 |
public void setSize(long size) { |
|
2438 | 134 |
if (size < 0) { |
2 | 135 |
throw new IllegalArgumentException("invalid entry size"); |
136 |
} |
|
137 |
this.size = size; |
|
138 |
} |
|
139 |
||
140 |
/** |
|
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) |
|
144 |
*/ |
|
145 |
public long getSize() { |
|
146 |
return size; |
|
147 |
} |
|
148 |
||
149 |
/** |
|
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) |
|
155 |
*/ |
|
156 |
public long getCompressedSize() { |
|
157 |
return csize; |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Sets the size of the compressed entry data. |
|
162 |
* @param csize the compressed size to set to |
|
163 |
* @see #getCompressedSize() |
|
164 |
*/ |
|
165 |
public void setCompressedSize(long csize) { |
|
166 |
this.csize = csize; |
|
167 |
} |
|
168 |
||
169 |
/** |
|
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 |
|
174 |
* @see #getCrc() |
|
175 |
*/ |
|
176 |
public void setCrc(long crc) { |
|
177 |
if (crc < 0 || crc > 0xFFFFFFFFL) { |
|
178 |
throw new IllegalArgumentException("invalid entry crc-32"); |
|
179 |
} |
|
180 |
this.crc = crc; |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Returns the CRC-32 checksum of the uncompressed entry data, or -1 if |
|
185 |
* not known. |
|
186 |
* @return the CRC-32 checksum of the uncompressed entry data, or -1 if |
|
187 |
* not known |
|
188 |
* @see #setCrc(long) |
|
189 |
*/ |
|
190 |
public long getCrc() { |
|
191 |
return crc; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
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 |
|
198 |
* method is invalid |
|
199 |
* @see #getMethod() |
|
200 |
*/ |
|
201 |
public void setMethod(int method) { |
|
202 |
if (method != STORED && method != DEFLATED) { |
|
203 |
throw new IllegalArgumentException("invalid compression method"); |
|
204 |
} |
|
205 |
this.method = method; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
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) |
|
212 |
*/ |
|
213 |
public int getMethod() { |
|
214 |
return method; |
|
215 |
} |
|
216 |
||
217 |
/** |
|
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 |
|
222 |
* @see #getExtra() |
|
223 |
*/ |
|
224 |
public void setExtra(byte[] extra) { |
|
225 |
if (extra != null && extra.length > 0xFFFF) { |
|
226 |
throw new IllegalArgumentException("invalid extra field length"); |
|
227 |
} |
|
228 |
this.extra = extra; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
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[]) |
|
235 |
*/ |
|
236 |
public byte[] getExtra() { |
|
237 |
return extra; |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Sets the optional comment string for the entry. |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
242 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
243 |
* <p>ZIP entry comments have maximum length of 0xffff. If the length of the |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
244 |
* specified comment string is greater than 0xFFFF bytes after encoding, only |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
245 |
* the first 0xFFFF bytes are output to the ZIP file entry. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
246 |
* |
2 | 247 |
* @param comment the comment string |
3844
74c6b6e2db6c
6881337: ZipEntry.setComment() was accidentally changed back to old spec/impl in jdk7-b64
sherman
parents:
3111
diff
changeset
|
248 |
* |
2 | 249 |
* @see #getComment() |
250 |
*/ |
|
251 |
public void setComment(String comment) { |
|
252 |
this.comment = comment; |
|
253 |
} |
|
254 |
||
255 |
/** |
|
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) |
|
259 |
*/ |
|
260 |
public String getComment() { |
|
261 |
return comment; |
|
262 |
} |
|
263 |
||
264 |
/** |
|
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 |
|
268 |
*/ |
|
269 |
public boolean isDirectory() { |
|
270 |
return name.endsWith("/"); |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Returns a string representation of the ZIP entry. |
|
275 |
*/ |
|
276 |
public String toString() { |
|
277 |
return getName(); |
|
278 |
} |
|
279 |
||
280 |
/* |
|
281 |
* Converts DOS time to Java time (number of milliseconds since epoch). |
|
282 |
*/ |
|
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)); |
|
290 |
return d.getTime(); |
|
291 |
} |
|
292 |
||
293 |
/* |
|
294 |
* Converts Java time to DOS time. |
|
295 |
*/ |
|
296 |
private static long javaToDosTime(long time) { |
|
297 |
Date d = new Date(time); |
|
298 |
int year = d.getYear() + 1900; |
|
299 |
if (year < 1980) { |
|
300 |
return (1 << 21) | (1 << 16); |
|
301 |
} |
|
302 |
return (year - 1980) << 25 | (d.getMonth() + 1) << 21 | |
|
303 |
d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 | |
|
304 |
d.getSeconds() >> 1; |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Returns the hash code value for this entry. |
|
309 |
*/ |
|
310 |
public int hashCode() { |
|
311 |
return name.hashCode(); |
|
312 |
} |
|
313 |
||
314 |
/** |
|
315 |
* Returns a copy of this entry. |
|
316 |
*/ |
|
317 |
public Object clone() { |
|
318 |
try { |
|
319 |
ZipEntry e = (ZipEntry)super.clone(); |
|
320 |
e.extra = (extra == null) ? null : extra.clone(); |
|
321 |
return e; |
|
322 |
} catch (CloneNotSupportedException e) { |
|
323 |
// This should never happen, since we are Cloneable |
|
324 |
throw new InternalError(); |
|
325 |
} |
|
326 |
} |
|
327 |
} |