1 15 16 18 36 37 import java.io.*; import java.util.*; 40 78 public class MakeSelfExtractor { 79 80 public final static boolean debug=true; 81 82 public final static String USAGE_TEXT = 83 "USAGE: java MakeSelfExtractor\n" + 84 "\t[ --type (-t) <ARCHIVE-TYPE> ][ --name (-m) <APP-CLASS-NAME>]\n" + 85 "\t[ --classpath (-cp) <EXTRA-CLASS-PATH> ]\n" + 86 "\t[ -h ][ -u ]\n" + 87 "\tARCHIVE\n" + 88 "\nOPTIONS:\n" + 89 " -cp specifies the extra class path.\n" + 90 " -m specifies the application class name.\n" + 91 " -t specifies the archive type class type.\n" + 92 " -h -u print this help text message.\n" + 93 "\nOPTIONS:\n" + 94 "Creates a self extractable Java class for the FreeInstaller.\n" + 95 "Written by Peter Pilgrim Mon Mar 08 00:22:56 GMT 1999\n"; 96 97 98 public final static String EXTRACTOR_CLASS = "Extractor.class"; 99 100 101 protected static String archiveFilename=""; 102 103 protected static String archiveType="ZIP"; 104 105 protected static String appClassName=""; 106 107 protected static String extraClassPath=""; 108 109 110 public static void error( String message ) 111 { 112 System.err.println("MakeSelfExtractor: "+message); 113 System.exit(1); 114 } 115 116 117 public static void main( String [] args ) 118 { 119 boolean acceptFlag=false; 120 int j; 121 122 for (j=0; j<args.length; ++j) { 123 String argument = args[j]; 124 125 if (argument.equals("-h") || argument.equals("-u") || 126 argument.equals("--help") || argument.equals("--usage")) { 127 System.out.println( USAGE_TEXT ); 128 System.exit(0); 129 } 130 else if (argument.equals("-t") || argument.equals("--type")) { 131 ++j; archiveType = args[j].trim(); 132 } 133 else if (argument.equals("-m") || argument.equals("--name")) { 134 ++j; appClassName = args[j].trim(); 135 } 136 else if (argument.equals("-cp") || argument.equals("-classpath")) { 137 ++j; extraClassPath = args[j].trim(); 138 } 139 else { 140 if (acceptFlag) 141 error("accepts only one archive."); 142 else { 143 acceptFlag = true; 144 archiveFilename = argument.trim(); 145 } 146 } 147 } 148 149 if ( appClassName.length() < 1 ) 150 error("no application main class supplied."); 151 if ( archiveFilename.length() < 1 ) 152 error("no archive filename supplied."); 153 154 File extractFile = new File(EXTRACTOR_CLASS); 155 if ( !extractFile.exists() ) 156 error("no such file:`"+extractFile+"'" ); 157 if ( !extractFile.isFile() ) 158 error("file:`"+extractFile+"' is not a regular file." ); 159 160 int retval = 0; 161 BufferedInputStream bis = null; 162 BufferedOutputStream bos = null; 163 164 System.out.println( "Creating self extracting file: "+extractFile.getPath() ); 165 System.out.println( "archive filename: "+archiveFilename ); 166 System.out.println( "archive type: "+archiveType ); 167 System.out.println( "extra class path: "+extraClassPath ); 168 169 String line = "-=+ARCHIVE+=-|" + archiveType+ "|" + appClassName+ "|" + extraClassPath+"|\n"; 170 int total_bytes=0; 171 try { 172 bis = new BufferedInputStream( new FileInputStream( archiveFilename )); 173 bos = new BufferedOutputStream( 174 new FileOutputStream( extractFile.getPath(), true )); 175 176 byte[] data = line.getBytes(); 178 bos.write( data ); 179 bos.flush(); 180 181 byte[] buffer = new byte[8192]; 183 int num_bytes; 184 while ( (num_bytes = bis.read(buffer)) > 0 ) { 185 bos.write( buffer, 0, num_bytes ); 186 bos.flush(); 187 total_bytes += num_bytes; 188 } 189 190 System.out.println( "appended "+total_bytes+" bytes."); 191 } 192 catch (IOException ioe) { 193 System.err.println(ioe); 194 retval = 1; 195 } 196 finally { 197 if (bis == null) try { bis.close(); } catch (IOException p) { ; } 198 if (bos == null) try { bos.close(); } catch (IOException q) { ; } 199 } 200 201 System.exit(retval); 202 } 203 } 204 | Popular Tags |