001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.core.io;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import java.util.zip.ZipEntry;
025: import java.util.zip.ZipInputStream;
026:
027: /**
028: * Zip archive operations are handled by this class.
029: *
030: * @author fdietz
031: */
032: public class ZipFileIO {
033:
034: /**
035: * No instances needed.
036: */
037: private ZipFileIO() {
038: // don't instantiate this class
039: }
040:
041: /**
042: * Extract zip file to destination folder.
043: *
044: * @param file
045: * zip file to extract
046: * @param destination
047: * destinatin folder
048: */
049: public static void extract(File file, File destination)
050: throws IOException {
051: ZipInputStream in = null;
052: OutputStream out = null;
053: try {
054: // Open the ZIP file
055: in = new ZipInputStream(new FileInputStream(file));
056:
057: // Get the first entry
058: ZipEntry entry = null;
059:
060: while ((entry = in.getNextEntry()) != null) {
061: String outFilename = entry.getName();
062:
063: // Open the output file
064: if (entry.isDirectory()) {
065: new File(destination, outFilename).mkdirs();
066: } else {
067: out = new FileOutputStream(new File(destination,
068: outFilename));
069:
070: // Transfer bytes from the ZIP file to the output file
071: byte[] buf = new byte[1024];
072: int len;
073:
074: while ((len = in.read(buf)) > 0) {
075: out.write(buf, 0, len);
076: }
077:
078: // Close the stream
079: out.close();
080: }
081: }
082: } finally {
083: // Close the stream
084: if (in != null) {
085: in.close();
086: }
087: if (out != null) {
088: out.close();
089: }
090: }
091: }
092:
093: /**
094: * Return the first directory of this archive. This is needed to determine
095: * the plugin directory.
096: *
097: * @param zipFile
098: * @return <class>File</class> containing the first entry of this archive
099: */
100: public static File getFirstFile(File zipFile) throws IOException {
101: ZipInputStream in = null;
102: try {
103: // Open the ZIP file
104: in = new ZipInputStream(new FileInputStream(zipFile));
105:
106: // Get the first entry
107: ZipEntry entry = null;
108:
109: while ((entry = in.getNextEntry()) != null) {
110: String outFilename = entry.getName();
111:
112: if (entry.isDirectory()) {
113: return new File(outFilename);
114: }
115: }
116: } finally {
117: if (in != null) {
118: // Close the stream
119: in.close();
120: }
121: }
122: return null;
123: }
124: }
|