001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.registry.util;
043:
044: /**
045: *
046: * @author Winston Prakash
047: */
048:
049: import java.util.*;
050: import java.util.jar.*;
051: import java.io.*;
052:
053: public class JarUtil {
054: JarFile jar;
055: File jarFile;
056:
057: public JarUtil(File file) {
058: jarFile = file;
059: }
060:
061: // public void list(){
062: // try{
063: // jar = new JarFile(jarFile);
064: // for(Enumeration listEntries = jar.entries(); listEntries.hasMoreElements();){
065: //// System.out.println(listEntries.nextElement());
066: // }
067: // }catch(IOException exc){
068: // exc.printStackTrace();
069: // }
070: // }
071:
072: public void extract(File toDir) {
073: try {
074: jar = new JarFile(jarFile);
075: for (Enumeration extractEntries = jar.entries(); extractEntries
076: .hasMoreElements();) {
077: Object element = extractEntries.nextElement();
078: String name = element.toString();
079: JarEntry current = jar.getJarEntry(name);
080:
081: if (name.endsWith("/")) {
082: File dir = new File(toDir, name);
083: dir.mkdirs();
084: } else {
085: InputStream in = jar.getInputStream(current);
086: FileOutputStream fout = new FileOutputStream(toDir
087: .getPath()
088: + "/" + name);
089: int read = in.read();
090: while (read != -1) {
091: fout.write(read);
092: read = in.read();
093: }
094: in.close();
095: fout.close();
096: }
097: }
098: } catch (IOException exc) {
099: exc.printStackTrace();
100: }
101: }
102:
103: public void extract(String name, File toFile) {
104: try {
105: jar = new JarFile(jarFile);
106: File parentFile = toFile.getParentFile();
107: if (!parentFile.exists()) {
108: if (!parentFile.mkdirs())
109: return;
110: }
111: JarEntry current = jar.getJarEntry(name);
112: /**
113: * If current is null, we didn't find the entry.
114: */
115: if (null == current) {
116: return;
117: }
118: InputStream in = jar.getInputStream(current);
119: FileOutputStream fout = new FileOutputStream(toFile
120: .getPath());
121: int read = in.read();
122: while (read != -1) {
123: fout.write(read);
124: read = in.read();
125: }
126: in.close();
127: fout.close();
128: } catch (IOException exc) {
129: exc.printStackTrace();
130: }
131: }
132:
133: /**
134: * Open a file and get the BuffrededReader of the input stream
135: */
136: public BufferedReader openFile(String name) {
137: try {
138: jar = new JarFile(jarFile);
139: JarEntry current = jar.getJarEntry(name);
140:
141: // If current is null, we didn't find the entry.
142: if (null == current) {
143: return null;
144: }
145: InputStream in = jar.getInputStream(current);
146: BufferedReader buffReader = new BufferedReader(
147: new InputStreamReader(in));
148: return buffReader;
149: } catch (IOException exc) {
150: exc.printStackTrace();
151: }
152: return null;
153: }
154:
155: public void addDirectory(File fromDir) {
156: try {
157: // !PW IZ 48680 Make sure the directory for this jar file exists or this operation
158: // will fail. Note the jar file does not need (and is not likely) to exist.
159: if (!jarFile.exists() && jarFile.getParentFile() != null) {
160: jarFile.getParentFile().mkdirs();
161: }
162: JarOutputStream jarout = new JarOutputStream(
163: new FileOutputStream(jarFile));
164: File files[] = fromDir.listFiles();
165: for (int i = 0; i < files.length; i++) {
166: if (files[i].isDirectory()) {
167: String name = files[i].getName() + "/";
168: JarEntry directory = new JarEntry(name);
169: jarout.putNextEntry(directory);
170: addDirectory(files[i], files[i].getName(), jarout);
171: } else {
172: addFile(files[i], files[i].getName(), jarout);
173: }
174: }
175: jarout.close();
176: } catch (java.io.IOException ioe) {
177: ioe.printStackTrace();
178: }
179: }
180:
181: public void addDirectory(File dir, final String parentIn,
182: JarOutputStream jarout) {
183: String parent = parentIn;
184: try {
185: File files[] = dir.listFiles();
186: for (int i = 0; i < files.length; i++) {
187: if (files[i].isDirectory()) {
188: String name;
189: String parentName = parent;
190: if (!parentName.equals("")) {
191: name = parentName + "/" + files[i].getName()
192: + "/";
193: parentName = parentName + "/"
194: + files[i].getName();
195: } else {
196: name = files[i].getName() + "/";
197: parentName = files[i].getName();
198: }
199: JarEntry directory = new JarEntry(name);
200: jarout.putNextEntry(directory);
201: addDirectory(files[i], parentName, jarout);
202: } else {
203: String name;
204: String parentName = parent;
205: if (!parentName.equals("")) {
206: name = parentName + "/" + files[i].getName();
207: } else {
208: name = files[i].getName();
209: }
210: addFile(files[i], name, jarout);
211: }
212: }
213: } catch (java.io.IOException ioe) {
214: ioe.printStackTrace();
215: }
216: }
217:
218: public void addFile(File file, String name, JarOutputStream jarout) {
219: try {
220: JarEntry entry = new JarEntry(name);
221: jarout.putNextEntry(entry);
222: FileInputStream fin = new FileInputStream(file);
223: int read = fin.read();
224: while (read != -1) {
225: jarout.write(read);
226: read = fin.read();
227: }
228: fin.close();
229: } catch (java.io.IOException ioe) {
230: ioe.printStackTrace();
231: }
232: }
233:
234: public static void main(String args[]) {
235: File jarFile = new File("D:\\wsdl2java\\webservice.jar");
236: if (jarFile.exists())
237: jarFile.delete();
238: JarUtil jarUtil = new JarUtil(jarFile);
239: jarUtil.addDirectory(new File("D:\\wsdl2java\\classes"));
240: jarUtil
241: .extract(
242: "www/xmethods/net/TemperaturePortTypeClient.java",
243: new File(
244: "D:\\wsdl2java\\TemperaturePortTypeClient.java"));
245: jarUtil.extract("www/xmethods/net/TemperaturePortType.class",
246: new File("D:\\wsdl2java\\TemperaturePortType.class"));
247: jarUtil.extract(new File("D:\\wsdl2java\\tmp"));
248: }
249: }
|