001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ZipFactory.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.internal.support;
030:
031: import java.io.FileOutputStream;
032: import java.io.InputStream;
033: import java.io.IOException;
034:
035: import java.util.Enumeration;
036: import java.util.zip.ZipEntry;
037: import java.util.zip.ZipException;
038: import java.util.zip.ZipFile;
039: import java.io.File;
040:
041: public class ZipFactory {
042:
043: /* Buffer to read data
044: *
045: */
046: private byte[] mBuffer;
047:
048: /* Unzip Directory
049: *
050: */
051: private String mDir;
052:
053: /**
054: * This object provides utility methods to unzip files.
055: * @param dir Directory to unzip
056: */
057: public ZipFactory(String dir) {
058:
059: this .mDir = dir;
060: this .mBuffer = new byte[8092];
061:
062: }
063:
064: /**
065: * Unzips a file.
066: *
067: * @param zpFile File to be unzipped
068: * @throws IOException if error trying to read entry.
069: * @throws java.util.zip.ZipException if error trying to read zip file.
070: */
071: public void unZip(File zpFile) throws IOException, ZipException {
072:
073: if (!zpFile.getName().endsWith(".zip")) {
074: throw new ZipException("Not a zip file? "
075: + zpFile.getName());
076: }
077:
078: // process all entries in that JAR file
079: ZipFile zp = new ZipFile(zpFile);
080: Enumeration all = zp.entries();
081: while (all.hasMoreElements()) {
082: getEntry(zp, ((ZipEntry) (all.nextElement())));
083: }
084:
085: zp.close();
086: }
087:
088: /**
089: * Gets one file <code>entry</code> from <code>zpFile</code>.
090: *
091: * @param zp the ZIP file reference to retrieve <code>entry</code> from.
092: * @param entry the file from the ZIP to extract.
093: * @throws IOException if error trying to read entry.
094: */
095: private void getEntry(ZipFile zp, ZipEntry entry)
096: throws IOException {
097:
098: String entryName = entry.getName();
099: // if a directory, mkdir it (remember to
100: // create intervening subdirectories if needed!)
101: if (entryName.endsWith("/")) {
102: new File(mDir, entryName).mkdirs();
103: return;
104: }
105:
106: File f = new File(mDir, entryName);
107:
108: if (!f.getParentFile().exists()) {
109: f.getParentFile().mkdirs();
110: }
111:
112: // Must be a file; create output stream to the file
113: FileOutputStream fostream = new FileOutputStream(f);
114: InputStream istream = zp.getInputStream(entry);
115:
116: // extract files
117: int n = 0;
118: while ((n = istream.read(mBuffer)) > 0) {
119: fostream.write(mBuffer, 0, n);
120: }
121:
122: try {
123: istream.close();
124: fostream.close();
125: } catch (IOException e) {
126: e.printStackTrace();
127: }
128: }
129:
130: }
|