KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > zip > Unzipper


1 package de.jwi.zip;
2
3 /*
4  * jFM - Java Web File Manager
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jFM.
9  *
10  * jFM is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jFM is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jFM; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import java.util.zip.ZipInputStream JavaDoc;
34
35 /**
36  * @author Jürgen Weber
37  * Source file created on 12.03.2004
38  *
39  */

40 public class Unzipper
41 {
42     private static final int BUFSIZE = 1024;
43     
44     public static void unzip(InputStream JavaDoc is, File JavaDoc targetDir) throws FileNotFoundException JavaDoc, IOException JavaDoc
45     {
46         ZipEntry JavaDoc entry;
47         ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(is);
48         byte[] buf = new byte[BUFSIZE];
49         
50         if (!targetDir.exists())
51         {
52             throw new FileNotFoundException JavaDoc(targetDir.toString() + " does not exist.");
53         }
54
55         if (!targetDir.isDirectory())
56         {
57             throw new FileNotFoundException JavaDoc(targetDir.toString() + " is not a directory.");
58         }
59         
60         while ((entry = zis.getNextEntry()) != null)
61         {
62             String JavaDoc name = entry.getName();
63             
64             long size = entry.getSize();
65             
66             long time = entry.getTime();
67             time = (time != -1) ? time : new Date JavaDoc().getTime();
68             
69             File JavaDoc f = new File JavaDoc(targetDir,name);
70             
71             
72             
73             if (entry.isDirectory())
74             {
75                 f.mkdirs();
76             }
77             else
78             {
79                 f.getParentFile().mkdirs();
80                 
81                 FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(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             // size should be 0 here
92

93             f.setLastModified(time);
94
95             zis.closeEntry();
96             
97         }
98         zis.close();
99     }
100
101     public static void main(String JavaDoc[] args) throws Exception JavaDoc
102     {
103         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(args[0]);
104         unzip(fis,new File JavaDoc(args[1]));
105     }
106 }
107
Popular Tags