KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > analyzer > unzip > Unzipper


1 /*
2  * Unzipper.java
3  *
4  * Created on May 28, 2005, 8:20 PM
5  *
6  * To change this template, choose Tools | Options and locate the template under
7  * the Source Creation and Management node. Right-click the template and choose
8  * Open. You can then make changes to the template in the Source Editor.
9  */

10
11 package analyzer.unzip;
12
13 import java.util.zip.*;
14 import java.io.*;
15
16 /**
17  *
18  * @author loicsegapelli
19  */

20 public class Unzipper {
21     
22     /** Creates a new instance of Unzipper */
23     public Unzipper() {
24     }
25     
26     public static void unzip(String JavaDoc path){
27         String JavaDoc 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 JavaDoc name) {
32                     return name.endsWith(".zip");
33                 }
34             };
35             String JavaDoc 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 JavaDoc dirName=zips[i].substring(0, dotIndex);
41                 String JavaDoc 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 JavaDoc e){
61             System.out.println("exception while unzipping");
62             e.printStackTrace();
63         }
64     }
65     
66     public static void unzip(ZipInputStream zin, String JavaDoc 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