KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > zip > ZipEntry


1 /*
2  * @(#)ZipEntry.java 1.38 05/08/09
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.zip;
9
10 import java.util.Date JavaDoc;
11
12 /**
13  * This class is used to represent a ZIP file entry.
14  *
15  * @version 1.38, 08/09/05
16  * @author David Connelly
17  */

18 public
19 class ZipEntry implements ZipConstants JavaDoc, Cloneable JavaDoc {
20     String JavaDoc name; // entry name
21
long time = -1; // modification time (in DOS time)
22
long crc = -1; // crc-32 of entry data
23
long size = -1; // uncompressed size of entry data
24
long csize = -1; // compressed size of entry data
25
int method = -1; // compression method
26
byte[] extra; // optional extra field data for entry
27
String JavaDoc comment; // optional comment string for entry
28
// The following flags are used only by Zip{Input,Output}Stream
29
int flag; // bit flags
30
int version; // version needed to extract
31
long offset; // offset of loc header
32

33     /**
34      * Compression method for uncompressed entries.
35      */

36     public static final int STORED = 0;
37
38     /**
39      * Compression method for compressed (deflated) entries.
40      */

41     public static final int DEFLATED = 8;
42
43     static {
44     /* Zip library is loaded from System.initializeSystemClass */
45     initIDs();
46     }
47
48     private static native void initIDs();
49
50     /**
51      * Creates a new zip entry with the specified name.
52      *
53      * @param name the entry name
54      * @exception NullPointerException if the entry name is null
55      * @exception IllegalArgumentException if the entry name is longer than
56      * 0xFFFF bytes
57      */

58     public ZipEntry(String JavaDoc name) {
59     if (name == null) {
60         throw new NullPointerException JavaDoc();
61     }
62     if (name.length() > 0xFFFF) {
63         throw new IllegalArgumentException JavaDoc("entry name too long");
64     }
65     this.name = name;
66     }
67
68     /**
69      * Creates a new zip entry with fields taken from the specified
70      * zip entry.
71      * @param e a zip Entry object
72      */

73     public ZipEntry(ZipEntry JavaDoc e) {
74     name = e.name;
75     time = e.time;
76     crc = e.crc;
77     size = e.size;
78     csize = e.csize;
79     method = e.method;
80     extra = e.extra;
81     comment = e.comment;
82     }
83
84     /*
85      * Creates a new zip entry for the given name with fields initialized
86      * from the specified jzentry data.
87      */

88     ZipEntry(String JavaDoc name, long jzentry) {
89     this.name = name;
90     initFields(jzentry);
91     }
92
93     private native void initFields(long jzentry);
94
95     /*
96      * Creates a new zip entry with fields initialized from the specified
97      * jzentry data.
98      */

99     ZipEntry(long jzentry) {
100     initFields(jzentry);
101     }
102
103     /**
104      * Returns the name of the entry.
105      * @return the name of the entry
106      */

107     public String JavaDoc getName() {
108     return name;
109     }
110
111     /**
112      * Sets the modification time of the entry.
113      * @param time the entry modification time in number of milliseconds
114      * since the epoch
115      * @see #getTime()
116      */

117     public void setTime(long time) {
118     this.time = javaToDosTime(time);
119     }
120
121     /**
122      * Returns the modification time of the entry, or -1 if not specified.
123      * @return the modification time of the entry, or -1 if not specified
124      * @see #setTime(long)
125      */

126     public long getTime() {
127     return time != -1 ? dosToJavaTime(time) : -1;
128     }
129
130     /**
131      * Sets the uncompressed size of the entry data.
132      * @param size the uncompressed size in bytes
133      * @exception IllegalArgumentException if the specified size is less
134      * than 0 or greater than 0xFFFFFFFF bytes
135      * @see #getSize()
136      */

137     public void setSize(long size) {
138     if (size < 0 || size > 0xFFFFFFFFL) {
139         throw new IllegalArgumentException JavaDoc("invalid entry size");
140     }
141     this.size = size;
142     }
143
144     /**
145      * Returns the uncompressed size of the entry data, or -1 if not known.
146      * @return the uncompressed size of the entry data, or -1 if not known
147      * @see #setSize(long)
148      */

149     public long getSize() {
150     return size;
151     }
152
153     /**
154      * Returns the size of the compressed entry data, or -1 if not known.
155      * In the case of a stored entry, the compressed size will be the same
156      * as the uncompressed size of the entry.
157      * @return the size of the compressed entry data, or -1 if not known
158      * @see #setCompressedSize(long)
159      */

160     public long getCompressedSize() {
161     return csize;
162     }
163
164     /**
165      * Sets the size of the compressed entry data.
166      * @param csize the compressed size to set to
167      * @see #getCompressedSize()
168      */

169     public void setCompressedSize(long csize) {
170     this.csize = csize;
171     }
172
173     /**
174      * Sets the CRC-32 checksum of the uncompressed entry data.
175      * @param crc the CRC-32 value
176      * @exception IllegalArgumentException if the specified CRC-32 value is
177      * less than 0 or greater than 0xFFFFFFFF
178      * @see #setCrc(long)
179      */

180     public void setCrc(long crc) {
181     if (crc < 0 || crc > 0xFFFFFFFFL) {
182         throw new IllegalArgumentException JavaDoc("invalid entry crc-32");
183     }
184     this.crc = crc;
185     }
186
187     /**
188      * Returns the CRC-32 checksum of the uncompressed entry data, or -1 if
189      * not known.
190      * @return the CRC-32 checksum of the uncompressed entry data, or -1 if
191      * not known
192      * @see #getCrc()
193      */

194     public long getCrc() {
195     return crc;
196     }
197
198     /**
199      * Sets the compression method for the entry.
200      * @param method the compression method, either STORED or DEFLATED
201      * @exception IllegalArgumentException if the specified compression
202      * method is invalid
203      * @see #getMethod()
204      */

205     public void setMethod(int method) {
206     if (method != STORED && method != DEFLATED) {
207         throw new IllegalArgumentException JavaDoc("invalid compression method");
208     }
209     this.method = method;
210     }
211
212     /**
213      * Returns the compression method of the entry, or -1 if not specified.
214      * @return the compression method of the entry, or -1 if not specified
215      * @see #setMethod(int)
216      */

217     public int getMethod() {
218     return method;
219     }
220
221     /**
222      * Sets the optional extra field data for the entry.
223      * @param extra the extra field data bytes
224      * @exception IllegalArgumentException if the length of the specified
225      * extra field data is greater than 0xFFFF bytes
226      * @see #getExtra()
227      */

228     public void setExtra(byte[] extra) {
229     if (extra != null && extra.length > 0xFFFF) {
230         throw new IllegalArgumentException JavaDoc("invalid extra field length");
231     }
232     this.extra = extra;
233     }
234
235     /**
236      * Returns the extra field data for the entry, or null if none.
237      * @return the extra field data for the entry, or null if none
238      * @see #setExtra(byte[])
239      */

240     public byte[] getExtra() {
241     return extra;
242     }
243
244     /**
245      * Sets the optional comment string for the entry.
246      * @param comment the comment string
247      * @exception IllegalArgumentException if the length of the specified
248      * comment string is greater than 0xFFFF bytes
249      * @see #getComment()
250      */

251     public void setComment(String JavaDoc comment) {
252     if (comment != null && comment.length() > 0xffff/3
253                     && ZipOutputStream.getUTF8Length(comment) > 0xffff) {
254         throw new IllegalArgumentException JavaDoc("invalid entry comment length");
255     }
256     this.comment = comment;
257     }
258
259     /**
260      * Returns the comment string for the entry, or null if none.
261      * @return the comment string for the entry, or null if none
262      * @see #setComment(String)
263      */

264     public String JavaDoc getComment() {
265     return comment;
266     }
267
268     /**
269      * Returns true if this is a directory entry. A directory entry is
270      * defined to be one whose name ends with a '/'.
271      * @return true if this is a directory entry
272      */

273     public boolean isDirectory() {
274     return name.endsWith("/");
275     }
276
277     /**
278      * Returns a string representation of the ZIP entry.
279      */

280     public String JavaDoc toString() {
281     return getName();
282     }
283
284     /*
285      * Converts DOS time to Java time (number of milliseconds since epoch).
286      */

287     private static long dosToJavaTime(long dtime) {
288     Date JavaDoc d = new Date JavaDoc((int)(((dtime >> 25) & 0x7f) + 80),
289               (int)(((dtime >> 21) & 0x0f) - 1),
290               (int)((dtime >> 16) & 0x1f),
291               (int)((dtime >> 11) & 0x1f),
292               (int)((dtime >> 5) & 0x3f),
293               (int)((dtime << 1) & 0x3e));
294     return d.getTime();
295     }
296
297     /*
298      * Converts Java time to DOS time.
299      */

300     private static long javaToDosTime(long time) {
301     Date JavaDoc d = new Date JavaDoc(time);
302     int year = d.getYear() + 1900;
303     if (year < 1980) {
304         return (1 << 21) | (1 << 16);
305     }
306     return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
307                d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
308                d.getSeconds() >> 1;
309     }
310
311     /**
312      * Returns the hash code value for this entry.
313      */

314     public int hashCode() {
315     return name.hashCode();
316     }
317
318     /**
319      * Returns a copy of this entry.
320      */

321     public Object JavaDoc clone() {
322     try {
323         ZipEntry JavaDoc e = (ZipEntry JavaDoc)super.clone();
324         e.extra = (extra == null ? null : (byte[])extra.clone());
325         return e;
326     } catch (CloneNotSupportedException JavaDoc e) {
327         // This should never happen, since we are Cloneable
328
throw new InternalError JavaDoc();
329     }
330     }
331 }
332
Popular Tags