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: * @(#)JarFactory.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.jar.JarException;
037: import java.io.File;
038: import java.io.FileInputStream;
039: import java.util.jar.JarInputStream;
040:
041: import java.util.zip.ZipFile;
042: import java.util.zip.ZipEntry;
043: import java.util.zip.ZipException;
044: import java.util.zip.ZipInputStream;
045:
046: public class JarFactory {
047:
048: /* Buffer to read data
049: *
050: */
051: private byte[] mBuffer;
052:
053: /* Unzip Directory
054: *
055: */
056: private String mDir;
057:
058: /**
059: * This object provides utility methods to manipulate DOM tree.
060: * @param dir Directory to unjar
061: */
062: public JarFactory(String dir) {
063:
064: this .mDir = dir;
065: this .mBuffer = new byte[8092];
066:
067: }
068:
069: /**
070: * Unjars a file.
071: *
072: * @param jarFile File to be unjared
073: * @throws IOException if error trying to read entry.
074: * @throws java.util.jar.JarException if error trying to read jar file.
075: */
076: public void unJar(File jarFile) throws IOException, JarException,
077: ZipException {
078: // process all entries in that JAR file
079: FileInputStream fis = new FileInputStream(jarFile);
080: JarInputStream jis = new JarInputStream(fis);
081: unJar(jis);
082: jis.close();
083: }
084:
085: /**
086: * Unjars a zip file using it's InputStream.
087: *
088: * @param zis zip input stream
089: * @throws IOException if error trying to read entry.
090: * @throws java.util.jar.JarException if error trying to read jar file.
091: */
092: public void unJar(java.util.zip.ZipInputStream zis)
093: throws IOException, ZipException {
094:
095: // process all entries
096: ZipEntry zipEntry = zis.getNextEntry();
097: while (zipEntry != null) {
098: getEntry(zis, zipEntry);
099: zipEntry = zis.getNextEntry();
100: }
101: }
102:
103: /**
104: * Gets one file entry from the zip input stream
105: *
106: * @param jarFile the JAR file reference to retrieve <code>entry</code> from.
107: * @param entry the file from the JAR to extract.
108: * @return the ZipEntry name
109: * @throws IOException if error trying to read entry.
110: */
111: public String getEntry(ZipInputStream zis, ZipEntry entry)
112: throws IOException {
113:
114: String entryName = entry.getName();
115: // if a directory, mkdir it (remember to create
116: // intervening subdirectories if needed!)
117: if (entryName.endsWith("/")) {
118: new File(mDir, entryName).mkdirs();
119: return entryName;
120: }
121:
122: File f = new File(mDir, entryName);
123:
124: if (!f.getParentFile().exists()) {
125: f.getParentFile().mkdirs();
126: }
127:
128: // Must be a file; create output stream to the file
129: FileOutputStream fostream = new FileOutputStream(f);
130:
131: // extract files
132: int n = 0;
133: while ((n = zis.read(mBuffer)) > 0) {
134: fostream.write(mBuffer, 0, n);
135: }
136:
137: try {
138: // istream.close ();
139: fostream.close();
140: } catch (IOException e) {
141: // do nothing
142: }
143: return entryName;
144: }
145: }
|