2
|
1 |
/*
|
2438
|
2 |
* Copyright 1995-2009 Sun Microsystems, Inc. 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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
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
|
|
43 |
byte[] extra; // optional extra field data for entry
|
|
44 |
String comment; // optional comment string for entry
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Compression method for uncompressed entries.
|
|
48 |
*/
|
|
49 |
public static final int STORED = 0;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Compression method for compressed (deflated) entries.
|
|
53 |
*/
|
|
54 |
public static final int DEFLATED = 8;
|
|
55 |
|
|
56 |
static {
|
|
57 |
/* Zip library is loaded from System.initializeSystemClass */
|
|
58 |
initIDs();
|
|
59 |
}
|
|
60 |
|
|
61 |
private static native void initIDs();
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Creates a new zip entry with the specified name.
|
|
65 |
*
|
|
66 |
* @param name the entry name
|
|
67 |
* @exception NullPointerException if the entry name is null
|
|
68 |
* @exception IllegalArgumentException if the entry name is longer than
|
|
69 |
* 0xFFFF bytes
|
|
70 |
*/
|
|
71 |
public ZipEntry(String name) {
|
|
72 |
if (name == null) {
|
|
73 |
throw new NullPointerException();
|
|
74 |
}
|
|
75 |
if (name.length() > 0xFFFF) {
|
|
76 |
throw new IllegalArgumentException("entry name too long");
|
|
77 |
}
|
|
78 |
this.name = name;
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Creates a new zip entry with fields taken from the specified
|
|
83 |
* zip entry.
|
|
84 |
* @param e a zip Entry object
|
|
85 |
*/
|
|
86 |
public ZipEntry(ZipEntry e) {
|
|
87 |
name = e.name;
|
|
88 |
time = e.time;
|
|
89 |
crc = e.crc;
|
|
90 |
size = e.size;
|
|
91 |
csize = e.csize;
|
|
92 |
method = e.method;
|
|
93 |
extra = e.extra;
|
|
94 |
comment = e.comment;
|
|
95 |
}
|
|
96 |
|
|
97 |
/*
|
|
98 |
* Creates a new zip entry for the given name with fields initialized
|
|
99 |
* from the specified jzentry data.
|
|
100 |
*/
|
|
101 |
ZipEntry(String name, long jzentry) {
|
|
102 |
this.name = name;
|
|
103 |
initFields(jzentry);
|
|
104 |
}
|
|
105 |
|
|
106 |
private native void initFields(long jzentry);
|
|
107 |
|
|
108 |
/*
|
|
109 |
* Creates a new zip entry with fields initialized from the specified
|
|
110 |
* jzentry data.
|
|
111 |
*/
|
|
112 |
ZipEntry(long jzentry) {
|
|
113 |
initFields(jzentry);
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Returns the name of the entry.
|
|
118 |
* @return the name of the entry
|
|
119 |
*/
|
|
120 |
public String getName() {
|
|
121 |
return name;
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Sets the modification time of the entry.
|
|
126 |
* @param time the entry modification time in number of milliseconds
|
|
127 |
* since the epoch
|
|
128 |
* @see #getTime()
|
|
129 |
*/
|
|
130 |
public void setTime(long time) {
|
|
131 |
this.time = javaToDosTime(time);
|
|
132 |
}
|
|
133 |
|
|
134 |
/**
|
|
135 |
* Returns the modification time of the entry, or -1 if not specified.
|
|
136 |
* @return the modification time of the entry, or -1 if not specified
|
|
137 |
* @see #setTime(long)
|
|
138 |
*/
|
|
139 |
public long getTime() {
|
|
140 |
return time != -1 ? dosToJavaTime(time) : -1;
|
|
141 |
}
|
|
142 |
|
|
143 |
/**
|
|
144 |
* Sets the uncompressed size of the entry data.
|
|
145 |
* @param size the uncompressed size in bytes
|
|
146 |
* @exception IllegalArgumentException if the specified size is less
|
2438
|
147 |
* than 0, is greater than 0xFFFFFFFF when
|
|
148 |
* <a href="package-summary.html#zip64">ZIP64 format</a> is not supported,
|
|
149 |
* or is less than 0 when ZIP64 is supported
|
2
|
150 |
* @see #getSize()
|
|
151 |
*/
|
|
152 |
public void setSize(long size) {
|
2438
|
153 |
if (size < 0) {
|
2
|
154 |
throw new IllegalArgumentException("invalid entry size");
|
|
155 |
}
|
|
156 |
this.size = size;
|
|
157 |
}
|
|
158 |
|
|
159 |
/**
|
|
160 |
* Returns the uncompressed size of the entry data, or -1 if not known.
|
|
161 |
* @return the uncompressed size of the entry data, or -1 if not known
|
|
162 |
* @see #setSize(long)
|
|
163 |
*/
|
|
164 |
public long getSize() {
|
|
165 |
return size;
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Returns the size of the compressed entry data, or -1 if not known.
|
|
170 |
* In the case of a stored entry, the compressed size will be the same
|
|
171 |
* as the uncompressed size of the entry.
|
|
172 |
* @return the size of the compressed entry data, or -1 if not known
|
|
173 |
* @see #setCompressedSize(long)
|
|
174 |
*/
|
|
175 |
public long getCompressedSize() {
|
|
176 |
return csize;
|
|
177 |
}
|
|
178 |
|
|
179 |
/**
|
|
180 |
* Sets the size of the compressed entry data.
|
|
181 |
* @param csize the compressed size to set to
|
|
182 |
* @see #getCompressedSize()
|
|
183 |
*/
|
|
184 |
public void setCompressedSize(long csize) {
|
|
185 |
this.csize = csize;
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Sets the CRC-32 checksum of the uncompressed entry data.
|
|
190 |
* @param crc the CRC-32 value
|
|
191 |
* @exception IllegalArgumentException if the specified CRC-32 value is
|
|
192 |
* less than 0 or greater than 0xFFFFFFFF
|
|
193 |
* @see #getCrc()
|
|
194 |
*/
|
|
195 |
public void setCrc(long crc) {
|
|
196 |
if (crc < 0 || crc > 0xFFFFFFFFL) {
|
|
197 |
throw new IllegalArgumentException("invalid entry crc-32");
|
|
198 |
}
|
|
199 |
this.crc = crc;
|
|
200 |
}
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
|
|
204 |
* not known.
|
|
205 |
* @return the CRC-32 checksum of the uncompressed entry data, or -1 if
|
|
206 |
* not known
|
|
207 |
* @see #setCrc(long)
|
|
208 |
*/
|
|
209 |
public long getCrc() {
|
|
210 |
return crc;
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* Sets the compression method for the entry.
|
|
215 |
* @param method the compression method, either STORED or DEFLATED
|
|
216 |
* @exception IllegalArgumentException if the specified compression
|
|
217 |
* method is invalid
|
|
218 |
* @see #getMethod()
|
|
219 |
*/
|
|
220 |
public void setMethod(int method) {
|
|
221 |
if (method != STORED && method != DEFLATED) {
|
|
222 |
throw new IllegalArgumentException("invalid compression method");
|
|
223 |
}
|
|
224 |
this.method = method;
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Returns the compression method of the entry, or -1 if not specified.
|
|
229 |
* @return the compression method of the entry, or -1 if not specified
|
|
230 |
* @see #setMethod(int)
|
|
231 |
*/
|
|
232 |
public int getMethod() {
|
|
233 |
return method;
|
|
234 |
}
|
|
235 |
|
|
236 |
/**
|
|
237 |
* Sets the optional extra field data for the entry.
|
|
238 |
* @param extra the extra field data bytes
|
|
239 |
* @exception IllegalArgumentException if the length of the specified
|
|
240 |
* extra field data is greater than 0xFFFF bytes
|
|
241 |
* @see #getExtra()
|
|
242 |
*/
|
|
243 |
public void setExtra(byte[] extra) {
|
|
244 |
if (extra != null && extra.length > 0xFFFF) {
|
|
245 |
throw new IllegalArgumentException("invalid extra field length");
|
|
246 |
}
|
|
247 |
this.extra = extra;
|
|
248 |
}
|
|
249 |
|
|
250 |
/**
|
|
251 |
* Returns the extra field data for the entry, or null if none.
|
|
252 |
* @return the extra field data for the entry, or null if none
|
|
253 |
* @see #setExtra(byte[])
|
|
254 |
*/
|
|
255 |
public byte[] getExtra() {
|
|
256 |
return extra;
|
|
257 |
}
|
|
258 |
|
|
259 |
/**
|
|
260 |
* Sets the optional comment string for the entry.
|
|
261 |
* @param comment the comment string
|
|
262 |
* @exception IllegalArgumentException if the length of the specified
|
|
263 |
* comment string is greater than 0xFFFF bytes
|
|
264 |
* @see #getComment()
|
|
265 |
*/
|
|
266 |
public void setComment(String comment) {
|
|
267 |
if (comment != null && comment.length() > 0xffff/3
|
|
268 |
&& ZipOutputStream.getUTF8Length(comment) > 0xffff) {
|
|
269 |
throw new IllegalArgumentException("invalid entry comment length");
|
|
270 |
}
|
|
271 |
this.comment = comment;
|
|
272 |
}
|
|
273 |
|
|
274 |
/**
|
|
275 |
* Returns the comment string for the entry, or null if none.
|
|
276 |
* @return the comment string for the entry, or null if none
|
|
277 |
* @see #setComment(String)
|
|
278 |
*/
|
|
279 |
public String getComment() {
|
|
280 |
return comment;
|
|
281 |
}
|
|
282 |
|
|
283 |
/**
|
|
284 |
* Returns true if this is a directory entry. A directory entry is
|
|
285 |
* defined to be one whose name ends with a '/'.
|
|
286 |
* @return true if this is a directory entry
|
|
287 |
*/
|
|
288 |
public boolean isDirectory() {
|
|
289 |
return name.endsWith("/");
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
|
293 |
* Returns a string representation of the ZIP entry.
|
|
294 |
*/
|
|
295 |
public String toString() {
|
|
296 |
return getName();
|
|
297 |
}
|
|
298 |
|
|
299 |
/*
|
|
300 |
* Converts DOS time to Java time (number of milliseconds since epoch).
|
|
301 |
*/
|
|
302 |
private static long dosToJavaTime(long dtime) {
|
|
303 |
Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
|
|
304 |
(int)(((dtime >> 21) & 0x0f) - 1),
|
|
305 |
(int)((dtime >> 16) & 0x1f),
|
|
306 |
(int)((dtime >> 11) & 0x1f),
|
|
307 |
(int)((dtime >> 5) & 0x3f),
|
|
308 |
(int)((dtime << 1) & 0x3e));
|
|
309 |
return d.getTime();
|
|
310 |
}
|
|
311 |
|
|
312 |
/*
|
|
313 |
* Converts Java time to DOS time.
|
|
314 |
*/
|
|
315 |
private static long javaToDosTime(long time) {
|
|
316 |
Date d = new Date(time);
|
|
317 |
int year = d.getYear() + 1900;
|
|
318 |
if (year < 1980) {
|
|
319 |
return (1 << 21) | (1 << 16);
|
|
320 |
}
|
|
321 |
return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
|
|
322 |
d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
|
|
323 |
d.getSeconds() >> 1;
|
|
324 |
}
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Returns the hash code value for this entry.
|
|
328 |
*/
|
|
329 |
public int hashCode() {
|
|
330 |
return name.hashCode();
|
|
331 |
}
|
|
332 |
|
|
333 |
/**
|
|
334 |
* Returns a copy of this entry.
|
|
335 |
*/
|
|
336 |
public Object clone() {
|
|
337 |
try {
|
|
338 |
ZipEntry e = (ZipEntry)super.clone();
|
|
339 |
e.extra = (extra == null) ? null : extra.clone();
|
|
340 |
return e;
|
|
341 |
} catch (CloneNotSupportedException e) {
|
|
342 |
// This should never happen, since we are Cloneable
|
|
343 |
throw new InternalError();
|
|
344 |
}
|
|
345 |
}
|
|
346 |
}
|