1 package com.opensymphony.webwork.portlet.util; 2 3 4 import com.opensymphony.util.TextUtils; 5 6 import java.io.File ; 7 import java.util.Enumeration ; 8 import java.util.zip.ZipEntry ; 9 import java.util.zip.ZipFile ; 10 11 public class FileUnzipper extends AbstractUnzipper { 12 private File zipFile; 13 14 public FileUnzipper(File zipFile, File destDir) { 15 this.zipFile = zipFile; 16 super.destDir = destDir; 17 } 18 19 public void unzip() throws Exception { 20 if (zipFile == null || !zipFile.isFile()) 21 return; 22 ZipFile zf = new ZipFile (zipFile); 23 ZipEntry zipEntry; 24 for (Enumeration zipEntries = zf.entries(); zipEntries.hasMoreElements(); saveEntry( 25 zf.getInputStream(zipEntry), zipEntry)) 26 zipEntry = (ZipEntry ) zipEntries.nextElement(); 27 28 zf.close(); 29 } 30 31 public File unzipFileInArchive(String fileName) throws Exception { 32 File result = null; 33 if (zipFile == null || !zipFile.isFile() || !TextUtils.stringSet(fileName)) 34 return result; 35 boolean fileFound = false; 36 ZipFile zf = new ZipFile (zipFile); 37 Enumeration zipEntries = zf.entries(); 38 do { 39 if (!zipEntries.hasMoreElements()) 40 break; 41 ZipEntry entry = (ZipEntry ) zipEntries.nextElement(); 42 String 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 |