1 23 24 27 28 package com.sun.enterprise.config.backup.util; 29 30 import java.io.*; 31 import java.util.*; 32 import java.util.zip.*; 33 import java.util.jar.JarFile ; 34 35 39 40 42 public class ZipFile 43 { 44 public ZipFile(String zipFilename, String explodeDirName) throws ZipFileException 45 { 46 this(new File (zipFilename), new File (explodeDirName)); 47 } 48 49 51 public ZipFile(File zipFile, File anExplodeDir) throws ZipFileException 52 { 53 BufferedInputStream bis = null; 55 56 try 57 { 58 bis = new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE); 59 ctor(bis, anExplodeDir); 60 } 61 catch(Throwable e) 62 { 63 if (bis != null) 64 { 65 try 66 { 67 bis.close(); 68 } 69 catch (Throwable thr) 70 { 71 throw new ZipFileException(thr); 72 } 73 } 74 75 throw new ZipFileException(e); 76 } 77 } 78 79 81 public ZipFile(InputStream inStream, File anExplodeDir) throws ZipFileException 82 { 83 ctor(inStream, anExplodeDir); 84 } 85 86 88 public ArrayList explode() throws ZipFileException 89 { 90 files = new ArrayList(); 91 ZipInputStream zin = null; 92 93 try 94 { 95 zin = zipStream; ZipEntry ze; 97 98 while( (ze = zin.getNextEntry()) != null ) 99 { 100 String filename = ze.getName(); 101 102 108 109 File fullpath = null; 110 111 if(isDirectory(filename)) 112 { 113 fullpath = new File (explodeDir, filename.substring(0, filename.length() - 1)); 115 116 119 fullpath.mkdirs(); 120 continue; 121 } 122 123 fullpath = new File (explodeDir, filename); 124 File newDir = fullpath.getParentFile(); 125 126 if(newDir.mkdirs()) 127 { } 130 131 if(fullpath.delete()) { 133 } 135 136 BufferedOutputStream os = new BufferedOutputStream(getOutputStream(filename), BUFFER_SIZE); 137 138 if(os == null) continue; 140 141 int totalBytes = 0; 142 143 for(int numBytes = zin.read(buffer); numBytes > 0; numBytes = zin.read(buffer)) 144 { 145 os.write(buffer, 0, numBytes); 146 totalBytes += numBytes; 147 } 148 os.close(); 149 files.add(filename); 151 } 152 } 153 catch(IOException e) 154 { 155 throw new ZipFileException(e); 156 } 157 finally 158 { 159 try 161 { 162 zin.close(); 163 } 164 catch(IOException e) 165 { 166 throw new ZipFileException("Got an exception while trying to close Jar input stream: " + e); } 168 } 169 return files; 171 } 172 173 175 public ArrayList getFileList() 176 { 177 return files; 178 } 179 180 183 private static ArrayList doExplode(ZipFile zf) throws ZipFileException 184 { 185 ArrayList finalList = new ArrayList(50); 186 ArrayList zipFileList = new ArrayList(); 187 ArrayList tmpList = null; 188 ZipFile tmpZf = null; 189 Iterator itr = null; 190 String fileName = null; 191 192 zipFileList.add(zf); 193 while (zipFileList.size() > 0) 194 { 195 tmpZf = (ZipFile)zipFileList.remove(zipFileList.size() - 1); 197 tmpList = tmpZf.explode(); 198 199 itr = tmpList.iterator(); 201 while (itr.hasNext()) 202 { 203 fileName = (String )itr.next(); 204 if ( ! fileName.endsWith(".jar") ) 205 { 206 finalList.add(fileName); 208 } 209 else 210 { 211 File f = new File (tmpZf.explodeDir, fileName); 213 ZipFile newZf = new ZipFile(f, tmpZf.explodeDir); 214 zipFileList.add(newZf); 215 } 216 217 if (tmpZf != zf) { 219 tmpZf.explodeDir.delete(); 220 } 221 } 222 } 223 return finalList; 224 } 225 226 228 private void ctor(InputStream inStream, File anExplodeDir) throws ZipFileException 229 { 230 insist(anExplodeDir != null); 231 explodeDir = anExplodeDir; 232 233 try 234 { 235 zipStream = new ZipInputStream(inStream); 236 checkExplodeDir(); 237 } 238 catch(Throwable t) 239 { 240 if (zipStream != null) { 241 try { 242 zipStream.close(); 243 } catch (Throwable thr) { 244 } 245 } 246 throw new ZipFileException(t.toString()); 247 } 248 } 249 250 252 private boolean isDirectory(String s) 253 { 254 char c = s.charAt(s.length() - 1); 255 256 return c== '/' || c == '\\'; 257 } 258 259 261 private void checkZipFile(File zipFile) throws ZipFileException 262 { 263 insist(zipFile != null); 264 265 String zipFileName = zipFile.getPath(); 266 267 insist( zipFile.exists(), "zipFile (" + zipFileName + ") doesn't exist" ); insist( !zipFile.isDirectory(), "zipFile (" + zipFileName + ") is actually a directory!" ); } 270 271 273 private void checkExplodeDir() throws ZipFileException 274 { 275 String explodeDirName = explodeDir.getPath(); 276 277 explodeDir.mkdirs(); 279 280 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 ); } 284 285 287 private static boolean isSpecial(String filename) 288 { 289 return filename.toUpperCase().startsWith(specialDir.toUpperCase()); 290 } 291 292 294 private FileOutputStream getOutputStream(String filename) throws ZipFileException 295 { 296 File f = new File (explodeDir, filename); 297 298 if(f.isDirectory()) 299 { 300 return null; 302 } 303 304 try 305 { 306 return new FileOutputStream(f); 307 } 308 catch(FileNotFoundException e) 309 { 310 throw new ZipFileException("filename: " + f.getPath() + " " + e); 311 } 312 catch(IOException e) 313 { 314 throw new ZipFileException(e); 315 } 316 } 317 318 320 private boolean isManifest(String filename) 321 { 322 if(filename.toLowerCase().endsWith("manifest.mf")) return false; 324 325 return false; 326 } 327 328 334 private static void pr(String s) 335 { 336 System.out.println( s ); 337 } 338 339 341 private static void insist(String s) throws ZipFileException 342 { 343 if( s == null || s.length() < 0 ) 344 throw new ZipFileException(); 345 else 346 return; 347 } 348 349 351 private static void insist(String s, String mesg) throws ZipFileException 352 { 353 if( s == null || s.length() < 0 ) 354 throw new ZipFileException( mesg ); 355 else 356 return; 357 } 358 359 361 private static void insist(boolean b) throws ZipFileException 362 { 363 if( !b ) 364 throw new ZipFileException(); 365 else 366 return; 367 } 368 369 371 private static void insist(boolean b, String mesg) throws ZipFileException 372 { 373 if( !b ) 374 throw new ZipFileException( mesg ); 375 else 376 return; 377 } 378 379 381 private static final int BUFFER_SIZE = 0x10000; private File explodeDir = null; 383 private ArrayList files = null; 384 private static final String specialDir = "META-INF/"; private byte[] buffer = new byte[BUFFER_SIZE]; 386 private ZipInputStream zipStream = null; 387 } 388 389 | Popular Tags |