1 22 package org.objectweb.petals.util; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.URI ; 28 import java.util.Enumeration ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipException ; 31 import java.util.zip.ZipFile ; 32 33 import org.objectweb.petals.PetalsException; 34 35 43 public final class ZipUtil { 44 45 48 private ZipUtil() { 49 super(); 50 } 51 52 62 public static void explodeIntoDirectory(ZipFile zipFile, File aDirectory) 63 throws PetalsException { 64 if (zipFile == null || aDirectory == null) { 65 throw new IllegalArgumentException ( 66 "The zip file or directory must not be null."); 67 } else if (!aDirectory.exists() || !aDirectory.isDirectory()) { 68 throw new IllegalArgumentException ( 69 "The given file is not a directory or doesn't exist."); 70 } 71 Enumeration entries = zipFile.entries(); 72 while (entries.hasMoreElements()) { 73 ZipEntry anEntry = (ZipEntry ) entries.nextElement(); 74 File unpackFile = new File (aDirectory, anEntry.getName()); 75 File parentDir = unpackFile.getParentFile(); 76 if (!parentDir.exists() && !parentDir.mkdirs()) { 77 throw new PetalsException( 78 "Failed to explode zip into directory: " 79 + anEntry.getName()); 80 } 81 if (!anEntry.isDirectory()) { 82 InputStream zipStream = null; 83 try { 84 zipStream = zipFile.getInputStream(anEntry); 85 NioUtil.copyStreamToFile(zipStream, unpackFile); 86 } catch (IOException ioe) { 87 throw new PetalsException( 88 "Failed to unpack a Zip file entry: " 89 + anEntry.getName(), ioe); 90 } finally { 91 if (zipStream != null) { 92 try { 93 zipStream.close(); 94 } catch (IOException e) { 95 } 97 } 98 } 99 } 100 } 101 } 102 103 111 public static File getEntryAsTemp(ZipFile zipFile, String entryName) { 112 if (zipFile == null) { 113 throw new IllegalArgumentException ("Invalid null ZipFile."); 114 } else if (entryName == null || "".equals(entryName)) { 115 throw new IllegalArgumentException ( 116 "Invalid Zip entry was specified: null or empty."); 117 } 118 File tempFile = null; 119 InputStream zipStream = null; 120 try { 121 ZipEntry theEntry = null; 122 if (entryName.indexOf("/") > -1) { 123 theEntry = zipFile.getEntry(entryName); 124 if (theEntry == null) { 125 theEntry = zipFile.getEntry(entryName.replace("/", "\\")); 126 if (theEntry == null) { 127 return null; 128 } 129 } 130 } else { 131 theEntry = zipFile.getEntry(entryName); 132 if (theEntry == null) { 133 theEntry = zipFile.getEntry(entryName.replace("\\", "/")); 134 if (theEntry == null) { 135 return null; 136 } 137 } 138 } 139 zipStream = zipFile.getInputStream(theEntry); 140 tempFile = File.createTempFile("petals-", "-zip.tmp"); 141 NioUtil.copyStreamToFile(zipStream, tempFile); 142 } catch (IOException e) { 143 return null; 145 } finally { 146 try { 147 if (zipStream != null) { 148 zipStream.close(); 149 } 150 } catch (IOException ioe) { 151 } 153 } 154 return tempFile; 155 } 156 157 166 public static boolean hasEntry(ZipFile archive, String entryName) { 167 ZipEntry theEntry = archive.getEntry(entryName); 168 return theEntry != null; 169 } 170 171 180 public static ZipFile openZipFile(URI archiveURI) throws PetalsException { 181 ZipFile zipArchive = null; 182 String msg; 183 try { 184 File archiveFile = new File (archiveURI); 185 zipArchive = new ZipFile (archiveFile); 186 } catch (ZipException ze) { 187 msg = "Error opening Zip file. Location: " + archiveURI.toString(); 188 throw new PetalsException(msg, ze); 189 } catch (IOException ioe) { 190 msg = "Unexpected I/O exception handling Zip file. Location : " 191 + archiveURI.toString(); 192 throw new PetalsException(msg, ioe); 193 } 194 return zipArchive; 195 } 196 197 } 198 | Popular Tags |