KickJava   Java API By Example, From Geeks To Geeks.

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


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

16
17 package de.schlichtherle.util.zip;
18
19 import de.schlichtherle.io.rof.ReadOnlyFile;
20 import de.schlichtherle.io.util.SynchronizedInputStream;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.zip.ZipException JavaDoc;
30
31 /**
32  * Drop-in replacement for {@link java.util.zip.ZipFile java.util.zip.ZipFile}.
33  * <p>
34  * This class adds support for file name encodings other than UTF-8
35  * (which is required to work on ZIP compatible files created by native
36  * zip tools and is able to skip a preamble like the one found in self
37  * extracting archives. Furthermore it returns instances of
38  * <code>de.schlichtherle.util.zip.ZipEntry</code> instead of
39  * <code>java.util.zip.ZipEntry</code>.
40  * <p>
41  * It doesn't extend <code>java.util.zip.ZipFile</code> as it would
42  * have to reimplement all methods anyway. Like
43  * <code>java.util.ZipFile</code>, it uses RandomAccessFile under the
44  * covers and supports compressed and uncompressed entries.
45  * <p>
46  * This class is mostly backwards compatible to
47  * <code>java.util.zip.ZipFile</code>, with the following exceptions:
48  * <ul>
49  * <li>There is no <code>getName()</code> method.</li>
50  * <li>This class is designed to be thread-safe.</li>
51  * </ul>
52  */

