1 23 24 package com.rift.coad.lib.common; 26 27 import com.rift.coad.lib.common.*; 29 import java.io.InputStream ; 30 import java.io.FileOutputStream ; 31 import java.io.File ; 32 import java.util.Enumeration ; 33 import java.util.jar.JarFile ; 34 import java.util.jar.JarEntry ; 35 36 41 public class JarUtil { 42 43 46 private JarUtil() { 47 } 48 49 50 56 public static void extract (File source, File destination) 57 throws CommonException { 58 try { 59 JarFile jarFile = new JarFile (source); 60 Enumeration entries = jarFile.entries(); 61 byte[] readBuffer = new byte[1024]; 62 while (entries.hasMoreElements()) { 63 JarEntry entry = (JarEntry )entries.nextElement(); 64 File path = new File (destination + File.separator 65 + entry.getName()); 66 if (entry.isDirectory()) { 67 if (!path.mkdirs()) { 68 throw new CommonException("Failed to create the dir [" + 69 path.getAbsolutePath() + "]"); 70 } 71 continue; 72 } 73 InputStream inputStream = 74 jarFile.getInputStream(entry); 75 FileOutputStream outputStream = new FileOutputStream (path); 76 int readBytes = 0; 77 while((readBytes = 78 inputStream.read(readBuffer,0,readBuffer.length)) != -1) { 79 outputStream.write(readBuffer,0,readBytes); 80 } 81 outputStream.flush(); 82 inputStream.close(); 83 outputStream.close(); 84 } 85 } catch (Exception ex) { 86 throw new CommonException("Failed to extract the file [" + 87 source.getAbsolutePath() + "] to [" + 88 destination.getAbsolutePath() + "] because :" + 89 ex.getMessage(),ex); 90 } 91 } 92 } 93 | Popular Tags |