1 23 24 27 28 package com.sun.enterprise.util.zip; 29 30 import com.sun.enterprise.util.OS; 31 import java.io.*; 32 import java.util.*; 33 import java.util.zip.*; 34 import java.util.jar.JarFile ; 35 36 import com.sun.enterprise.util.io.FileUtils; 37 import com.sun.enterprise.util.Assertion; 38 import com.sun.enterprise.util.diagnostics.Reporter; 39 40 import com.sun.logging.LogDomains; 41 import java.util.logging.Logger ; 42 import java.util.logging.Level ; 43 44 45 47 public class ZipFile 48 { 49 public ZipFile(String zipFilename, String explodeDirName) throws ZipFileException 50 { 51 this(new File (zipFilename), new File (explodeDirName)); 52 } 53 54 56 public ZipFile(InputStream inStream, String anExplodeDirName) throws ZipFileException 57 { 58 this(new BufferedInputStream(inStream, BUFFER_SIZE), new File (anExplodeDirName)); 59 } 60 61 63 public ZipFile(File zipFile, File anExplodeDir) throws ZipFileException 64 { 65 checkZipFile(zipFile); 66 BufferedInputStream bis = null; 67 68 try 69 { 70 bis = new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE); 71 ctor(bis, anExplodeDir); 72 this.zipFile = zipFile; 73 } 74 catch(Throwable e) 75 { 76 if (bis != null) { 77 try { 78 bis.close(); 79 } catch (Throwable thr) { 80 throw new ZipFileException(thr); 81 } 82 } 83 throw new ZipFileException(e); 84 } 85 } 86 87 89 public ZipFile(InputStream inStream, File anExplodeDir) throws ZipFileException 90 { 91 ctor(inStream, anExplodeDir); 92 } 93 94 102 103 105 public ArrayList explode() throws ZipFileException 106 { 107 files = new ArrayList(); 108 ZipInputStream zin = null; 109 110 try 111 { 112 zin = zipStream; ZipEntry ze; 114 115 while( (ze = zin.getNextEntry()) != null ) 116 { 117 String filename = ze.getName(); 118 119 125 126 File fullpath = null; 127 128 if(isDirectory(filename)) 129 { 130 fullpath = new File (explodeDir, filename.substring(0, filename.length() - 1)); 132 if (OS.isWindows()) { 133 FileUtils.validateWindowsFilePathLength(fullpath); 134 } 135 fullpath.mkdir(); 136 continue; 137 } 138 139 fullpath = new File (explodeDir, filename); 140 if (OS.isWindows() ) { 141 FileUtils.validateWindowsFilePathLength(fullpath); 142 } 143 144 File newDir = fullpath.getParentFile(); 145 146 if(newDir.mkdirs()) 147 { Reporter.verbose("Created new directory: " + newDir); } 150 151 if(fullpath.delete()) { Reporter.info("deleted pre-existing file: " + fullpath); 156 if (_utillogger.isLoggable(Level.FINE) ) { 157 _utillogger.log(Level.FINE, "File " + fullpath.getAbsolutePath() + " is being overwritten during expansion of " + (zipFile != null ? ("file " + zipFile.getAbsolutePath() ) : "stream")); 158 } 159 } 160 161 BufferedOutputStream os = new BufferedOutputStream(getOutputStream(filename), BUFFER_SIZE); 162 163 if(os == null) continue; 165 166 int totalBytes = 0; 167 168 for(int numBytes = zin.read(buffer); numBytes > 0; numBytes = zin.read(buffer)) 169 { 170 os.write(buffer, 0, numBytes); 171 totalBytes += numBytes; 172 } 173 os.close(); 174 Reporter.verbose("Wrote " + totalBytes + " to " + filename); files.add(filename); 176 } 177 } 178 catch(IOException e) 179 { 180 throw new ZipFileException(e); 181 } 182 finally 183 { 184 Reporter.verbose("Closing zin..."); try 186 { 187 zin.close(); 188 } 189 catch(IOException e) 190 { 191 throw new ZipFileException("Got an exception while trying to close Jar input stream: " + e); } 193 } 194 Reporter.info("Successfully Exploded ZipFile" + " to " + explodeDir.getPath()); return files; 196 } 197 198 210 public static void extractJar(String jarEntryName, JarFile earFile, 211 File jarFile) throws ZipFileException 212 { 213 214 try 215 { 216 if (OS.isWindows()) { 217 FileUtils.validateWindowsFilePathLength(jarFile); 218 } 219 File parent = jarFile.getParentFile(); 220 if (!parent.exists()) 221 { 222 parent.mkdirs(); 223 } 224 225 ZipEntry jarEntry = earFile.getEntry(jarEntryName); 226 if (jarEntryName == null) 227 { 228 throw new ZipFileException(jarEntryName 229 + " not found in " + earFile.getName()); 230 } 231 232 InputStream is = earFile.getInputStream(jarEntry); 233 FileOutputStream fos = new FileOutputStream(jarFile); 234 FileUtils.copy(is, fos); 237 } 238 catch (IOException e) 239 { 240 throw new ZipFileException(e); 241 } 242 } 243 244 246 public ArrayList getFileList() 247 { 248 return files; 249 } 250 251 254 private static ArrayList doExplode(ZipFile zf) throws ZipFileException 255 { 256 ArrayList finalList = new ArrayList(50); 257 ArrayList zipFileList = new ArrayList(); 258 ArrayList tmpList = null; 259 ZipFile tmpZf = null; 260 Iterator itr = null; 261 String fileName = null; 262 263 zipFileList.add(zf); 264 while (zipFileList.size() > 0) 265 { 266 tmpZf = (ZipFile)zipFileList.remove(zipFileList.size() - 1); 268 tmpList = tmpZf.explode(); 269 270 itr = tmpList.iterator(); 272 while (itr.hasNext()) 273 { 274 fileName = (String )itr.next(); 275 if ( ! fileName.endsWith(".jar") ) 276 { 277 finalList.add(fileName); 279 } 280 else 281 { 282 File f = new File (tmpZf.explodeDir, fileName); 284 ZipFile newZf = new ZipFile(f, tmpZf.explodeDir); 285 zipFileList.add(newZf); 286 } 287 288 if (tmpZf != zf) { 290 tmpZf.explodeDir.delete(); 291 } 292 } 293 } 294 return finalList; 295 } 296 297 299 private void ctor(InputStream inStream, File anExplodeDir) throws ZipFileException 300 { 301 insist(anExplodeDir != null); 302 explodeDir = anExplodeDir; 303 304 try 305 { 306 zipStream = new ZipInputStream(inStream); 307 checkExplodeDir(); 308 } 309 catch(Throwable t) 310 { 311 if (zipStream != null) { 312 try { 313 zipStream.close(); 314 } catch (Throwable thr) { 315 } 316 } 317 throw new ZipFileException(t.toString()); 318 } 319 } 320 321 323 private boolean isDirectory(String s) 324 { 325 char c = s.charAt(s.length() - 1); 326 327 return c== '/' || c == '\\'; 328 } 329 330 332 private void checkZipFile(File zipFile) throws ZipFileException 333 { 334 insist(zipFile != null); 335 336 String zipFileName = zipFile.getPath(); 337 338 insist( zipFile.exists(), "zipFile (" + zipFileName + ") doesn't exist" ); insist( !zipFile.isDirectory(), "zipFile (" + zipFileName + ") is actually a directory!" ); } 341 342 344 private void checkExplodeDir() throws ZipFileException 345 { 346 String explodeDirName = explodeDir.getPath(); 347 348 explodeDir.mkdirs(); 350 351 insist(explodeDir.exists(), "Target Directory doesn't exist: " + explodeDirName ); insist(explodeDir.isDirectory(), "Target Directory isn't a directory: " + explodeDirName ); insist(explodeDir.canWrite(), "Can't write to Target Directory: " + explodeDirName ); } 355 356 358 private static boolean isSpecial(String filename) 359 { 360 return filename.toUpperCase().startsWith(specialDir.toUpperCase()); 361 } 362 363 365 private FileOutputStream getOutputStream(String filename) throws ZipFileException 366 { 367 File f = new File (explodeDir, filename); 368 369 if(f.isDirectory()) 370 { 371 Reporter.warn("Weird! A directory is listed as an entry in the jar file -- skipping..."); return null; 373 } 374 375 try 376 { 377 return new FileOutputStream(f); 378 } 379 catch(FileNotFoundException e) 380 { 381 throw new ZipFileException("filename: " + f.getPath() + " " + e); 382 } 383 catch(IOException e) 384 { 385 throw new ZipFileException(e); 386 } 387 } 388 389 391 private boolean isManifest(String filename) 392 { 393 if(filename.toLowerCase().endsWith("manifest.mf")) return false; 395 396 return false; 397 } 398 399 405 private static void pr(String s) 406 { 407 System.out.println( s ); 408 } 409 410 412 private static void insist(String s) throws ZipFileException 413 { 414 if( s == null || s.length() < 0 ) 415 throw new ZipFileException(); 416 else 417 return; 418 } 419 420 422 private static void insist(String s, String mesg) throws ZipFileException 423 { 424 if( s == null || s.length() < 0 ) 425 throw new ZipFileException( mesg ); 426 else 427 return; 428 } 429 430 432 private static void insist(boolean b) throws ZipFileException 433 { 434 if( !b ) 435 throw new ZipFileException(); 436 else 437 return; 438 } 439 440 442 private static void insist(boolean b, String mesg) throws ZipFileException 443 { 444 if( !b ) 445 throw new ZipFileException( mesg ); 446 else 447 return; 448 } 449 450 452 private static final int BUFFER_SIZE = 0x10000; private File explodeDir = null; 454 private ArrayList files = null; 455 private static final String specialDir = "META-INF/"; private byte[] buffer = new byte[BUFFER_SIZE]; 457 private ZipInputStream zipStream = null; 458 private Logger _utillogger = LogDomains.getLogger(LogDomains.UTIL_LOGGER); 459 private File zipFile = null; 460 } 461 462 | Popular Tags |