1 6 7 package org.jfox.ioc.util; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.io.OutputStream ; 14 import java.net.URL ; 15 import java.net.URLClassLoader ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 import java.util.StringTokenizer ; 19 import java.util.jar.Attributes ; 20 import java.util.jar.JarFile ; 21 import java.util.jar.JarInputStream ; 22 import java.util.jar.Manifest ; 23 import java.util.zip.ZipEntry ; 24 25 28 29 public class JarUtils { 30 31 34 public static void extract(File jarFile, File dest) throws IOException { 35 if(!dest.exists()) { 36 dest.mkdirs(); 37 } 38 if(!dest.isDirectory()) { 39 throw new IOException ("Destination must be a directory."); 40 } 41 JarInputStream jin = new JarInputStream (new FileInputStream (jarFile)); 42 byte[] buffer = new byte[1024]; 43 44 ZipEntry entry = jin.getNextEntry(); 45 while(entry != null) { 46 String fileName = entry.getName(); 47 if(fileName.charAt(fileName.length() - 1) == '/') { 48 fileName = fileName.substring(0, fileName.length() - 1); 49 } 50 if(fileName.charAt(0) == '/') { 51 fileName = fileName.substring(1); 52 } 53 if(File.separatorChar != '/') { 54 fileName = fileName.replace('/', File.separatorChar); 55 } 56 File file = new File (dest, fileName); 57 if(entry.isDirectory()) { 58 file.mkdirs(); 60 jin.closeEntry(); 61 } 62 else { 63 File parent = file.getParentFile(); 65 if(parent != null && !parent.exists()) { 66 parent.mkdirs(); 67 } 68 69 OutputStream out = new FileOutputStream (file); 71 int len = 0; 72 while((len = jin.read(buffer, 0, buffer.length)) != -1) { 73 out.write(buffer, 0, len); 74 } 75 out.flush(); 76 out.close(); 77 jin.closeEntry(); 78 file.setLastModified(entry.getTime()); 79 } 80 entry = jin.getNextEntry(); 81 } 82 85 Manifest mf = jin.getManifest(); 86 if(mf != null) { 87 File file = new File (dest, "META-INF/MANIFEST.MF"); 88 File parent = file.getParentFile(); 89 if(parent.exists() == false) { 90 parent.mkdirs(); 91 } 92 OutputStream out = new FileOutputStream (file); 93 mf.write(out); 94 out.flush(); 95 out.close(); 96 } 97 jin.close(); 98 } 99 100 107 public static URL getDescriptor(URLClassLoader loader, String descriptorName) { 108 return loader.findResource(descriptorName); 109 } 110 111 public static Manifest getManifest(File jarFile) throws IOException { 112 return new JarFile (jarFile).getManifest(); 113 } 114 115 public static String [] getClassPathFromManifest(File jarFile) throws IOException { 116 Manifest mani = getManifest(jarFile); 117 Attributes attrs = mani.getMainAttributes(); 118 String cpString = attrs.getValue("Class-Path"); 119 if(cpString == null || cpString.trim().equals("")) { 120 return new String [0]; 121 } 122 else { 123 StringTokenizer st = new StringTokenizer (cpString); 124 List cpList = new ArrayList (); 125 while(st.hasMoreTokens()) { 126 cpList.add(st.nextToken()); 127 } 128 return (String []) cpList.toArray(new String [cpList.size()]); 129 } 130 } 131 132 public static void main(String [] args) { 133 134 } 135 } 136 137 138 139 | Popular Tags |