001: package org.contineo.core;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.File;
005: import java.io.FileOutputStream;
006: import java.io.InputStream;
007: import java.util.Enumeration;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.apache.tools.zip.ZipEntry;
012: import org.apache.tools.zip.ZipFile;
013:
014: /**
015: * This class is for handling with zip-files.
016: *
017: * @author Michael Scholz
018: * @version 1.0
019: */
020: public class ZipBean {
021: /**
022: * This method extracts all entries of a zip-file.
023: *
024: * @param zipsource Path of the zip-file.
025: * @param target Path of the extracted files.
026: * @return True if successfully extracted.
027: */
028: public static boolean unzip(String zipsource, String target) {
029: boolean result = true;
030: try {
031: if (!target.endsWith("/"))
032: target = target + "/";
033: ZipFile zip = new ZipFile(zipsource);
034: Enumeration entries = zip.getEntries();
035:
036: while (entries.hasMoreElements()) {
037: ZipEntry entry = (ZipEntry) entries.nextElement();
038: saveEntry(target, zip, entry);
039: }
040:
041: zip.close();
042: } catch (Exception e) {
043: result = false;
044: logError(e.getMessage());
045: }
046: return result;
047: }
048:
049: /**
050: * This method extracts one entry of a zip-file.
051: *
052: * @param zipsource Path of the zip-file.
053: * @param target Path of the extracted files.
054: * @param entry Name of the entry to be extracted.
055: * @return True if successfully extracted.
056: */
057: public static boolean unzip(String zipsource, String target,
058: String entry) {
059: boolean result = true;
060:
061: try {
062: if (!target.endsWith("/")) {
063: target = target + "/";
064: }
065:
066: ZipFile zip = new ZipFile(zipsource);
067: ZipEntry zipe = new ZipEntry(entry);
068: saveEntry(target, zip, zipe);
069: } catch (Exception e) {
070: result = false;
071: logError(e.getMessage());
072: }
073:
074: return result;
075: }
076:
077: /**
078: * Extracts an entry from a zip file to a target directory.
079: *
080: * @param target the base directory the entry should be extracted to
081: * @param zip the ZIP file
082: * @param entry the to be extracted entry in the ZIP file
083: */
084: private static void saveEntry(String target, ZipFile zip,
085: ZipEntry entry) throws Exception {
086: String targetFileName = target + entry.getName();
087:
088: if (entry.isDirectory()) {
089: File dir = new File(targetFileName);
090: dir.mkdirs();
091: } else {
092: File file = new File(targetFileName);
093: File dir = new File(file.getParent());
094: dir.mkdirs();
095: dir = null;
096: file = null;
097:
098: InputStream in = zip.getInputStream(entry);
099: BufferedOutputStream bos = new BufferedOutputStream(
100: new FileOutputStream(targetFileName));
101: byte[] buffer = new byte[1024];
102: int len;
103: while ((len = in.read(buffer)) > 0) {
104: bos.write(buffer, 0, len);
105: }
106: in.close();
107: bos.close();
108: }
109: }
110:
111: private static void logError(String message) {
112: Log logger = LogFactory.getLog(ZipBean.class);
113: logger.error(message);
114: }
115: }
|