Ways to read data from a Java JAR file

Source: Internet
Author: User

This article mainly introduces the method of reading data from Java JAR file, and analyzes the related operation skills of Java files, and the friends who need it can refer to the following

The example in this article describes how to read data from a Java jar file. Share to everyone for your reference. as follows:

The Java archive (Java Archive, JAR) file is a packaged solution based on Java technology. They allow developers to package all relevant content (. class, pictures, sounds, and supporting files, etc.) into a single file. The JAR file format supports compression, authentication, and versioning, as well as many other features.

Getting the file content it contains from a JAR file is tricky, but it's not something you can't do. This tip will tell you how to get a file from the JAR file. We will first get the file directory in this JAR file, and then read the specified file.

If you are familiar with the common ZIP format, you will find that the JAR file does not differ from it. The JAR file provides a way to package multiple files into a single file, and each file that is packaged can be compressed separately. A JAR file can add something called manifest that allows developers to add additional information about the content. For example, manifest can indicate which file in the JAR file started running the application, or specify the version of the library, and so on.

The Java 2 SDK Standard Edition provides a Jar tool through which you can read and write jar files under the console. Then, there may be times when you want to read and write JAR files in your program. (This tip only involves reading the contents of a JAR file in a program.) Very happy, you can do it, and do not need to consider the problem of decompression, because the class library has been to help you deal with. The classes you want to use are in the Java.util.jar package. The main class to use here is the Jarfile class, which is a reference to the. jar file itself. Each of these files is referenced by Jarentry.

Now, pass a parameter to create a Jarfile instance of the Jarfile constructor, which may be String or file, which is the location of a. jar file:

The code is as follows:

Jarfile jarfile = new Jarfile ("Thefile.jar");

Or

The code is as follows:

File File = new file ("Thefile.jar");

Jarfile jarfile = new Jarfile (file);

It also has some other constructors that support authentication and tag files for deletion. But these constructors are not involved here.

After you have a reference to a JAR file, you can read the contents of the directory. Jarfile's Entries method returns a enumeration object for all entries, and you can also obtain its properties, authentication information, and other information, such as the name and size of the entry, from the manifest file.

?

1 2 3 4 5 6 7 Translator Note: Enum in Java 5.0 is a keyword, so this example should be compiled in 5.0 failed//But the original English version was published before Java 5.0, so you can use the enum as the variable name//True to the original, there is no modification enumeration enum = Jarfile.entries (); while (Enum.hasmoreelements ()) {process (enum.nextelement ());}

It has been mentioned before that each individual is a jarentry. This class has some methods such as GetName, GetSize, and Getcompressedsize.

Let's illustrate how to use these features in a program. The following program displays a list of the contents of the JAR file and the name, size, and size of the items. (This is similar to using JAR commands with "T" and "V" parameters.) )

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 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.prin TLN ("Please provide a JAR filename"); System.exit (-1); } jarfile 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 the Jce.jar in J2SE 1.4.1 to test the Jardir program above, you should see output like the one below. Should show more files):

?

1 2 3 4 5 6 7 8 9 10 11 12-13 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 that the top of the input contains meta-inf lines, which are menifest and security verification information. Items with a size of 0 are not files, but directories.

To actually read the contents of the file from the JAR file, you must obtain the InputStream of the corresponding entry. This is different from jarentry. Jarentry contains only the entry information, but not the actual content. It's like the difference between File and Fileinputsteram. Access to file only, never opens the appropriate files, it only reads information in the directory. Here's how to get InputStream from an entry:

Copy code code as follows:

InputStream input = Jarfile.getinputstream (entry);

After you get the input stream, you just need to read it just as you would any other stream. If it's a text stream, remember to use a Reader to get the characters from the stream. And for the byte stream, such as the picture, you have to read directly.

The following program shows the reading of content from a JAR file. When you run the program, you need to specify the name of the file to read from the JAR file, which must be a text file type.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Import java.io.*; Import java.util.jar.*; public class Jarread {public static void main (String args[]) throws IOException {if (args.length!= 2) {System.out.pri NTLN ("Please provide a JAR filename and file to read"); System.exit (-1); } jarfile 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 (); } }

Suppose you have a jar file called Myfiles.jar, which has a text file called Spider.txt, and assume that pider.txt contains the following text:

?

1 2 3 4 The Itsy Bitsy Spider Ran up the water spout down came the rain and washed the spider out

Run the following command to display the contents of the text file in the JAR file:

The code is as follows:

Java jarread Myfiles.jar Spider.txt

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.