1 19 20 package org.netbeans.modules.websvc.registry.util; 21 22 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 49 public void extract(File toDir){ 50 try{ 51 jar = new JarFile(jarFile); 52 for(Enumeration extractEntries = jar.entries(); extractEntries.hasMoreElements();) { 53 Object element = extractEntries.nextElement(); 54 String 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 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 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 108 public BufferedReader openFile(String name){ 109 try{ 110 jar = new JarFile(jarFile); 111 JarEntry current = jar.getJarEntry(name); 112 113 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 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 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 ioe) { 147 ioe.printStackTrace(); 148 } 149 } 150 151 public void addDirectory(File dir, final String parentIn, JarOutputStream jarout) { 152 String 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 name; 158 String 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 name; 171 String 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 ioe) { 181 ioe.printStackTrace(); 182 } 183 } 184 185 public void addFile(File file, String 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 ioe) { 197 ioe.printStackTrace(); 198 } 199 } 200 201 public static void main(String 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 |