KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > portlet > util > FileUnzipper


1 package com.opensymphony.webwork.portlet.util;
2
3
4 import com.opensymphony.util.TextUtils;
5
6 import java.io.File JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.zip.ZipEntry JavaDoc;
9 import java.util.zip.ZipFile JavaDoc;
10
11 public class FileUnzipper extends AbstractUnzipper {
12     private File JavaDoc zipFile;
13
14     public FileUnzipper(File JavaDoc zipFile, File JavaDoc destDir) {
15         this.zipFile = zipFile;
16         super.destDir = destDir;
17     }
18
19     public void unzip() throws Exception JavaDoc {
20         if (zipFile == null || !zipFile.isFile())
21             return;
22         ZipFile JavaDoc zf = new ZipFile JavaDoc(zipFile);
23         ZipEntry JavaDoc zipEntry;
24         for (Enumeration JavaDoc zipEntries = zf.entries(); zipEntries.hasMoreElements(); saveEntry(
25                 zf.getInputStream(zipEntry), zipEntry))
26             zipEntry = (ZipEntry JavaDoc) zipEntries.nextElement();
27
28         zf.close();
29     }
30
31     public File JavaDoc unzipFileInArchive(String JavaDoc fileName) throws Exception JavaDoc {
32         File JavaDoc result = null;
33         if (zipFile == null || !zipFile.isFile() || !TextUtils.stringSet(fileName))
34             return result;
35         boolean fileFound = false;
36         ZipFile JavaDoc zf = new ZipFile JavaDoc(zipFile);
37         Enumeration JavaDoc zipEntries = zf.entries();
38         do {
39             if (!zipEntries.hasMoreElements())
40                 break;
41             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) zipEntries.nextElement();
42             String JavaDoc entryName = entry.getName();
43             if (TextUtils.stringSet(entryName) && entryName.startsWith("/"))
44                 entryName = entryName.substring(1);
45             if (fileName.equals(entryName)) {
46                 fileFound = true;
47                 result = saveEntry(zf.getInputStream(entry), entry);
48             }
49         } while (true);
50         if (!fileFound)
51             AbstractUnzipper.log.error("The file: " + fileName + " could not be found in the archive: "
52                     + zipFile.getAbsolutePath());
53         zf.close();
54         return result;
55     }
56
57 }
Popular Tags