KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > util > JarUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.registry.util;
21
22 /**
23  *
24  * @author Winston Prakash
25  */

26
27 import java.util.*;
28 import java.util.jar.*;
29 import java.io.*;
30
31 public class JarUtil {
32     JarFile jar;
33     File jarFile;
34     public JarUtil(File file) {
35         jarFile = file;
36     }
37
38 // public void list(){
39
// try{
40
// jar = new JarFile(jarFile);
41
// for(Enumeration listEntries = jar.entries(); listEntries.hasMoreElements();){
42
//// System.out.println(listEntries.nextElement());
43
// }
44
// }catch(IOException exc){
45
// exc.printStackTrace();
46
// }
47
// }
48

49     public void extract(File toDir){
50         try{
51             jar = new JarFile(jarFile);
52             for(Enumeration extractEntries = jar.entries(); extractEntries.hasMoreElements();) {
53                 Object JavaDoc element = extractEntries.nextElement();
54                 String JavaDoc name = element.toString();
55                 JarEntry current = jar.getJarEntry(name);
56                 
57                 if(name.endsWith("/")){
58                     File dir = new File(toDir, name);
59                     dir.mkdirs();
60                 } else {
61                     InputStream in = jar.getInputStream(current);
62                     FileOutputStream fout = new FileOutputStream(toDir.getPath() + "/" + name);
63                     int read = in.read();
64                     while(read != -1){
65                         fout.write(read);
66                         read = in.read();
67                     }
68                     in.close();
69                     fout.close();
70                 }
71             }
72         }catch(IOException exc){
73             exc.printStackTrace();
74         }
75     }
76     
77     public void extract(String JavaDoc name, File toFile){
78         try{
79             jar = new JarFile(jarFile);
80             File parentFile = toFile.getParentFile();
81             if(!parentFile.exists()) {
82                 if (!parentFile.mkdirs()) return;
83             }
84             JarEntry current = jar.getJarEntry(name);
85             /**
86              * If current is null, we didn't find the entry.
87              */

88             if(null ==current) {
89                 return;
90             }
91             InputStream in = jar.getInputStream(current);
92             FileOutputStream fout = new FileOutputStream(toFile.getPath());
93             int read = in.read();
94             while(read != -1){
95                 fout.write(read);
96                 read = in.read();
97             }
98             in.close();
99             fout.close();
100         }catch(IOException exc){
101             exc.printStackTrace();
102         }
103     }
104     
105     /**
106      * Open a file and get the BuffrededReader of the input stream
107      */

108     public BufferedReader openFile(String JavaDoc name){
109         try{
110             jar = new JarFile(jarFile);
111             JarEntry current = jar.getJarEntry(name);
112       
113             // If current is null, we didn't find the entry.
114
if(null == current) {
115                 return null;
116             }
117             InputStream in = jar.getInputStream(current);
118             BufferedReader buffReader = new BufferedReader(new InputStreamReader(in));
119             return buffReader;
120         }catch(IOException exc){
121             exc.printStackTrace();
122         }
123         return null;
124     }
125     
126     public void addDirectory(File fromDir){
127         try {
128             // !PW IZ 48680 Make sure the directory for this jar file exists or this operation
129
// will fail. Note the jar file does not need (and is not likely) to exist.
130
if(!jarFile.exists() && jarFile.getParentFile() != null) {
131                 jarFile.getParentFile().mkdirs();
132             }
133             JarOutputStream jarout = new JarOutputStream(new FileOutputStream(jarFile));
134             File files[] = fromDir.listFiles();
135             for(int i = 0; i < files.length; i++) {
136                 if(files[i].isDirectory()) {
137                     String JavaDoc name = files[i].getName() + "/";
138                     JarEntry directory = new JarEntry(name);
139                     jarout.putNextEntry(directory);
140                     addDirectory(files[i], files[i].getName(), jarout);
141                 }else {
142                     addFile(files[i], files[i].getName(), jarout);
143                 }
144             }
145             jarout.close();
146         }catch(java.io.IOException JavaDoc ioe) {
147             ioe.printStackTrace();
148         }
149     }
150     
151     public void addDirectory(File dir, final String JavaDoc parentIn, JarOutputStream jarout) {
152         String JavaDoc parent = parentIn;
153         try {
154             File files[] = dir.listFiles();
155             for(int i = 0; i < files.length; i++) {
156                 if(files[i].isDirectory()) {
157                     String JavaDoc name;
158                     String JavaDoc parentName = parent;
159                     if(!parentName.equals("")){
160                         name = parentName + "/" + files[i].getName() + "/";
161                         parentName = parentName + "/" + files[i].getName();
162                     }else{
163                         name = files[i].getName() + "/";
164                         parentName = files[i].getName();
165                     }
166                     JarEntry directory = new JarEntry(name);
167                     jarout.putNextEntry(directory);
168                     addDirectory(files[i], parentName, jarout);
169                 }else {
170                     String JavaDoc name;
171                     String JavaDoc parentName = parent;
172                     if(!parentName.equals("")){
173                         name = parentName + "/" + files[i].getName();
174                     }else{
175                         name = files[i].getName();
176                     }
177                     addFile(files[i], name, jarout);
178                 }
179             }
180         } catch(java.io.IOException JavaDoc ioe) {
181             ioe.printStackTrace();
182         }
183     }
184     
185     public void addFile(File file, String JavaDoc name, JarOutputStream jarout){
186         try {
187             JarEntry entry = new JarEntry(name);
188             jarout.putNextEntry(entry);
189             FileInputStream fin = new FileInputStream(file);
190             int read = fin.read();
191             while(read != -1) {
192                 jarout.write(read);
193                 read = fin.read();
194             }
195             fin.close();
196         } catch(java.io.IOException JavaDoc ioe) {
197             ioe.printStackTrace();
198         }
199     }
200     
201     public static void main(String JavaDoc args[]) {
202         File jarFile = new File("D:\\wsdl2java\\webservice.jar");
203         if(jarFile.exists()) jarFile.delete();
204         JarUtil jarUtil = new JarUtil(jarFile);
205         jarUtil.addDirectory(new File("D:\\wsdl2java\\classes"));
206         jarUtil.extract("www/xmethods/net/TemperaturePortTypeClient.java", new File("D:\\wsdl2java\\TemperaturePortTypeClient.java"));
207         jarUtil.extract("www/xmethods/net/TemperaturePortType.class", new File("D:\\wsdl2java\\TemperaturePortType.class"));
208         jarUtil.extract(new File("D:\\wsdl2java\\tmp"));
209     }
210 }
211
Popular Tags