The example of this article tells the method of reading data from the JAR file of Java. Share it for everyone for your reference. The specifics are as follows:
Java archives (JAR) files are based on Java technology packaging solutions. They allow developers to pack all related content (.class, pictures, sound, support files, etc.) into a single file. JAR file format supports compression, authentication and versions, and many other features.
It is a tricky thing to get the file content contained in the JAR file, but it is not possible. This technique will tell you how to get a file from the jar file. We will get the file directory in this jar file first, and then read the specified file.
If you are familiar with the common ZIP format, you will find that the jar file is not much different from it. JAR file provides a method for packing multiple files to one file, and each file that is packaged can be compressed separately. JAR files can add a thing called Manifest, which allows developers to add other information related to content. For example, Manifest can indicate which files in the jar file to start running applications, or specify the version of the library.
Java 2 SDK Standard Edition provides a jar tool that you can read and write jar files through it through it. Then, maybe you want to read and write jar files in your program. (This technique only involves the content of reading jar files in the program.) Very happy, you can do it, and you don't need to consider the problem of decompression, because the class library has helped you handle it. The classes you need to use are in java.util.jar. The main class to be used here is the jarfile class, which is a .jar file itself. Each file is cited by Jarentry.
At the beginning, pass a parameter to create a jarfile instance for the constructor of jarfile. This parameter may be string or file. It is the position of a .jar file:
Copy code code as follows: jarfile jarfile = new jarfile ("thefile.jar");
Or copy the code code as follows: file file = new file ("thefile.jar");
Jarfile jarfile = new jarfile (file);
It also has some other constructors that support identity verification and mark files to delete. However, these constructors will not be 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 the 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.
// Translator Note: ENUM is a keyword in Java 5.0, so this case should compile failure in 5.0. But the original English book was published before the appearance of Java 5.0, so you can use Enum as a variable name // No modification 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 Getcompressetsize.
Let's give us an example how to use these features in the program. The following program shows the list of content of the jar file and the names, size, and the size of the compression. (This is similar to the jar command with the "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 Provideo a Jar Filename"); System.exit (-1);} jarfile jarfile = new jarfile (ARGS [0]); enumeration enum = jarfile.entries (); w hile (enum.hasmoreElements ()) {Process (ENUM.NEXTELEMENT ());}} Private Static Void Process (Object Obj) {Jarentry Entry = (Jarentry) Obj; String name = Entry.getName ( );; long compressedsize = Entry.getCompressetsize (); 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 like the following (more files should be displayed at the following ...): more files: more files): more files: more files):
META-INF/MANIFEST.MF 5315 1910META-INF/4JCEJARS.SF 5368 1958META-INF/4JCEJARS.DSA 2207 1503META-INF/ 0 2javax/ 0 0javax/crypto/ 0 0javax/crypto/interfaces/ 0 0javax/crypto/interfaces/ DHKEY.Class 209 185Javax/Crypto/Interfaces/DHPUBLICKEY.Class 265 215Javax/Crypto/Interfaces/DHPRIVATEKEY.Class 267 215JAVAX/Crypto/Into/Into ERFACES/PBEKEY.CLASS 268 224javax/Crypto/Secretkey.class 167 155 ...
Note that the top of the input content contains the lines of Meta-INF, which is Menifest and security verification information. The item with a size of 0 is not a file, but a directory.
To truly read the content of the file from the jar file, you must get the corresponding entry purpose inputStream. This is different from Jarentry. Jarentry only includes entry information, but does not include actual content. This is very similar to the difference between File and FileInputstrateam. When you only visit File, you will never open the corresponding file. It only reads the information in the directory. Here's how to get inputStream from an entry:
Copy code code as follows: inputStream input = jarfile.getinputstream (ENTRY);
After getting the input stream, you just need to read it like reading other streams. If it is a text flow, remember to use a Reader to get characters from the stream. For byte flow, such as pictures, you have to read it directly.
The following program demonstrates the content from a jar file. When running a program, you need to specify the file name to read from the jar file. This file must be a text file type.
Import java.io.*; Import java.util.jar.*; Public class jarread {Public Static void Main (String ARGS []) Throws IOEXCEPION {if (ARGS.Length! = 2) {System.out.println (" Please Provideo a Jar Filename and File to read "); System.exit (-1);} jarfile jarfile = new jarfile (ARGS [0]); Jarentry Entry = jarfile.getJarentry ]); InputStream input = jarfile .getInputStream (Entry); proces (input); jarfile.close ();} Private Static Void Process (InputStream Input) ThroWS IOEXCEPTION {InputStream I SR = New InputStreamReader (input); bufferedReader reader = new bufferedReader (ISR); string line; While ((line = reader.readline ())! = NULL) {system.out.println (line);} Reader.close ();}
Suppose you have a jar file called myfiles.jar, one of which is called spider.txt text file, and then assume that the pider.txt contains the following text:
The itSy Bitsy Spider Ran up the Water Spoutdown Came the Rain andwashed the Spider Out
Run the following command to display the contents of the text file in the jar file:
Copy code code as follows: java jarread myfiles.jar spider.txt
It is hoped that this article is helpful to everyone's Java program design.