KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > util > ZipUtil


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.util;
6
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.BufferedReader JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23  * An utility to manage Zip files.
24  * @author Carlos Arévalo
25  */

26 public final class ZipUtil
27 {
28     private static final Log LOG = LogFactory.getLog(ZipUtil.class);
29
30     private static final int BUFFER_SIZE = 2048;
31
32     /**
33      * Private constructor.
34      */

35     private ZipUtil()
36     {
37     }
38
39     /**
40      * Create an input stream on a file inside an archive file.
41      * @param zipFileName the zip file name
42      * @param entryName the entry name on which the stream is to be created
43      * @return the input stream
44      * @throws IOException if there is an IO error
45      */

46     public static InputStream JavaDoc getInputStream(String JavaDoc zipFileName, String JavaDoc entryName) throws IOException JavaDoc
47     {
48         if (LOG.isDebugEnabled()) LOG.debug("Zip file name " + zipFileName);
49         ZipFile JavaDoc zip = new ZipFile JavaDoc(zipFileName);
50         ZipEntry JavaDoc entry = zip.getEntry(entryName);
51         InputStream JavaDoc is = new BufferedInputStream JavaDoc(zip.getInputStream(entry));
52
53         return is;
54     }
55
56     /**
57      * Create a reader on a zip file
58      * @param zipFileName the zip file name
59      * @param entryName the entry name on which the stream is to be created
60      * @return the input stream
61      * @throws IOException if there is an IO error
62      */

63     public static Reader JavaDoc getReader(String JavaDoc zipFileName, String JavaDoc entryName) throws IOException JavaDoc
64     {
65         InputStream JavaDoc is = getInputStream(zipFileName, entryName);
66         return new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is), BUFFER_SIZE);
67     }
68
69     /**
70      * Extract the files in a zip file to a directory
71      * @param zipFileName the zip file name
72      * @param targetDir a string containing the path name where the files will
73      * be extracted
74      * @param prefix if this parameter is not null only files starting with
75      * this prefix will be extracted.
76      * @return the path the files were extracted to
77      * @throws IOException thrown if there is an IO problem
78      */

79     public static String JavaDoc extractFiles(String JavaDoc zipFileName, String JavaDoc targetDir, String JavaDoc prefix) throws IOException JavaDoc
80     {
81         ZipFile JavaDoc zipFile = new ZipFile JavaDoc(zipFileName);
82         File JavaDoc targetDirFile = new File JavaDoc(targetDir);
83         return extractFiles(zipFile, targetDirFile, prefix);
84     }
85
86
87     /**
88      * Extract the files in a zip file to a directory
89      * @param zipFile an object of class ZipFile that represents the zip file
90      * to be extracted
91      * @param targetDir the directory where the files will be extracted
92      * @param prefix if this parameter is not null only files starting with
93      * this prefix will be extracted.
94      * @return the path the files were extracted to
95      * @throws IOException thrown if there is an IO problem
96      */

97     public static String JavaDoc extractFiles(ZipFile JavaDoc zipFile, File JavaDoc targetDir, String JavaDoc prefix) throws IOException JavaDoc
98     {
99         if (LOG.isDebugEnabled()) LOG.debug("unzipping file " + zipFile.getName());
100
101         //targetDir.mkdir();
102

103         Enumeration JavaDoc entries = zipFile.entries();
104         while (entries.hasMoreElements())
105         {
106             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
107             String JavaDoc entryName = entry.getName();
108
109             if ((prefix == null) || entryName.startsWith(prefix))
110             {
111                 if (LOG.isDebugEnabled()) LOG.debug("unzipping " + entry.getName());
112
113                 File JavaDoc file = new File JavaDoc(targetDir, entryName);
114                 if (entry.isDirectory())
115                 {
116                     file.mkdirs();
117                 }
118                 else
119                 {
120                     InputStream JavaDoc in = zipFile.getInputStream(entry);
121                     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
122
123                     byte[] b = new byte[BUFFER_SIZE];
124                     int len = in.read(b);
125                     while (len != -1)
126                     {
127                         out.write(b, 0, len);
128                         len = in.read(b);
129                     }
130                     out.close();
131                     in.close();
132                 }
133             }
134         }
135
136         return targetDir.getPath();
137     }
138 }
139
Popular Tags