KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > io > ZipFileIO


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.io;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.zip.ZipEntry JavaDoc;
25 import java.util.zip.ZipInputStream JavaDoc;
26
27 /**
28  * Zip archive operations are handled by this class.
29  *
30  * @author fdietz
31  */

32 public class ZipFileIO {
33
34     /**
35      * No instances needed.
36      */

37     private ZipFileIO() {
38         // don't instantiate this class
39
}
40
41     /**
42      * Extract zip file to destination folder.
43      *
44      * @param file
45      * zip file to extract
46      * @param destination
47      * destinatin folder
48      */

49     public static void extract(File JavaDoc file, File JavaDoc destination) throws IOException JavaDoc {
50         ZipInputStream JavaDoc in = null;
51         OutputStream JavaDoc out = null;
52         try {
53             // Open the ZIP file
54
in = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(file));
55
56             // Get the first entry
57
ZipEntry JavaDoc entry = null;
58
59             while ((entry = in.getNextEntry()) != null) {
60                 String JavaDoc outFilename = entry.getName();
61
62                 // Open the output file
63
if (entry.isDirectory()) {
64                     new File JavaDoc(destination, outFilename).mkdirs();
65                 } else {
66                     out = new FileOutputStream JavaDoc(new File JavaDoc(destination,
67                             outFilename));
68
69                     // Transfer bytes from the ZIP file to the output file
70
byte[] buf = new byte[1024];
71                     int len;
72
73                     while ((len = in.read(buf)) > 0) {
74                         out.write(buf, 0, len);
75                     }
76
77                     // Close the stream
78
out.close();
79                 }
80             }
81         } finally {
82             // Close the stream
83
if (in != null) {
84                 in.close();
85             }
86             if (out != null) {
87                 out.close();
88             }
89         }
90     }
91
92     /**
93      * Return the first directory of this archive. This is needed to determine
94      * the plugin directory.
95      *
96      * @param zipFile
97      * @return <class>File</class> containing the first entry of this archive
98      */

99     public static File JavaDoc getFirstFile(File JavaDoc zipFile) throws IOException JavaDoc {
100         ZipInputStream JavaDoc in = null;
101         try {
102             // Open the ZIP file
103
in = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(zipFile));
104
105             // Get the first entry
106
ZipEntry JavaDoc entry = null;
107
108             while ((entry = in.getNextEntry()) != null) {
109                 String JavaDoc outFilename = entry.getName();
110
111                 if (entry.isDirectory()) {
112                     return new File JavaDoc(outFilename);
113                 }
114             }
115         } finally {
116             if (in != null) {
117                 // Close the stream
118
in.close();
119             }
120         }
121         return null;
122     }
123 }
124
Popular Tags