2 Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java
.util
.zip
;
41 import java
.util
.Calendar
;
42 import java
.util
.Date
;
45 * This class represents a member of a zip archive. ZipFile and
46 * ZipInputStream will give you instances of this class as information
47 * about the members in an archive. On the other hand ZipOutputStream
48 * needs an instance of this class to create a new member.
50 * @author Jochen Hoenicke
52 public class ZipEntry
implements ZipConstants
, Cloneable
54 private static final int KNOWN_SIZE
= 1;
55 private static final int KNOWN_CSIZE
= 2;
56 private static final int KNOWN_CRC
= 4;
57 private static final int KNOWN_TIME
= 8;
59 private static Calendar cal
;
63 private long compressedSize
= -1;
66 private short known
= 0;
67 private short method
= -1;
68 private byte[] extra
= null;
69 private String comment
= null;
71 int flags
; /* used by ZipOutputStream */
72 int offset
; /* used by ZipFile and ZipOutputStream */
75 * Compression method. This method doesn't compress at all.
77 public static final int STORED
= 0;
79 * Compression method. This method uses the Deflater.
81 public static final int DEFLATED
= 8;
84 * Creates a zip entry with the given name.
85 * @param name the name. May include directory components separated
88 * @exception NullPointerException when name is null.
89 * @exception IllegalArgumentException when name is bigger then 65535 chars.
91 public ZipEntry(String name
)
93 int length
= name
.length();
95 throw new IllegalArgumentException("name length is " + length
);
100 * Creates a copy of the given zip entry.
101 * @param e the entry to copy.
103 public ZipEntry(ZipEntry e
)
108 compressedSize
= e
.compressedSize
;
116 final void setDOSTime(int dostime
)
118 this.dostime
= dostime
;
122 final int getDOSTime()
124 if ((known
& KNOWN_TIME
) == 0)
131 * Creates a copy of this zip entry.
136 public Object
clone()
140 // The JCL says that the `extra' field is also copied.
141 ZipEntry clone
= (ZipEntry
) super.clone();
143 clone
.extra
= (byte[]) extra
.clone();
146 catch (CloneNotSupportedException ex
)
148 throw new InternalError();
153 * Returns the entry name. The path components in the entry are
154 * always separated by slashes ('/').
156 public String
getName()
162 * Sets the time of last modification of the entry.
163 * @time the time of last modification of the entry.
165 public void setTime(long time
)
167 Calendar cal
= getCalendar();
170 cal
.setTime(new Date(time
));
171 dostime
= (cal
.get(Calendar
.YEAR
) - 1980 & 0x7f) << 25
172 | (cal
.get(Calendar
.MONTH
) + 1) << 21
173 | (cal
.get(Calendar
.DAY_OF_MONTH
)) << 16
174 | (cal
.get(Calendar
.HOUR_OF_DAY
)) << 11
175 | (cal
.get(Calendar
.MINUTE
)) << 5
176 | (cal
.get(Calendar
.SECOND
)) >> 1;
178 this.known
|= KNOWN_TIME
;
182 * Gets the time of last modification of the entry.
183 * @return the time of last modification of the entry, or -1 if unknown.
185 public long getTime()
187 if ((known
& KNOWN_TIME
) == 0)
190 int sec
= 2 * (dostime
& 0x1f);
191 int min
= (dostime
>> 5) & 0x3f;
192 int hrs
= (dostime
>> 11) & 0x1f;
193 int day
= (dostime
>> 16) & 0x1f;
194 int mon
= ((dostime
>> 21) & 0xf) - 1;
195 int year
= ((dostime
>> 25) & 0x7f) + 1980; /* since 1900 */
202 cal
.set(year
, mon
, day
, hrs
, min
, sec
);
203 return cal
.getTime().getTime();
206 catch (RuntimeException ex
)
208 /* Ignore illegal time stamp */
209 known
&= ~KNOWN_TIME
;
214 private static synchronized Calendar
getCalendar()
217 cal
= Calendar
.getInstance();
223 * Sets the size of the uncompressed data.
224 * @exception IllegalArgumentException if size is not in 0..0xffffffffL
226 public void setSize(long size
)
228 if ((size
& 0xffffffff00000000L
) != 0)
229 throw new IllegalArgumentException();
230 this.size
= (int) size
;
231 this.known
|= KNOWN_SIZE
;
235 * Gets the size of the uncompressed data.
236 * @return the size or -1 if unknown.
238 public long getSize()
240 return (known
& KNOWN_SIZE
) != 0 ? size
& 0xffffffffL
: -1L;
244 * Sets the size of the compressed data.
246 public void setCompressedSize(long csize
)
248 this.compressedSize
= csize
;
252 * Gets the size of the compressed data.
253 * @return the size or -1 if unknown.
255 public long getCompressedSize()
257 return compressedSize
;
261 * Sets the crc of the uncompressed data.
262 * @exception IllegalArgumentException if crc is not in 0..0xffffffffL
264 public void setCrc(long crc
)
266 if ((crc
& 0xffffffff00000000L
) != 0)
267 throw new IllegalArgumentException();
268 this.crc
= (int) crc
;
269 this.known
|= KNOWN_CRC
;
273 * Gets the crc of the uncompressed data.
274 * @return the crc or -1 if unknown.
278 return (known
& KNOWN_CRC
) != 0 ? crc
& 0xffffffffL
: -1L;
282 * Sets the compression method. Only DEFLATED and STORED are
284 * @exception IllegalArgumentException if method is not supported.
285 * @see ZipOutputStream#DEFLATED
286 * @see ZipOutputStream#STORED
288 public void setMethod(int method
)
290 if (method
!= ZipOutputStream
.STORED
291 && method
!= ZipOutputStream
.DEFLATED
)
292 throw new IllegalArgumentException();
293 this.method
= (short) method
;
297 * Gets the compression method.
298 * @return the compression method or -1 if unknown.
300 public int getMethod()
306 * Sets the extra data.
307 * @exception IllegalArgumentException if extra is longer than 0xffff bytes.
309 public void setExtra(byte[] extra
)
317 if (extra
.length
> 0xffff)
318 throw new IllegalArgumentException();
323 while (pos
< extra
.length
)
325 int sig
= (extra
[pos
++] & 0xff)
326 | (extra
[pos
++] & 0xff) << 8;
327 int len
= (extra
[pos
++] & 0xff)
328 | (extra
[pos
++] & 0xff) << 8;
331 /* extended time stamp */
332 int flags
= extra
[pos
];
333 if ((flags
& 1) != 0)
335 long time
= ((extra
[pos
+1] & 0xff)
336 | (extra
[pos
+2] & 0xff) << 8
337 | (extra
[pos
+3] & 0xff) << 16
338 | (extra
[pos
+4] & 0xff) << 24);
345 catch (ArrayIndexOutOfBoundsException ex
)
353 * Gets the extra data.
354 * @return the extra data or null if not set.
356 public byte[] getExtra()
362 * Sets the entry comment.
363 * @exception IllegalArgumentException if comment is longer than 0xffff.
365 public void setComment(String comment
)
367 if (comment
!= null && comment
.length() > 0xffff)
368 throw new IllegalArgumentException();
369 this.comment
= comment
;
374 * @return the comment or null if not set.
376 public String
getComment()
382 * Gets true, if the entry is a directory. This is solely
383 * determined by the name, a trailing slash '/' marks a directory.
385 public boolean isDirectory()
387 int nlen
= name
.length();
388 return nlen
> 0 && name
.charAt(nlen
- 1) == '/';
392 * Gets the string representation of this ZipEntry. This is just
393 * the name as returned by getName().
395 public String
toString()
401 * Gets the hashCode of this ZipEntry. This is just the hashCode
402 * of the name. Note that the equals method isn't changed, though.
404 public int hashCode()
406 return name
.hashCode();