1 10 11 package analyzer.unzip; 12 13 import java.util.zip.*; 14 import java.io.*; 15 16 20 public class Unzipper { 21 22 23 public Unzipper() { 24 } 25 26 public static void unzip(String path){ 27 String sep=System.getProperty("file.separator"); 28 try{ 29 File dir = new File(path); 30 FilenameFilter filter = new FilenameFilter() { 31 public boolean accept(File dir, String name) { 32 return name.endsWith(".zip"); 33 } 34 }; 35 String zips[]=dir.list(filter); 36 if(zips==null||zips.length==0)return; 37 38 for(int i=0;i<zips.length;i++){ 39 int dotIndex=zips[i].lastIndexOf("."); 40 String dirName=zips[i].substring(0, dotIndex); 41 String filePath=path.equals(".")?"":path+sep; 42 System.out.println("filePath:"+filePath); 43 File f=new File(filePath+dirName); 44 f.mkdir(); 45 InputStream in = new BufferedInputStream(new FileInputStream(filePath+zips[i])); 46 ZipInputStream zin = new ZipInputStream(in); 47 ZipEntry e; 48 while((e=zin.getNextEntry())!= null) { 49 unzip(zin, filePath+dirName+sep+e.getName()); 50 } 51 zin.close(); 52 if(dirName.endsWith("user")){ 53 System.out.println("recursion:"+filePath+" "+dirName); 54 unzip(filePath+dirName); 55 }else{ 56 File delete=new File(filePath+zips[i]); 57 delete.delete(); 58 } 59 } 60 }catch(Exception e){ 61 System.out.println("exception while unzipping"); 62 e.printStackTrace(); 63 } 64 } 65 66 public static void unzip(ZipInputStream zin, String s) throws IOException { 67 System.out.println("unzipping " + s); 68 FileOutputStream out = new FileOutputStream(s); 69 byte [] b = new byte[512]; 70 int len = 0; 71 while ( (len=zin.read(b))!= -1 ) { 72 out.write(b,0,len); 73 } 74 out.close(); 75 } 76 77 } 78 | Popular Tags |