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