1 23 24 27 28 package com.sun.enterprise.tools.common.util.zip; 29 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.FileNotFoundException ; 35 import java.io.InputStream ; 36 import java.io.PrintStream ; 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.zip.ZipEntry ; 40 import java.util.zip.ZipInputStream ; 41 42 import com.sun.enterprise.tools.common.util.diagnostics.Reporter; 43 import com.sun.enterprise.tools.common.util.Assertion; 44 import com.sun.enterprise.tools.common.util.ContainerHelper; 45 46 public class ZipFile 47 { 48 public ZipFile(String zipFilename, String explodeDirName) throws ZipFileException 49 { 50 assertIt(zipFilename); 51 assertIt(explodeDirName); 52 53 this.zipFilename = zipFilename; 54 this.explodeDirName = explodeDirName; 55 56 try 57 { 58 checkZipFile(); 59 zipStream = new ZipInputStream (new FileInputStream (zipFile)); 60 checkExplodeDir(); 62 } 63 catch(Exception f) 64 { 65 throw new ZipFileException(f); 66 } 67 } 68 69 70 public ZipFile(InputStream inStream, String explodeDirName) throws ZipFileException 71 { 72 assertIt(explodeDirName); 74 75 this.explodeDirName = explodeDirName; 77 78 try 79 { 80 zipStream = new ZipInputStream (inStream); 82 checkExplodeDir(); 83 } 84 catch(Assertion.Failure f) 85 { 86 throw new ZipFileException(f); 87 } 88 } 89 90 92 public String toString() 93 { 94 String s = "Zip File Name: " + zipFilename + "\n"; 98 return s; 99 } 100 101 103 109 110 112 public String [] explode() throws ZipFileException 113 { 114 ArrayList explodedFiles = new ArrayList (); 115 116 118 ZipInputStream zin = null; 119 120 124 try 125 { 126 zin = zipStream; ZipEntry ze; 128 129 while( (ze = zin.getNextEntry()) != null ) 130 { 131 String filename = ze.getName(); 132 133 if(isManifest(filename)) 134 { 135 continue; } 137 138 File fullpath = new File (explodeDir, filename); 139 File newDir = fullpath.getParentFile(); 140 141 if(newDir.mkdirs()) 142 { Reporter.verbose("Created new directory: " + newDir); } 145 146 if(fullpath.delete()) Reporter.info("deleted pre-existing file: " + fullpath); 149 FileOutputStream os = getOutputStream(filename); 151 152 if(os == null) continue; 154 155 int totalBytes = 0; 156 157 for(int numBytes = zin.read(buffer); numBytes > 0; numBytes = zin.read(buffer)) 158 { 159 os.write(buffer, 0, numBytes); 160 totalBytes += numBytes; 161 } 162 os.close(); 163 Reporter.verbose("Wrote " + totalBytes + " to " + filename); explodedFiles.add(filename); 165 } 166 } 167 catch(IOException e) 168 { 169 throw new ZipFileException(e); 170 } 171 finally 172 { 173 Reporter.verbose("Closing zin..."); try 175 { 176 zin.close(); 177 } 178 catch(IOException e) 179 { 180 throw new ZipFileException("Got an exception while trying to close Jar input stream: " + e); } 182 } 183 Reporter.info("Successfully Exploded " + zipFilename + " to " + explodeDirName); return ContainerHelper.toStringArray(explodedFiles); 185 } 186 187 190 191 private void createFileNameList() throws ZipFileException 192 { 193 ZipInputStream zin; 194 195 assertIt( (files != null) ? false : true, "createFileNameList() called a second time. Should only be called once and only once!" ); files = new ArrayList (); 197 zin = null; 198 try 199 { 200 ZipEntry ze; 201 202 zin = zipStream; while( (ze = zin.getNextEntry()) != null ) 204 { 205 String name = ze.getName(); 206 zin.closeEntry(); 207 files.add(name); 208 } 209 zin.close(); 210 } 211 catch( IOException e) 212 { 213 Reporter.error(e + " " + zipFile); throw new ZipFileException(e); 215 } 216 } 217 218 220 private void checkZipFile() throws ZipFileException 221 { 222 assertIt(zipFilename); 223 zipFile = new File ( zipFilename ); 224 assertIt( zipFile.exists(), "zipFile (" + zipFilename + ") doesn't exist" ); assertIt( !zipFile.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" ); } 228 229 231 private void checkExplodeDir() throws ZipFileException 232 { 233 File dir; 234 235 assertIt(explodeDirName); 236 explodeDir = new File (explodeDirName); 237 238 assertIt(explodeDir.exists(), "Target Directory doesn't exist: " + explodeDirName ); assertIt(explodeDir.isDirectory(), "Target Directory isn't a directory: " + explodeDirName ); assertIt(explodeDir.canWrite(), "Can't write to Target Directory: " + explodeDirName ); } 242 243 245 private static boolean isSpecial(String filename) 246 { 247 return filename.toUpperCase().startsWith(specialDir.toUpperCase()); 248 } 249 250 252 private void createDirs() throws ZipFileException 253 { 254 257 Assertion.check(explodeDir, "Programmer Error -- need to setup explodeDir"); Iterator iter = files.iterator(); 259 260 while(iter.hasNext()) 261 { 262 String fname = (String ) iter.next(); 263 File fullpath = new File (explodeDir, fname); 264 File newDir = fullpath.getParentFile(); 265 266 if(newDir.mkdirs()) 267 { Reporter.verbose("Created new directory: " + newDir); } 270 271 if(fullpath.delete()) Reporter.info("deleted pre-existing file: " + fullpath); 274 assertIt(newDir.exists() && newDir.isDirectory(), "Couldn't create directory: " + newDir); } 276 } 277 278 280 private FileOutputStream getOutputStream(String filename) throws ZipFileException 281 { 282 Assertion.check(explodeDir, "Programmer Error -- need to setup explodeDir"); File f = new File (explodeDir, filename); 284 285 if(f.isDirectory()) 286 { 287 Reporter.warn("Weird! A directory is listed as an entry in the jar file -- skipping..."); return null; 289 } 290 291 try 292 { 293 return new FileOutputStream (f); 294 } 295 catch(FileNotFoundException e) 296 { 297 throw new ZipFileException(e); 298 } 299 catch(IOException e) 300 { 301 throw new ZipFileException(e); 302 } 303 } 304 305 307 private boolean isManifest(String filename) 308 { 309 if(filename.toLowerCase().endsWith("manifest.mf")) return false; 311 312 return false; 313 } 314 315 321 private static void pr(String s) 322 { 323 System.out.println( s ); } 325 326 328 private static void assertIt(String s) throws ZipFileException 329 { 330 if( s == null || s.length() < 0 ) 331 throw new ZipFileException(); 332 else 333 return; 334 } 335 336 338 private static void assertIt(String s, String mesg) throws ZipFileException 339 { 340 if( s == null || s.length() < 0 ) 341 throw new ZipFileException( mesg ); 342 else 343 return; 344 } 345 346 348 private static void assertIt(boolean b) throws ZipFileException 349 { 350 if( !b ) 351 throw new ZipFileException(); 352 else 353 return; 354 } 355 356 358 private static void assertIt(boolean b, String mesg) throws ZipFileException 359 { 360 if( !b ) 361 throw new ZipFileException( mesg ); 362 else 363 return; 364 } 365 366 368 public static void main(String [] String_1darray1) 369 { 370 try 371 { 372 ZipFile zip = new ZipFile("D:\\test\\AccessorTestEnterpriseBean.jar", "D:/test/zipOut"); 374 pr("" + zip); zip.explode(); 376 } 377 catch(ZipFileException e) 378 { 379 pr("ZipFileException: " + e); } 381 } 382 383 385 private String zipFilename = null; 386 private String explodeDirName = null; 387 private File zipFile = null; 388 private File explodeDir = null; 389 private ArrayList files = null; 390 private static final String specialDir = "META-INF/"; private byte[] buffer = new byte[16384]; 392 private ZipInputStream zipStream = null; 393 } 394 | Popular Tags |