1 package com.sslexplorer.util; 2 3 import java.io.File ; 4 import java.io.FileOutputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.OutputStream ; 8 import java.util.zip.ZipEntry ; 9 import java.util.zip.ZipInputStream ; 10 11 import com.sslexplorer.boot.Util; 12 13 14 public class ZipExtract { 15 16 public static final void copyInputStream(InputStream in, OutputStream out) 17 throws IOException 18 { 19 byte[] buffer = new byte[1024]; 20 int len; 21 22 while((len = in.read(buffer)) >= 0) 23 out.write(buffer, 0, len); 24 25 in.close(); 26 out.close(); 27 } 28 29 30 public static final void extractZipFile(File basedir, InputStream in) throws IOException { 31 extractZipFile(basedir, in, true); 32 } 33 34 public static final void extractZipFile(File basedir, InputStream in, boolean onlyIfNewer) throws IOException { 35 36 ZipInputStream zin = new ZipInputStream (in); 37 try { 38 39 40 ZipEntry entry; 41 byte[] buf = new byte[32768]; 42 int read; 43 do { 44 entry = zin.getNextEntry(); 45 46 if(entry==null) 47 break; 48 49 File f = new File (basedir, entry.getName()); 50 51 if(entry.isDirectory()) { 52 f.mkdirs(); 53 zin.closeEntry(); 54 if(entry.getTime() != -1) { 55 f.setLastModified(entry.getTime()); 56 } 57 continue; 58 } 59 60 if(onlyIfNewer && entry.getTime() != -1 && entry.getTime() == f.lastModified()) { 61 continue; 62 } 63 64 f.getParentFile().mkdirs(); 65 FileOutputStream out = new FileOutputStream (f); 66 67 try { 68 while((read = zin.read(buf, 0, buf.length)) > -1) { 69 out.write(buf, 0, read); 70 } 71 72 zin.closeEntry(); 73 74 } finally { 75 Util.closeStream(out); 76 } 77 if(entry.getTime() != -1) { 78 f.setLastModified(entry.getTime()); 79 } 80 81 } while(entry!=null); 82 83 } finally { 84 Util.closeStream(zin); 85 } 86 } 87 88 } 89 90 | Popular Tags |