1 package de.jwi.zip; 2 3 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.util.Date ; 32 import java.util.zip.ZipEntry ; 33 import java.util.zip.ZipInputStream ; 34 35 40 public class Unzipper 41 { 42 private static final int BUFSIZE = 1024; 43 44 public static void unzip(InputStream is, File targetDir) throws FileNotFoundException , IOException 45 { 46 ZipEntry entry; 47 ZipInputStream zis = new ZipInputStream (is); 48 byte[] buf = new byte[BUFSIZE]; 49 50 if (!targetDir.exists()) 51 { 52 throw new FileNotFoundException (targetDir.toString() + " does not exist."); 53 } 54 55 if (!targetDir.isDirectory()) 56 { 57 throw new FileNotFoundException (targetDir.toString() + " is not a directory."); 58 } 59 60 while ((entry = zis.getNextEntry()) != null) 61 { 62 String name = entry.getName(); 63 64 long size = entry.getSize(); 65 66 long time = entry.getTime(); 67 time = (time != -1) ? time : new Date ().getTime(); 68 69 File f = new File (targetDir,name); 70 71 72 73 if (entry.isDirectory()) 74 { 75 f.mkdirs(); 76 } 77 else 78 { 79 f.getParentFile().mkdirs(); 80 81 FileOutputStream fos = new FileOutputStream (f); 82 int len; 83 while ((len = zis.read(buf,0,BUFSIZE)) > 0) 84 { 85 fos.write(buf,0,len); 86 size-=len; 87 } 88 fos.close(); 89 } 90 91 93 f.setLastModified(time); 94 95 zis.closeEntry(); 96 97 } 98 zis.close(); 99 } 100 101 public static void main(String [] args) throws Exception 102 { 103 FileInputStream fis = new FileInputStream (args[0]); 104 unzip(fis,new File (args[1])); 105 } 106 } 107 | Popular Tags |