1 6 7 package org.contineo.core; 8 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.File ; 12 import java.io.FileOutputStream ; 13 import java.util.Enumeration ; 14 import java.util.jar.JarEntry ; 15 import java.util.jar.JarFile ; 16 17 import org.apache.log4j.Level; 18 import org.apache.log4j.Logger; 19 24 public class JarBean { 25 26 32 public static boolean unjar(String jarsource, String target) { 33 boolean result = true; 34 try { 35 if(!target.endsWith("/")) 36 target = target + "/"; 37 JarFile jar = new JarFile (jarsource); 38 JarEntry jare; 39 for(Enumeration enum1 = jar.entries(); enum1.hasMoreElements(); saveEntry(jar, jare, target)) 40 jare = (JarEntry )enum1.nextElement(); 41 } 42 catch (Exception e) { 43 result = false; 44 logError(e.getMessage()); 45 } 46 return result; 47 } 48 49 56 public static boolean unjar(String jarsource, String target, String entry) { 57 boolean result = true; 58 try { 59 if(!target.endsWith("/")) 60 target = target + "/"; 61 JarFile jar = new JarFile (jarsource); 62 JarEntry jare = new JarEntry (entry); 63 saveEntry(jar, jare, target); 64 } 65 catch (Exception e) { 66 result = false; 67 logError(e.getMessage()); 68 } 69 return result; 70 } 71 72 private static void saveEntry(JarFile jar, JarEntry jare, String target) throws Exception { 73 File file = new File (target + jare.getName()); 74 if(jare.isDirectory()) { 75 file.mkdirs(); 76 } 77 else { 78 java.io.InputStream is = jar.getInputStream(jare); 79 BufferedInputStream bis = new BufferedInputStream (is); 80 File dir = new File (file.getParent()); 81 dir.mkdirs(); 82 FileOutputStream fos = new FileOutputStream (file); 83 BufferedOutputStream bos = new BufferedOutputStream (fos); 84 for(int letter = 0; (letter = bis.read()) != -1;) 85 bos.write((byte)letter); 86 bos.close(); 87 fos.close(); 88 bis.close(); 89 } 90 } 91 92 private static void logError(String message) { 93 Logger logger = LoggingManager.getLogger(org.contineo.core.JarBean.class); 94 if (logger.isEnabledFor(Level.ERROR)) 95 logger.error(message); 96 } 97 } 98 | Popular Tags |