KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piaget > 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 org.netbeans.modules.piaget.unzip;
12
13 import java.util.zip.*;
14 import java.io.*;
15
16 /**
17  *
18  * @author loicsegapelli
19  */

20 public class Unzipper {
21     
22     static String JavaDoc sep = System.getProperty("file.separator");
23     
24     public static String JavaDoc unzipUserFile(String JavaDoc path){
25         try{
26             // first create the output directory
27
int dotIndex = path.lastIndexOf(".");
28             String JavaDoc dirPathname = path.substring(0, dotIndex);
29             int sepIndex = path.lastIndexOf(sep);
30             String JavaDoc dirName = dirPathname.substring(sepIndex+1);
31             File user = new File(dirPathname);
32             user.mkdir();
33             
34             //unzip content of user file, = directories
35
InputStream in = new BufferedInputStream(new FileInputStream(path));
36             ZipInputStream zin = new ZipInputStream(in);
37             ZipEntry e;
38             while((e = zin.getNextEntry())!= null) {
39                 unzip(zin, dirPathname + sep + e.getName());
40             }
41             zin.close();
42             
43             // now unzip the content of each directory, recursively
44
unzipDir(dirPathname, dirName);
45             
46             return user.getAbsolutePath();
47         }catch(Exception JavaDoc e){
48             System.out.println("exception while unzipping");
49             System.out.println(e.getMessage());
50             StackTraceElement JavaDoc el[] = e.getStackTrace();
51             for(int i = 0;i<el.length;i++){
52                 System.out.println(el[i].toString());
53             }
54             return null;
55         }
56         
57         
58     }
59     
60     private static void unzipDir(String JavaDoc path, String JavaDoc toRemove){
61         try{
62             // get all zip files in this directory
63
File dir = new File(path);
64             FilenameFilter filter = new FilenameFilter() {
65                 public boolean accept(File dir, String JavaDoc name) {
66                     return name.endsWith(".zip");
67                 }
68             };
69             String JavaDoc zip [];
70             zip = dir.list(filter);
71             
72             if(zip == null||zip.length == 0){
73                 return;
74             }
75             
76             for(int i = 0;i<zip.length;i++){
77                 int dotIndex = zip[i].lastIndexOf(".");
78                 String JavaDoc dirName = zip[i].substring(0, dotIndex);
79                 dirName = dirName.replaceFirst(toRemove+".", "");
80                 String JavaDoc filePath = path.equals(".") ? "" : path+sep;
81                 File f = new File(filePath+dirName);
82                 f.mkdir();
83                 InputStream in = new BufferedInputStream(new FileInputStream(filePath+zip[i]));
84                 ZipInputStream zin = new ZipInputStream(in);
85                 ZipEntry e;
86                 while((e = zin.getNextEntry()) != null) {
87                     String JavaDoc outName = e.getName().replaceFirst(toRemove+".", "");
88                     unzip(zin, filePath+dirName+sep+outName);
89                 }
90                 zin.close();
91                 
92                 // recursion
93
unzipDir(filePath+dirName, toRemove);
94                 
95                 File delete = new File(filePath+zip[i]);
96                 delete.delete();
97             }
98         }catch(Exception JavaDoc e){
99             e.printStackTrace(System.out);
100         }
101         
102     }
103     
104
105     
106     public static void unzip(ZipInputStream zin, String JavaDoc filename) throws IOException {
107         //System.out.println("unzipping " + s);
108
FileOutputStream out = new FileOutputStream(filename);
109         byte [] b = new byte[512];
110         int len = 0;
111         while ( (len = zin.read(b))!= -1 ) {
112             out.write(b,0,len);
113         }
114         out.close();
115     }
116     
117 }
118
Popular Tags