KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > util > ZipExtract


1 package com.sslexplorer.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.OutputStream JavaDoc;
8 import java.util.zip.ZipEntry JavaDoc;
9 import java.util.zip.ZipInputStream JavaDoc;
10
11 import com.sslexplorer.boot.Util;
12
13
14 public class ZipExtract {
15
16   public static final void copyInputStream(InputStream JavaDoc in, OutputStream JavaDoc out)
17   throws IOException JavaDoc
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 JavaDoc basedir, InputStream JavaDoc in) throws IOException JavaDoc {
31       extractZipFile(basedir, in, true);
32   }
33
34   public static final void extractZipFile(File JavaDoc basedir, InputStream JavaDoc in, boolean onlyIfNewer) throws IOException JavaDoc {
35
36     ZipInputStream JavaDoc zin = new ZipInputStream JavaDoc(in);
37     try {
38       
39
40       ZipEntry JavaDoc 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 JavaDoc f = new File JavaDoc(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 JavaDoc out = new FileOutputStream JavaDoc(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