1:
37:
38:
39: package ;
40:
41: import ;
42:
43:
51: public class ZipEntry implements ZipConstants, Cloneable
52: {
53: private static final byte KNOWN_SIZE = 1;
54: private static final byte KNOWN_CSIZE = 2;
55: private static final byte KNOWN_CRC = 4;
56: private static final byte KNOWN_TIME = 8;
57: private static final byte KNOWN_DOSTIME = 16;
58: private static final byte KNOWN_EXTRA = 32;
59:
60:
61: private final String name;
62:
63: private int size;
64:
65: private long compressedSize = -1;
66:
67: private int crc;
68:
69: private String comment = null;
70:
71: private byte method = -1;
72:
73: private byte known = 0;
74:
78: private int dostime;
79:
83: private long time;
84:
85: private byte[] extra = null;
86:
87: int flags;
88: int offset;
89:
90:
93: public static final int STORED = 0;
94:
97: public static final int DEFLATED = 8;
98:
99:
107: public ZipEntry(String name)
108: {
109: int length = name.length();
110: if (length > 65535)
111: throw new IllegalArgumentException("name length is " + length);
112: this.name = name;
113: }
114:
115:
119: public ZipEntry(ZipEntry e)
120: {
121: this(e, e.name);
122: }
123:
124: ZipEntry(ZipEntry e, String name)
125: {
126: this.name = name;
127: known = e.known;
128: size = e.size;
129: compressedSize = e.compressedSize;
130: crc = e.crc;
131: dostime = e.dostime;
132: time = e.time;
133: method = e.method;
134: extra = e.extra;
135: comment = e.comment;
136: }
137:
138: final void setDOSTime(int dostime)
139: {
140: this.dostime = dostime;
141: known |= KNOWN_DOSTIME;
142: known &= ~KNOWN_TIME;
143: }
144:
145: final int getDOSTime()
146: {
147: if ((known & KNOWN_DOSTIME) != 0)
148: return dostime;
149: else if ((known & KNOWN_TIME) != 0)
150: {
151: Calendar cal = Calendar.getInstance();
152: cal.setTimeInMillis(time);
153: dostime = (cal.get(Calendar.YEAR) - 1980 & 0x7f) << 25
154: | (cal.get(Calendar.MONTH) + 1) << 21
155: | (cal.get(Calendar.DAY_OF_MONTH)) << 16
156: | (cal.get(Calendar.HOUR_OF_DAY)) << 11
157: | (cal.get(Calendar.MINUTE)) << 5
158: | (cal.get(Calendar.SECOND)) >> 1;
159: known |= KNOWN_DOSTIME;
160: return dostime;
161: }
162: else
163: return 0;
164: }
165:
166:
169: public Object clone()
170: {
171:
172:
173:
174:
175: ZipEntry clone;
176: if (this.getClass() == ZipEntry.class)
177: clone = new ZipEntry(this);
178: else
179: {
180: try
181: {
182: clone = (ZipEntry) super.clone();
183: }
184: catch (CloneNotSupportedException e)
185: {
186: throw new InternalError();
187: }
188: }
189: if (extra != null)
190: {
191: clone.extra = new byte[extra.length];
192: System.arraycopy(extra, 0, clone.extra, 0, extra.length);
193: }
194: return clone;
195: }
196:
197:
201: public String getName()
202: {
203: return name;
204: }
205:
206:
210: public void setTime(long time)
211: {
212: this.time = time;
213: this.known |= KNOWN_TIME;
214: this.known &= ~KNOWN_DOSTIME;
215: }
216:
217:
221: public long getTime()
222: {
223:
224: parseExtra();
225:
226: if ((known & KNOWN_TIME) != 0)
227: return time;
228: else if ((known & KNOWN_DOSTIME) != 0)
229: {
230: int sec = 2 * (dostime & 0x1f);
231: int min = (dostime >> 5) & 0x3f;
232: int hrs = (dostime >> 11) & 0x1f;
233: int day = (dostime >> 16) & 0x1f;
234: int mon = ((dostime >> 21) & 0xf) - 1;
235: int year = ((dostime >> 25) & 0x7f) + 1980;
236:
237: try
238: {
239: Calendar cal = Calendar.getInstance();
240: cal.set(year, mon, day, hrs, min, sec);
241: time = cal.getTimeInMillis();
242: known |= KNOWN_TIME;
243: return time;
244: }
245: catch (RuntimeException ex)
246: {
247:
248: known &= ~KNOWN_TIME;
249: return -1;
250: }
251: }
252: else
253: return -1;
254: }
255:
256:
260: public void setSize(long size)
261: {
262: if ((size & 0xffffffff00000000L) != 0)
263: throw new IllegalArgumentException();
264: this.size = (int) size;
265: this.known |= KNOWN_SIZE;
266: }
267:
268:
272: public long getSize()
273: {
274: return (known & KNOWN_SIZE) != 0 ? size & 0xffffffffL : -1L;
275: }
276:
277:
280: public void setCompressedSize(long csize)
281: {
282: this.compressedSize = csize;
283: }
284:
285:
289: public long getCompressedSize()
290: {
291: return compressedSize;
292: }
293:
294:
298: public void setCrc(long crc)
299: {
300: if ((crc & 0xffffffff00000000L) != 0)
301: throw new IllegalArgumentException();
302: this.crc = (int) crc;
303: this.known |= KNOWN_CRC;
304: }
305:
306:
310: public long getCrc()
311: {
312: return (known & KNOWN_CRC) != 0 ? crc & 0xffffffffL : -1L;
313: }
314:
315:
322: public void setMethod(int method)
323: {
324: if (method != ZipOutputStream.STORED
325: && method != ZipOutputStream.DEFLATED)
326: throw new IllegalArgumentException();
327: this.method = (byte) method;
328: }
329:
330:
334: public int getMethod()
335: {
336: return method;
337: }
338:
339:
343: public void setExtra(byte[] extra)
344: {
345: if (extra == null)
346: {
347: this.extra = null;
348: return;
349: }
350: if (extra.length > 0xffff)
351: throw new IllegalArgumentException();
352: this.extra = extra;
353: }
354:
355: private void parseExtra()
356: {
357:
358: if ((known & KNOWN_EXTRA) != 0)
359: return;
360:
361: if (extra == null)
362: {
363: known |= KNOWN_EXTRA;
364: return;
365: }
366:
367: try
368: {
369: int pos = 0;
370: while (pos < extra.length)
371: {
372: int sig = (extra[pos++] & 0xff)
373: | (extra[pos++] & 0xff) << 8;
374: int len = (extra[pos++] & 0xff)
375: | (extra[pos++] & 0xff) << 8;
376: if (sig == 0x5455)
377: {
378:
379: int flags = extra[pos];
380: if ((flags & 1) != 0)
381: {
382: long time = ((extra[pos+1] & 0xff)
383: | (extra[pos+2] & 0xff) << 8
384: | (extra[pos+3] & 0xff) << 16
385: | (extra[pos+4] & 0xff) << 24);
386: setTime(time*1000);
387: }
388: }
389: pos += len;
390: }
391: }
392: catch (ArrayIndexOutOfBoundsException ex)
393: {
394:
395: }
396:
397: known |= KNOWN_EXTRA;
398: return;
399: }
400:
401:
405: public byte[] getExtra()
406: {
407: return extra;
408: }
409:
410:
414: public void setComment(String comment)
415: {
416: if (comment != null && comment.length() > 0xffff)
417: throw new IllegalArgumentException();
418: this.comment = comment;
419: }
420:
421:
425: public String getComment()
426: {
427: return comment;
428: }
429:
430:
434: public boolean isDirectory()
435: {
436: int nlen = name.length();
437: return nlen > 0 && name.charAt(nlen - 1) == '/';
438: }
439:
440:
444: public String toString()
445: {
446: return name;
447: }
448:
449:
453: public int hashCode()
454: {
455: return name.hashCode();
456: }
457: }