53 public class ZipFile extends BasicZipFile {
54
55     /**
56      * Opens the given file for reading its ZIP contents,
57      * assuming UTF-8 encoding for file names.
58      *
59      * @param name name of the file.
60      *
61      * @throws NullPointerException If <code>name</code> is <code>null</code>.
62      * @throws FileNotFoundException If the file cannot get opened for reading.
63      * @throws ZipException If the file is not ZIP compatible.
64      * @throws IOException On any other I/O related issue.
65      */

66     public ZipFile(String JavaDoc name)
67     throws NullPointerException JavaDoc,
68             FileNotFoundException JavaDoc,
69             ZipException JavaDoc,
70             IOException JavaDoc {
71         super(name);
72     }
73
74     /**
75      * Opens the given file for reading its ZIP contents,
76      * assuming the specified encoding for file names.
77      *
78      * @param name name of the file.
79      * @param encoding the encoding to use for file names
80      *
81      * @throws NullPointerException If <code>name</code> or <code>encoding</code> is
82      * <code>null</code>.
83      * @throws UnsupportedEncodingException If encoding is not supported by
84      * this JVM.
85      * @throws FileNotFoundException If the file cannot get opened for reading.
86      * @throws ZipException If the file is not ZIP compatible.
87      * @throws IOException On any other I/O related issue.
88      */

89     public ZipFile(String JavaDoc name, String JavaDoc encoding)
90     throws NullPointerException JavaDoc,
91             UnsupportedEncodingException JavaDoc,
92             FileNotFoundException JavaDoc,
93             ZipException JavaDoc,
94             IOException JavaDoc {
95         super(name, encoding);
96     }
97
98     /**
99      * Opens the given file for reading its ZIP contents,
100      * assuming the specified encoding for file names.
101      *
102      * @param name name of the file.
103      * @param encoding the encoding to use for file names
104      * @param preambled If this is <code>true</code>, then the ZIP compatible
105      * file may have a preamble.
106      * Otherwise, the ZIP compatible file must start with either a
107      * Local File Header (LFH) signature or an End Of Central Directory
108      * (EOCD) Header, causing this constructor to fail fast if the file
109      * is actually a false positive ZIP compatible file, i.e. not
110      * compatible to the ZIP File Format Specification.
111      * This may be used to read Self Extracting ZIP files (SFX), which
112      * usually contain the application code required for extraction in
113      * a preamble.
114      * This parameter is <code>true</code> by default.
115      * @param postambled If this is <code>true</code>, then the ZIP compatible
116      * file may have a postamble of arbitrary length.
117      * Otherwise, the ZIP compatible file must not have a postamble
118      * which exceeds 64KB size, including the End Of Central Directory
119      * record (i.e. including the ZIP file comment), causing this
120      * constructor to fail fast if the file is actually a false positive
121      * ZIP compatible file, i.e. not compatible to the ZIP File Format
122      * Specification.
123      * This may be used to read Self Extracting ZIP files (SFX) with
124      * large postambles.
125      * This parameter is <code>false</code> by default.
126      *
127      * @throws NullPointerException If <code>name</code> or <code>encoding</code> is
128      * <code>null</code>.
129      * @throws UnsupportedEncodingException If encoding is not supported by
130      * this JVM.
131      * @throws FileNotFoundException If the file cannot get opened for reading.
132      * @throws ZipException If the file is not ZIP compatible.
133      * @throws IOException On any other I/O related issue.
134      */

135     public ZipFile(
136             String JavaDoc name,
137             String JavaDoc encoding,
138             boolean preambled,
139             boolean postambled)
140     throws NullPointerException JavaDoc,
141             UnsupportedEncodingException JavaDoc,
142             FileNotFoundException JavaDoc,
143             ZipException JavaDoc,
144             IOException JavaDoc {
145         super(name, encoding, preambled, postambled);
146     }
147
148     /**
149      * Opens the given file for reading its ZIP contents,
150      * assuming UTF-8 encoding for file names.
151      *
152      * @param file The file.
153      *
154      * @throws NullPointerException If <code>file</code> is <code>null</code>.
155      * @throws FileNotFoundException If the file cannot get opened for reading.
156      * @throws ZipException If the file is not ZIP compatible.
157      * @throws IOException On any other I/O related issue.
158      */

159     public ZipFile(File file)
160     throws NullPointerException JavaDoc,
161             FileNotFoundException JavaDoc,
162             ZipException JavaDoc,
163             IOException JavaDoc {
164         super(file);
165     }
166
167     /**
168      * Opens the given file for reading its ZIP contents,
169      * assuming the specified encoding for file names.
170      *
171      * @param file The file.
172      * @param encoding The encoding to use for entry names and comments
173      * - must <em>not</em> be <code>null</code>!
174      *
175      * @throws NullPointerException If <code>file</code> or <code>encoding</code> is
176      * <code>null</code>.
177      * @throws UnsupportedEncodingException If encoding is not supported by
178      * this JVM.
179      * @throws FileNotFoundException If the file cannot get opened for reading.
180      * @throws ZipException If the file is not ZIP compatible.
181      * @throws IOException On any other I/O related issue.
182      */

183     public ZipFile(File file, String JavaDoc encoding)
184     throws NullPointerException JavaDoc,
185             UnsupportedEncodingException JavaDoc,
186             FileNotFoundException JavaDoc,
187             ZipException JavaDoc,
188             IOException JavaDoc {
189         super(file, encoding);
190     }
191
192     /**
193      * Opens the given file for reading its ZIP contents,
194      * assuming the specified encoding for file names.
195      *
196      * @param file The file.
197      * @param encoding The encoding to use for entry names and comments
198      * - must <em>not</em> be <code>null</code>!
199      * @param preambled If this is <code>true</code>, then the ZIP compatible
200      * file may have a preamble.
201      * Otherwise, the ZIP compatible file must start with either a
202      * Local File Header (LFH) signature or an End Of Central Directory
203      * (EOCD) Header, causing this constructor to fail fast if the file
204      * is actually a false positive ZIP compatible file, i.e. not
205      * compatible to the ZIP File Format Specification.
206      * This may be used to read Self Extracting ZIP files (SFX), which
207      * usually contain the application code required for extraction in
208      * a preamble.
209      * This parameter is <code>true</code> by default.
210      * @param postambled If this is <code>true</code>, then the ZIP compatible
211      * file may have a postamble of arbitrary length.
212      * Otherwise, the ZIP compatible file must not have a postamble
213      * which exceeds 64KB size, including the End Of Central Directory
214      * record (i.e. including the ZIP file comment), causing this
215      * constructor to fail fast if the file is actually a false positive
216      * ZIP compatible file, i.e. not compatible to the ZIP File Format
217      * Specification.
218      * This may be used to read Self Extracting ZIP files (SFX) with
219      * large postambles.
220      * This parameter is <code>false</code> by default.
221      *
222      * @throws NullPointerException If <code>file</code> or <code>encoding</code> is
223      * <code>null</code>.
224      * @throws UnsupportedEncodingException If encoding is not supported by
225      * this JVM.
226      * @throws FileNotFoundException If the file cannot get opened for reading.
227      * @throws ZipException If the file is not ZIP compatible.
228      * @throws IOException On any other I/O related issue.
229      */

230     public ZipFile(
231             File file,
232             String JavaDoc encoding,
233             boolean preambled,
234             boolean postambled)
235     throws NullPointerException JavaDoc,
236             UnsupportedEncodingException JavaDoc,
237             FileNotFoundException JavaDoc,
238             ZipException JavaDoc,
239             IOException JavaDoc {
240         super(file, encoding, preambled, postambled);
241     }
242
243     /**
244      * Opens the given read only file for reading its ZIP contents,
245      * assuming UTF-8 encoding for file names.
246      *
247      * @param rof The read only file.
248      * Note that this constructor <em>never</em> closes this file.
249      *
250      * @throws NullPointerException If <code>file</code> is <code>null</code>.
251      * @throws FileNotFoundException If the file cannot get opened for reading.
252      * @throws ZipException If the file is not ZIP compatible.
253      * @throws IOException On any other I/O related issue.
254      */

255     public ZipFile(ReadOnlyFile rof)
256     throws NullPointerException JavaDoc,
257             FileNotFoundException JavaDoc,
258             ZipException JavaDoc,
259             IOException JavaDoc {
260         super(rof);
261     }
262
263     /**
264      * Opens the given read only file for reading its ZIP contents,
265      * assuming the specified encoding for file names.
266      *
267      * @param rof The read only file.
268      * Note that this constructor <em>never</em> closes this file.
269      * @param encoding The encoding to use for entry names and comments
270      * - must <em>not</em> be <code>null</code>!
271      *
272      * @throws NullPointerException If <code>file</code> or <code>encoding</code> is
273      * <code>null</code>.
274      * @throws UnsupportedEncodingException If encoding is not supported by
275      * this JVM.
276      * @throws FileNotFoundException If the file cannot get opened for reading.
277      * @throws ZipException If the file is not ZIP compatible.
278      * @throws IOException On any other I/O related issue.
279      */

280     public ZipFile(ReadOnlyFile rof, String JavaDoc encoding)
281     throws NullPointerException JavaDoc,
282             UnsupportedEncodingException JavaDoc,
283             FileNotFoundException JavaDoc,
284             ZipException JavaDoc,
285             IOException JavaDoc {
286         super(rof, encoding);
287     }
288
289     /**
290      * Opens the given read only file for reading its ZIP contents,
291      * assuming the specified encoding for file names.
292      *
293      * @param rof The read only file.
294      * Note that this constructor <em>never</em> closes this file.
295      * @param encoding The encoding to use for entry names and comments
296      * - must <em>not</em> be <code>null</code>!
297      * @param preambled If this is <code>true</code>, then the ZIP compatible
298      * file may have a preamble.
299      * Otherwise, the ZIP compatible file must start with either a
300      * Local File Header (LFH) signature or an End Of Central Directory
301      * (EOCD) Header, causing this constructor to fail fast if the file
302      * is actually a false positive ZIP compatible file, i.e. not
303      * compatible to the ZIP File Format Specification.
304      * This may be used to read Self Extracting ZIP files (SFX), which
305      * usually contain the application code required for extraction in
306      * a preamble.
307      * This parameter is <code>true</code> by default.
308      * @param postambled If this is <code>true</code>, then the ZIP compatible
309      * file may have a postamble of arbitrary length.
310      * Otherwise, the ZIP compatible file must not have a postamble
311      * which exceeds 64KB size, including the End Of Central Directory
312      * record (i.e. including the ZIP file comment), causing this
313      * constructor to fail fast if the file is actually a false positive
314      * ZIP compatible file, i.e. not compatible to the ZIP File Format
315      * Specification.
316      * This may be used to read Self Extracting ZIP files (SFX) with
317      * large postambles.
318      * This parameter is <code>false</code> by default.
319      *
320      * @throws NullPointerException If <code>file</code> or <code>encoding</code> is
321      * <code>null</code>.
322      * @throws UnsupportedEncodingException If encoding is not supported by
323      * this JVM.
324      * @throws FileNotFoundException If the file cannot get opened for reading.
325      * @throws ZipException If the file is not ZIP compatible.
326      * @throws IOException On any other I/O related issue.
327      */

328     public ZipFile(
329             ReadOnlyFile rof,
330             String JavaDoc encoding,
331             boolean preambled,
332             boolean postambled)
333     throws NullPointerException JavaDoc,
334             UnsupportedEncodingException JavaDoc,
335             FileNotFoundException JavaDoc,
336             ZipException JavaDoc,
337             IOException JavaDoc {
338         super(rof, encoding, preambled, postambled);
339     }
340
341     /**
342      * Returns a safe enumeration of clones of the ZIP entries in this ZIP file.
343      * This method takes a snapshot of the collection of all entries and
344      * enumerates their clones, so concurrent modifications or state changes
345      * do not affect this instance, the returned enumeration or the
346      * enumerated ZIP entries.
347      */

348     public synchronized Enumeration JavaDoc entries() {
349     return new Enumeration JavaDoc() {
350         Enumeration JavaDoc e = Collections.enumeration(Collections.list(
351                     ZipFile.super.entries()));
352
353         public boolean hasMoreElements() {
354         return e.hasMoreElements();
355         }
356
357         public Object JavaDoc nextElement() {
358         return ((ZipEntry) e.nextElement()).clone();
359         }
360         };
361     }
362
363     /**
364      * Returns a clone of the {@link ZipEntry} for the given name or
365      * <code>null</code> if no entry with that name exists.
366      *
367      * @param name Name of the ZIP entry.
368      */

369     public synchronized ZipEntry getEntry(String JavaDoc name) {
370         ZipEntry ze = super.getEntry(name);
371         return ze != null ? (ZipEntry) ze.clone() : null;
372     }
373
374     public synchronized InputStream getPreambleInputStream() throws IOException JavaDoc {
375         return new SynchronizedInputStream(
376                 super.getPreambleInputStream(),
377                 this);
378     }
379
380     public synchronized InputStream getPostambleInputStream() throws IOException JavaDoc {
381         return new SynchronizedInputStream(
382                 super.getPostambleInputStream(),
383                 this);
384     }
385
386     public synchronized boolean busy() {
387         return super.busy();
388     }
389
390     protected synchronized InputStream getInputStream(
391             final String JavaDoc name,
392             final boolean check,
393             final boolean inflate)
394     throws IOException JavaDoc {
395         return new SynchronizedInputStream(
396                 super.getInputStream(name, check, inflate),
397                 this);
398     }
399
400     public synchronized void close() throws IOException JavaDoc {
401         super.close();
402     }
403 }
404
Popular Tags