Java archive (jar) files are packaged Based on Java technology. They allow developers to package all relevant content (. Class, images, sounds, and support files) into a single file. The jar file format supports compression, authentication, version, and many other features.
It is difficult to obtain the file content contained in the jar file, but it is not impossible. This technique will show you how to obtain a file from a jar file. We will first obtain the file directory in the jar file and then read the specified file.
If you are familiar with common zip formats, you will find that jar files are not much different from them. The jar file provides a method to package multiple files into one file, and each packaged file can be compressed separately. Jar files can be used to add something called manifest, which allows developers to add other information related to the content. For example, manifest can specify which file in the jar file starts to run the application, or specify the version of the library.
Java 2 SDK Standard Edition provides a jar tool that allows you to read and write jar files on the console. Then, you may want to read and write jar files in your program. (This technique only involves reading jar files in a program .) We are very happy that you can do this without having to worry about decompression, because the class library has already helped you deal with it. All the classes you want to use are in the Java. util. jar package. The main class used here is the jarfile class, which is a reference of the. jar file itself. Each file is referenced by jarentry.
Now, pass a parameter to the jarfile constructor to create a jarfile instance. This parameter may be string or file, and it is the location of A. jar file:
Jarfile = new jarfile ("thefile. Jar ");
Or
File file = new file ("thefile. Jar ");
Jarfile = new jarfile (File );
It also has some other constructor that supports authentication and file deletion. However, the constructor is not involved here.
After you get a reference to a jar file, you can read the directory of its content. The entries method of jarfile returns an enumeration object of all entries. You can also obtain its attributes, authentication information, and other information from the manifest file, such as the name and size of the entry.
Enumeration Enum = jarfile. Entries ();
While (enum. hasmoreelements ()){
Process (enum. nextelement ());
}
As mentioned before, each individual is a jarentry. This class has some methods such as getname, getsize, and getcompressedsize.
Let's take an example to illustrate how to use these features in a program. The following program displays the content list of the JAR file and the name, size, and compressed size of each item. (This is similar to the jar command with "T" and "V" parameters .)
Import java. Io .*;
Import java. util .*;
Import java. util. Jar .*;
Public class jardir {
Public static void main (string ARGs [])
Throws ioexception {
If (ARGs. length! = 1 ){
System. Out. println ("Please provide a jar FILENAME ");
System. Exit (-1 );
}
Jarfile = new jarfile (ARGs [0]);
Enumeration Enum = jarfile. Entries ();
While (enum. hasmoreelements ()){
Process (enum. nextelement ());
}
}
Private Static void process (Object OBJ ){
Jarentry entry = (jarentry) OBJ;
String name = entry. getname ();
Long size = entry. getsize ();
Long compressedsize = entry. getcompressedsize ();
System. Out. println (name + "" + size + "" + compressedsize );
}
}
If you use JCE. jar in j2se 1.4.1 to test the above jardir program, you should see the output as below (more files should be displayed in ):
META-INF/manifest. MF 5315 1910
META-INF/4jcejars. SF 5368 1958
META-INF/4jcejars. DSA 2207 1503
META-INF/0 2
Javax/0 0
Javax/crypto/0 0
Javax/crypto/interfaces/0 0
Javax/crypto/interfaces/dhkey. Class 209 185
Javax/crypto/interfaces/dhpublickey. Class 265 215
Javax/crypto/interfaces/dhprivatekey. Class 267 215
Javax/crypto/interfaces/pbekey. Class 268 224
Javax/crypto/secretkey. Class 167 155
...
Note the lines at the top of the input content that contain the META-INF, which are menifest and security verification information. The entry 0 is not a file, but a directory.
To actually read the file content from the jar file, you must obtain the corresponding inputstream. This is different from jarentry. Jarentry only contains the entry information, but does not contain the actual content. This is similar to the difference between file and fileinputsteram. Only access the file and never open the corresponding file. It only reads information in the directory. The following shows how to get inputstream from an entry:
Inputstream input = jarfile. getinputstream (entry );
After the input stream is obtained, you only need to read it like other streams. For a text stream, remember to use a reader to retrieve characters from the stream. For byte streams, slices have to be read directly.
The following program demonstrates reading content from a jar file. When running the program, you must specify the file name to be read from the jar file. This file must be of the text file type.
Import java. Io .*;
Import java. util. Jar .*;
Public class jarread {
Public static void main (string ARGs [])
Throws ioexception {
If (ARGs. length! = 2 ){
System. Out. println ("Please provide a jar filename and file to read ");
System. Exit (-1 );
}
Jarfile = new jarfile (ARGs [0]);
Jarentry entry = jarfile. getjarentry (ARGs [1]);
Inputstream input = jarfile. getinputstream (entry );
Process (input );
Jarfile. Close ();
}
Private Static void process (inputstream input)
Throws ioexception {
Inputstreamreader ISR =
New inputstreamreader (input );
Bufferedreader reader = new bufferedreader (ISR );
String line;
While (line = reader. Readline ())! = NULL ){
System. Out. println (line );
}
Reader. Close ();
}
}
Assume that you have a jar file named myfiles. jar, which has a text file named spider.txt, And that pider.txt contains the following text:
The itsy bitsy spider
Ran up the water spout
Down came the rain and
Initialized ed the spider out
Run the following command to display the content of the text file in the jar file:
Java jarread myfiles. Jar spider.txt
For more information about jar files, see JAR File Specification.
Original article: http://developer.java.sun.com/developer/JDCTechTips/2003/tt0122.html#1