KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > core > ZipBean


1 /*
2  * ZipBean.java
3  *
4  * Created on 18. August 2003, 14:06
5  */

6
7 package org.contineo.core;
8
9 import java.io.BufferedOutputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.util.Enumeration JavaDoc;
14
15 import org.apache.log4j.Level;
16 import org.apache.log4j.Logger;
17 import org.apache.tools.zip.ZipEntry;
18 import org.apache.tools.zip.ZipFile;
19 /**
20  * This class is for handling with zip-files.
21  * @author Michael Scholz
22  * @version 1.0
23  */

24 public class ZipBean {
25  
26     /**
27      * This method extracts all entries of a zip-file.
28      * @param zipsource Path of the zip-file.
29      * @param target Path of the extracted files.
30      * @return True if successfully extracted.
31      */

32     public static boolean unzip(String JavaDoc zipsource, String JavaDoc target) {
33         boolean result = true;
34         try {
35             if(!target.endsWith("/"))
36                 target = target + "/";
37             ZipFile zip = new ZipFile(zipsource);
38             Enumeration JavaDoc entries = zip.getEntries();
39             
40             while (entries.hasMoreElements()) {
41                 ZipEntry entry = (ZipEntry) entries.nextElement();
42                 saveEntry(target, zip, entry);
43             }
44
45             zip.close();
46         }
47         catch (Exception JavaDoc e) {
48             result = false;
49             logError(e.getMessage());
50         }
51         return result;
52     }
53     
54     /**
55      * This method extracts one entry of a zip-file.
56      * @param zipsource Path of the zip-file.
57      * @param target Path of the extracted files.
58      * @param entry Name of the entry to be extracted.
59      * @return True if successfully extracted.
60      */

61     public static boolean unzip(String JavaDoc zipsource, String JavaDoc target, String JavaDoc entry) {
62         boolean result = true;
63         try {
64             if(!target.endsWith("/"))
65                 target = target + "/";
66             ZipFile zip = new ZipFile(zipsource);
67             ZipEntry zipe = new ZipEntry(entry);
68             saveEntry(target, zip, zipe);
69         }
70         catch (Exception JavaDoc e) {
71             result = false;
72             logError(e.getMessage());
73         }
74         return result;
75     }
76     
77     /**
78      * Extracts an entry from a zip file to a target directory.
79      * @param target the base directory the entry should be extracted to
80      * @param zip the ZIP file
81      * @param entry the to be extracted entry in the ZIP file
82      */

83     private static void saveEntry(String JavaDoc target, ZipFile zip, ZipEntry entry) throws Exception JavaDoc {
84         String JavaDoc targetFileName = target + entry.getName();
85         
86         if (entry.isDirectory()) {
87             File JavaDoc dir = new File JavaDoc(targetFileName);
88             dir.mkdirs();
89         } else {
90             File JavaDoc file = new File JavaDoc(targetFileName);
91             File JavaDoc dir = new File JavaDoc(file.getParent());
92             dir.mkdirs();
93             dir = null;
94             file = null;
95             
96             InputStream JavaDoc in = zip.getInputStream(entry);
97             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(targetFileName));
98             byte[] buffer = new byte[1024];
99             int len;
100             while ((len = in.read(buffer)) > 0) {
101                 bos.write(buffer, 0, len);
102             }
103             in.close();
104             bos.close();
105         }
106     }
107     
108     private static void logError(String JavaDoc message) {
109         Logger logger = LoggingManager.getLogger(org.contineo.core.ZipBean.class);
110         if (logger.isEnabledFor(Level.ERROR))
111             logger.error(message);
112     }
113 }
114
115
Popular Tags