1 23 package com.sun.enterprise.tools.admingui.util; 24 25 import java.util.jar.JarInputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.util.zip.ZipEntry ; 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 33 public class JarExtract { 34 35 public static void extract(String fileName, String dir) throws IOException { 36 if(fileName == null || dir == null ) { 37 throw new IOException ("extract method: dir, or fileName is null"); 39 } 40 File f = new File (fileName); 41 if(!f.exists()) { 42 throw new IOException (fileName + ": file not found"); 44 } 45 FileInputStream fin = new FileInputStream (f); 46 JarInputStream jin = new JarInputStream (fin); 47 ZipEntry e; 48 49 while((e =jin.getNextEntry()) != null ) { 50 extract(jin, e, dir); 51 } 52 53 } 54 55 private static void extract(JarInputStream jin, ZipEntry e, String dir) 56 throws IOException { 57 58 File f = new File (dir + File.separatorChar + e.getName().replace('/', File.separatorChar)); 59 if (e.isDirectory()) { 60 if (!f.exists() && !f.mkdirs() || !f.isDirectory()) { 61 throw new IOException (f + ": could not create directory"); 63 } 64 } else { 65 if (f.getParent() != null) { 66 File d = new File (f.getParent()); 67 if (!d.exists() && !d.mkdirs() || !d.isDirectory()) { 68 throw new IOException (d + ": could not create directory"); 70 } 71 } 72 OutputStream os = new FileOutputStream (f); 73 byte[] b = new byte[512]; 74 int len; 75 while ((len = jin.read(b, 0, b.length)) != -1) { 76 os.write(b, 0, len); 77 } 78 jin.closeEntry(); 79 os.close(); 80 } 81 82 } 83 84 } 85 | Popular Tags |