KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MakeSelfExtractor


1 /* $Id$
2  *
3  * Copyright (c) 1998 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 // Extractor.java
17

18 // Description:
19
// A class to extract the package file into the current directory.
20
//
21
// Author:
22
// Peter Pilgrim
23
// Wed Feb 10 05:51:34 GMT 1999
24
//
25
// RCS HEADER
26
//
27
// $Author$
28
// $Date$
29
// $Source$
30
// $Revision$ $State$ $Locker$
31
//
32
// History
33
// ================================================================================
34
// $Log$
35

36
37 import java.io.*; // for File I/O Classes
38
import java.util.*; // for Vector/Hashtable
39

40 /*
41  * Extractor is a self extracting java class that can package a software
42  * distribution.
43  *
44  * The extractor class works by searching for a special key line
45  * in the class file, which it opens. The special key line is appended
46  * to the end of the class file and the distribution zip or jar file
47  * is append afterwards.
48  *
49  * The key line consist of the key value, the archive type,
50  * and the name of the java class that contains the
51  * <I> `public static void main( String [] )'</I>.
52  *
53  * The key line is:
54  * <PRE>
55  *
56  * KEY_LINE:
57  * KEY_VALUE '|' ARCHIVE_TYPE '|' CLASS_NAME '|' EXTRA_CLASS_PATH '|' '\n' ;
58  *
59  * Where:
60  * KEY_VALUE: "-=+ARCHIVE+=-" ;
61  *
62  * ARCHIVE_TYPE: currently only "ZIP" is recognised
63  * and case insensitive;
64  *
65  * CLASS_NAME: the java software package name ;
66  * ( e.g. "xenon.free.install.FreeInstaller" )
67  *
68  * EXTRA_CLASS_PATH: the extra class path to prepend to the
69  * default class path. For example to specify a
70  * pathname to JAR and/or ZIP files.
71  *
72  * </PRE>
73  *
74  * @see runApplicationInstaller
75  * @author Peter Pilgrim
76  * @version 1.0 , Mon Feb 22 23:07:13 GMT 1999
77  */

78 public class MakeSelfExtractor {
79
80     public final static boolean debug=true;
81
82     public final static String JavaDoc 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 JavaDoc EXTRACTOR_CLASS = "Extractor.class";
99     
100     /** The zip filename */
101     protected static String JavaDoc archiveFilename="";
102     /** The archive type */
103     protected static String JavaDoc archiveType="ZIP";
104     /** The class name to load dynamically and to call `main()' */
105     protected static String JavaDoc appClassName="";
106     /** The extra class path */
107     protected static String JavaDoc extraClassPath="";
108
109
110     public static void error( String JavaDoc message )
111     {
112     System.err.println("MakeSelfExtractor: "+message);
113     System.exit(1);
114     }
115
116     /** Creates a self extracting archive class */
117     public static void main( String JavaDoc [] args )
118     {
119     boolean acceptFlag=false;
120     int j;
121     
122     for (j=0; j<args.length; ++j) {
123         String JavaDoc 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 JavaDoc 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         // Write out the archive line.
177
byte[] data = line.getBytes();
178         bos.write( data );
179         bos.flush();
180         
181         // Append the archive file to the end of the self extractor
182
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