1 17 18 19 21 39 40 import java.io.*; import java.net.*; import java.util.*; import java.util.zip.*; import java.lang.reflect.*; 46 84 public class Extractor { 85 86 public final static boolean debug=true; 87 88 89 protected Vector extractedFiles; 90 91 protected String archiveType=""; 92 93 protected String appClassName=""; 94 95 protected String extraClassPath=""; 96 97 private static Extractor extractor= null; 100 101 102 103 public Extractor() 104 { 105 extractedFiles = new Vector(128); 106 } 107 108 109 110 111 private static void printChar( int ch ) 112 { 113 if ( ch >= 32 && ch < 128 ) 114 System.out.print((char)ch); 115 else 116 System.out.print("<"+ch+">"); 117 } 118 119 126 private boolean findArchiveKey( PushbackInputStream pis, String key ) 127 throws IOException 128 { 129 boolean archiveKeyFound=false; 130 int ch=0; 131 132 while ( ch != -1 ) { 133 int count=0; 134 for (int k=0; k<key.length(); ++k) { 135 ch = pis.read(); 136 if ( ch == key.charAt(k)) 138 ++count; 139 else 140 break; 141 } 142 if (count == key.length() ) { 143 archiveKeyFound = true; 144 break; 145 } 146 147 } 148 149 return (archiveKeyFound ); 150 } 151 152 155 private void readDelimiter( PushbackInputStream pis ) 156 throws IOException 157 { 158 if (debug) System.out.println("readDelimiter"); 159 160 int ch = pis.read(); 161 if ( ch != '|' ) 163 throw new IOException( "delimiter expected." ); 164 } 165 166 167 172 private String readArchiveType( PushbackInputStream pis ) 173 throws IOException 174 { 175 if (debug) System.out.println("readArchiveType"); 176 177 StringBuffer type = new StringBuffer (8); 178 int ch=0; 179 180 int counter=0; 181 do { 182 ch = pis.read(); ++counter; 183 if ( ch != '|' ) 185 type.append((char)ch); 186 else 187 pis.unread(ch); 188 } 189 while ( ch != '|' && counter < 32 ); 190 191 return type.toString(); 192 } 193 194 199 private String readApplicationClass( PushbackInputStream pis ) 200 throws IOException 201 { 202 if (debug) System.out.println("readApplicationClass"); 203 204 StringBuffer appClass = new StringBuffer (8); 205 int ch=0; 206 207 int counter=0; 208 do { 209 ch = pis.read(); ++counter; 210 if ( ch != '|' ) 212 appClass.append((char)ch); 213 else 214 pis.unread(ch); 215 } 216 while ( ch != '|' && counter < 512 ); 217 218 return appClass.toString(); 219 } 220 221 226 private String readExtraClassPath( PushbackInputStream pis ) 227 throws IOException 228 { 229 if (debug) System.out.println("readExtraClassPath"); 230 231 StringBuffer ecp = new StringBuffer (64); 232 int ch=0; 233 234 int counter=0; 235 do { 236 ch = pis.read(); ++counter; 237 if ( ch != '|' ) 239 ecp.append((char)ch); 240 else 241 pis.unread(ch); 242 } 243 while ( ch != '|' && counter < 512 ); 244 245 return ecp.toString(); 246 } 247 248 252 private void extractZipFiles( InputStream is ) 253 throws IOException 254 { 255 System.out.println( "extractZipFiles()" ); 257 ZipInputStream zis = null; 258 try { 259 zis = new ZipInputStream( is ); 260 ZipEntry zipEntry; 261 int num_entries=0; 262 263 byte [] buffer = new byte[4096]; 264 int total_bytes=0; 265 do { 266 zipEntry = zis.getNextEntry(); 267 if (zipEntry != null ) { 268 ++num_entries; 269 int method = zipEntry.getMethod(); 270 if (debug) { 271 System.out.println( "*** ENTRY ["+num_entries+"] ***" ); 272 System.out.println( " name: " + zipEntry.getName() ); 273 System.out.println( " size: " + zipEntry.getSize() ); 274 System.out.println( " extra: " + zipEntry.getExtra() ); 275 System.out.println( " compressed size: " + zipEntry.getCompressedSize() ); 276 System.out.println( " method: " + 277 (method == ZipEntry.DEFLATED ? "(Compressed)" : 278 method == ZipEntry.STORED ? "(Stored)" : "UNKNOWN!" )); 279 } 280 System.out.print('.'); 281 282 String entryFilePath = zipEntry.getName(); 283 entryFilePath = entryFilePath.replace('/', File.separatorChar ); 284 entryFilePath = entryFilePath.replace('\\', File.separatorChar ); 285 if (debug) System.out.println( "extracting: `"+entryFilePath +"'"); 286 287 288 File entryFile = new File( entryFilePath ); 289 if (entryFile.isDirectory() ) 290 entryFile.mkdirs(); 292 else { 293 if ( entryFile.getParent() != null ) 295 new File(entryFile.getParent()).mkdirs(); 296 297 extractedFiles.addElement( entryFilePath ); 298 FileOutputStream fos = new FileOutputStream( entryFile ); 299 int num_bytes; 300 while ( (num_bytes = zis.read( buffer, 0, buffer.length )) >= 0 ) { 301 fos.write( buffer, 0, num_bytes ); 302 fos.flush(); total_bytes += num_bytes; 304 } 305 fos.close(); } 307 zis.closeEntry(); 308 } 309 } 310 while (zipEntry != null); 311 312 System.out.println("\n\nExtracted a total "+total_bytes+" byte"+ 313 (total_bytes == 1 ? "" : "s" ) + 314 " from "+ num_entries+" ent"+ 315 (num_entries == 1 ? "ry" : "ries" )+"." ); 316 } 317 finally { 318 if (zis != null) try { zis.close(); } catch (IOException e ) { ; } 320 } 321 } 322 323 327 public void extractFiles() 328 { 329 String resname = getClass().getName(); 330 331 String left ="-=+"; 332 String right="+=-"; 333 String key=left+"ARCHIVE"+right; 334 335 PushbackInputStream pis=null; 336 try { 337 pis = new PushbackInputStream( 338 new FileInputStream( resname+".class" )); 339 int ch=0; 340 boolean foundKey=false; 341 foundKey = findArchiveKey( pis, key ); 345 if ( foundKey ) { 346 if (debug) System.out.println("\n*** KEY FOUND !!! ***"); 347 readDelimiter(pis); 348 archiveType = readArchiveType(pis); 349 readDelimiter(pis); 350 appClassName = readApplicationClass(pis); 351 readDelimiter(pis); 352 extraClassPath = readExtraClassPath(pis); 353 readDelimiter(pis); 354 355 ch = pis.read(); 358 if ( ch != '\n' ) { 359 pis.unread(ch); 360 } 361 362 if (debug) { 363 System.out.println( "\n***DEBUG***" ); 364 System.out.println( "archiveType=`"+archiveType+"'"); 365 System.out.println( "appClassName=`"+appClassName+"'" ); 366 System.out.println( "extraClassPath=`"+extraClassPath+"'"); 367 } 368 369 if ( archiveType.equalsIgnoreCase("ZIP" ) ) 370 extractZipFiles( pis ); 371 else 372 throw new RuntimeException ("Cannot extract archive type:`"+archiveType+"'"); 373 } 374 else 375 throw new RuntimeException ("Cannot find archive key."); 376 377 } 378 catch (IOException ioe) { 379 System.err.println(ioe); 380 cleanupFiles(); 381 System.exit(1); 382 } 383 finally { 384 if (pis != null) try { pis.close(); } catch (IOException e) { ; } 385 } 386 387 } 388 389 401 public void runApplicationInstaller( String [] args ) 402 { 403 System.out.println( getClass().getName()+".runInstaller()" ); 405 extractFiles(); 406 407 if ( extraClassPath.length() > 0 ) { 408 String pathSep = System.getProperty("path.separator"); 413 String fileSep = System.getProperty("file.separator"); 414 415 if ( pathSep.equals(";") ) 416 extraClassPath = extraClassPath.replace( ':' , ';' ); 418 else 419 extraClassPath = extraClassPath.replace( ';' , ':' ); 421 422 if ( fileSep.equals("\\") ) 423 extraClassPath = extraClassPath.replace( '/' , '\\' ); 425 else 426 extraClassPath = extraClassPath.replace( '\\' , '/' ); 428 429 434 if (debug) 435 System.out.println("*NEW* classpath = "+ System.getProperty("java.class.path") ); 436 } 437 438 try { 444 String [] dummyArgs = new String [0]; 445 Class mainClass = Class.forName( appClassName ); 446 System.out.println( "loaded class:"+appClassName ); Method mainMethod = mainClass.getMethod( 449 "main", new Class [] { dummyArgs.getClass() } ); 450 System.out.println( "about to invoke public static void main( String [] )"); Object retVal = mainMethod.invoke( null, new Object [] { args } ); 452 } 454 catch ( ClassNotFoundException e1 ) { 455 System.err.println(e1); 456 } 457 catch ( NoSuchMethodException e2 ) { 458 System.err.println(e2); 459 } 460 catch ( IllegalAccessException e3 ) { 461 System.err.println(e3); 462 } 463 catch ( InvocationTargetException e4 ) { 464 System.err.println("Method threw an "+e4.getTargetException() ); 465 } 466 467 } 468 469 470 public void cleanupFiles() 471 { 472 System.out.println("========================================"); 473 System.out.println("cleaning up files "); 474 Enumeration etor = extractedFiles.elements(); 475 while ( etor.hasMoreElements() ) { 476 String filename = (String )etor.nextElement(); 477 File file = new File( filename ); 478 if ( file.exists() ) { 479 if (debug) System.out.println("deleting file:`"+filename+"'" ); 480 file.delete(); 481 } 482 } 483 extractedFiles.removeAllElements(); 484 } 485 486 491 public void finalize() 492 throws Throwable 493 { 494 try { 495 if (debug) System.out.println("Extractor.finalize()"); 496 cleanupFiles(); 497 } 498 finally { 499 super.finalize(); } 501 } 502 503 public static void main( String [] args ) 504 { 505 System.runFinalizersOnExit(true); 523 524 extractor = new Extractor(); 525 extractor.runApplicationInstaller( args ); 526 } 527 528 } 570 571 | Popular Tags |