KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > util > zip > ZipEntry


1 /*
2  * Copyright 2005-2006 Schlichtherle IT Services
3  * Copyright 2001-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package de.schlichtherle.util.zip;
19
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.GregorianCalendar JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.zip.ZipException JavaDoc;
25
26 /**
27  * Replacement for {@link java.util.zip.ZipEntry}.
28  * <p>
29  * Note that a <code>ZipEntry</code> object can be used with only one
30  * {@link ZipFile} or {@link ZipOutputStream} object.
31  * Reusing the same <code>ZipEntry</code> object with a second object of these
32  * classes is an error and may result in unpredictable behaviour.
33  */

34 public class ZipEntry implements ZipConstants, Cloneable JavaDoc {
35     private String JavaDoc name;
36     private short platform = PLATFORM_FAT; // 2 bytes unsigned int
37
private short method = -1; // 2 bytes unsigned int
38
private long time = -1; // dos time as 4 bytes unsigned int
39
private long crc = -1; // 4 bytes unsigned int
40
private long csize = -1; // 4 bytes unsigned int
41
private long size = -1; // 4 bytes unsigned int
42
private byte[] extra; // null if no extra field
43
private String JavaDoc comment; // null if no comment field
44

45     /** Meaning depends on using class. */
46     long offset = -1;
47
48     /**
49      * Creates a new zip entry with the specified name.
50      */

51     public ZipEntry(String JavaDoc name) {
52     if (name == null)
53         throw new NullPointerException JavaDoc();
54     if (name.length() > 0xFFFF)
55         throw new IllegalArgumentException JavaDoc("Entry name too long!");
56         this.name = name;
57     }
58
59     /**
60      * Creates a new zip entry with fields taken from the specified zip entry.
61      */

62     public ZipEntry(ZipEntry entry) {
63     name = entry.name;
64     method = entry.method;
65     time = entry.time;
66     crc = entry.crc;
67     size = entry.size;
68     csize = entry.csize;
69     extra = entry.extra;
70     comment = entry.comment;
71         offset = entry.offset;
72     }
73
74     /**
75      * Overwrite clone.
76      */

77     public Object JavaDoc clone() {
78     try {
79         final ZipEntry entry = (ZipEntry) super.clone();
80         entry.extra = extra != null ? (byte[]) extra.clone() : null;
81         return entry;
82     }
83         catch (CloneNotSupportedException JavaDoc cannotHappen) {
84         throw new AssertionError JavaDoc(cannotHappen); // never say never
85
}
86     }
87
88     /** Returns the ZIP entry name. */
89     public String JavaDoc getName() {
90     return name;
91     }
92
93     /**
94      * Sets the ZIP entry name.
95      *
96      * @since TrueZIP 6.0
97      */

98     protected void setName(String JavaDoc name) {
99         this.name = name;
100     }
101
102     /**
103      * Returns true if and only if this ZIP entry represents a directory entry
104      * (i.e. end with <code>'/'</code>).
105      */

106     public boolean isDirectory() {
107     return name.endsWith("/");
108     }
109
110     public short getPlatform() {
111     return platform;
112     }
113
114     public void setPlatform(short platform) {
115         this.platform = platform;
116     }
117
118     public short getMethod() {
119     return method;
120     }
121
122     public void setMethod(short method) {
123     if (method != STORED && method != DEFLATED)
124         throw new IllegalArgumentException JavaDoc(name
125                     + ": Invalid entry compression method!");
126         this.method = method;
127     }
128
129     protected long getDosTime() {
130     return time;
131     }
132
133     protected void setDosTime(long time) {
134         if (time < 0)
135             throw new IllegalArgumentException JavaDoc(name
136                     + ": Invalid entry modification time!");
137     this.time = time;
138     }
139
140     public long getTime() {
141     return time != -1 ? dos2javaTime(time) : -1;
142     }
143
144     public void setTime(long time) {
145         if (time < 0)
146             throw new IllegalArgumentException JavaDoc(name
147                     + ": Invalid entry modification time!");
148     this.time = time != -1 ? java2dosTime(time) : -1;
149     }
150
151     public long getCrc() {
152     return crc;
153     }
154
155     public void setCrc(long crc) {
156     if (crc < 0 || 0xFFFFFFFFl < crc)
157         throw new IllegalArgumentException JavaDoc(name
158                     + ": Invalid entry crc!");
159     this.crc = crc;
160     }
161
162     public long getCompressedSize() {
163     return csize;
164     }
165
166     public void setCompressedSize(long csize) {
167     if (csize < 0 || 0xFFFFFFFFl < csize)
168         throw new IllegalArgumentException JavaDoc(name
169                     + ": Invalid entry compressed size!");
170     this.csize = csize;
171     }
172
173     public long getSize() {
174     return size;
175     }
176
177     public void setSize(long size) {
178     if (size < 0 || 0xFFFFFFFFl < size)
179         throw new IllegalArgumentException JavaDoc(name
180                     + ": Invalid entry size!");
181     this.size = size;
182     }
183
184     public byte[] getExtra() {
185     return extra != null ? (byte[]) extra.clone() : null;
186     }
187
188     public void setExtra(byte[] extra) {
189     if (extra != null && 0xFFFF < extra.length)
190         throw new IllegalArgumentException JavaDoc(name
191                     + ": Invalid entry extra field length!");
192     this.extra = extra;
193     }
194
195     public String JavaDoc getComment() {
196     return comment;
197     }
198
199     public void setComment(String JavaDoc comment) {
200     if (comment != null && 0xFFFF < comment.length())
201         throw new IllegalArgumentException JavaDoc(name
202                     + ": Invalid entry comment length!");
203     this.comment = comment;
204     }
205
206     /**
207      * Returns the ZIP entry name.
208      */

209     public String JavaDoc toString() {
210     return getName();
211     }
212
213     //
214
// Time conversion.
215
//
216

217     /**
218      * Converts a DOS date/time field to unix time.
219      *
220      * @param dosTime Contains the DOS date/time field.
221      *
222      * @return The number of milliseconds from the epoch.
223      */

224     protected static long dos2javaTime(long dosTime) {
225         final Calendar JavaDoc cal = (Calendar JavaDoc) calendar.get();
226         cal.set(Calendar.YEAR, (int) ((dosTime >> 25) & 0xff) + 1980);
227         cal.set(Calendar.MONTH, (int) ((dosTime >> 21) & 0x0f) - 1);
228         cal.set(Calendar.DATE, (int) (dosTime >> 16) & 0x1f);
229         cal.set(Calendar.HOUR_OF_DAY, (int) (dosTime >> 11) & 0x1f);
230         cal.set(Calendar.MINUTE, (int) (dosTime >> 5) & 0x3f);
231         cal.set(Calendar.SECOND, (int) (dosTime << 1) & 0x3e);
232         // According to the ZIP file format specification, its internal time
233
// has only two seconds granularity.
234
// Make calendar return only total seconds in order to make
235
// getTime work correctly.
236
cal.set(Calendar.MILLISECOND, 0);
237         return cal.getTimeInMillis();
238     }
239
240     /**
241      * Converts unix time to a DOS date/time field.
242      *
243      * @param time The number of milliseconds from the epoch.
244      *
245      * @return The DOS date/time field.
246      */

247     protected static long java2dosTime(long time) {
248         final Calendar JavaDoc cal = (Calendar JavaDoc) calendar.get();
249         cal.setTimeInMillis(time);
250         int year = cal.get(Calendar.YEAR);
251         if (year < 1980)
252             return MIN_DOS_TIME;
253         return (((year - 1980) & 0xff) << 25)
254              | ((cal.get(Calendar.MONTH) + 1) << 21)
255              | (cal.get(Calendar.DAY_OF_MONTH) << 16)
256              | (cal.get(Calendar.HOUR_OF_DAY) << 11)
257              | (cal.get(Calendar.MINUTE) << 5)
258              | (cal.get(Calendar.SECOND) >> 1);
259     }
260
261     private static final ThreadLocal JavaDoc calendar = new ThreadLocal JavaDoc() {
262         protected Object JavaDoc initialValue() {
263             return new GregorianCalendar JavaDoc();
264         }
265     };
266 }
Popular Tags