First, construct the URL
Generate Resource Url://jar:file:/d:\workspace\project\test.jar!/plugin.xml
URL FileURL = new URL ("jar:file:/" +dirpath+ "!/plugin.xml");
Returns the byte stream for this URL via Java.url.openStream ().
InputStream input = Fileurl.openstream ();
InputStreamReader in = new InputStreamReader (input);//create input Read stream
BufferedReader reader = new BufferedReader (in);//create buffered Read stream
String Line;
while (line = Reader.readline ())!= null) {//loop to display file contents
System.out.println (line);
}
Reader.close ()//close buffered read stream
Jarfile.close ()//closes the jar file object stream.
Second, the use of tool-type java.util.jar.*
Jarfile jarfile = new Jarfile (jarfilename);//Create a Jar file object from an incoming JAR file
Jarentry entry = jarfile.getjarentry ("Plugin.xml");//Get a jar entity for a single file in the jar file
InputStream input = Jarfile.getinputstream (entry);//create an input stream from an entity
InputStreamReader in = new InputStreamReader (input);//create input Read stream
BufferedReader reader = new BufferedReader (in);//create buffered Read stream
String Line;
while (line = Reader.readline ())!= null) {//loop to display file contents
System.out.println (line);
}
Reader.close ()//close buffered read stream
Jarfile.close ()//Close the jar file object stream
The first method to use when doing the project is to directly treat the jar as a folder and construct a URL. However, it was later found that this method does not apply in the Linux environment, and the constructed URL illegally causes the inability to use OpenStream ().
Therefore, the second method is recommended.
Detailed jar package processing can be found in this blog http://javasam.iteye.com/blog/1486803 provides the method:
Package com.file;
Import java.util.*;
Import java.util.jar.*;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
public class Operatorjar {//Operation jar file classes
public static void Readjarlist (String fileName) throws IOException {//Display jar file contents list
Jarfile jarfile = new Jarfile (fileName); Creating a Jar File object
Enumeration en = Jarfile.entries (); Enumeration gets the entities within the jar file, that is, relative paths
SYSTEM.OUT.PRINTLN ("filename \ t file size \ t compressed size");
while (En.hasmoreelements ()) {//traverse the display of content information in a jar file
Process (En.nextelement ()); Calling method Display content
}
}
private static void Process (object obj) {//Display object information
Jarentry entry = (jarentry) obj;//object into a Jar object
String name = Entry.getname ();//File name
Long size = Entry.getsize ();//File size
Long compressedsize = Entry.getcompressedsize ()//Compressed size
SYSTEM.OUT.PRINTLN (name + "T" + size + "\ T" + compressedsize);
}
public static void Readjarfile (String jarfilename, String fileName)
Throws IOException {//Read the individual file information in the jar file
Jarfile jarfile = new Jarfile (jarfilename);//Create a Jar file object from an incoming JAR file
Jarentry entry = Jarfile.getjarentry (fileName);//Get a jar entity for a single file in the jar file
InputStream input = Jarfile.getinputstream (entry);//create an input stream from an entity
ReadFile (input);//Call method get file information
Jarfile.close ()//Close the jar file object stream
}
public static void ReadFile (InputStream input) throws IOException {//Read the individual file information in the jar file
InputStreamReader in = new InputStreamReader (input);//create input Read stream
BufferedReader reader = new BufferedReader (in);//create buffered Read stream
String Line;
while (line = Reader.readline ())!= null) {//loop to display file contents
System.out.println (line);
}
Reader.close ()//close buffered read stream
}
public static void Main (String args[]) throws IOException {//Java program main entrance
Operatorjar j = new Operatorjar ();
System.out.println ("1. Enter a jar file (including paths and suffixes)");
Scanner scan = new Scanner (system.in);//Keyboard input value
String jarfilename = Scan.next ();//Get the value of the keyboard input
Readjarlist (jarfilename);//calling method displaying file information in a jar file
System.out.println ("2. View which file information in the jar file?");
String fileName = Scan.next ();//Keyboard input value
Readjarfile (Jarfilename, fileName);//Get the value of the keyboard input
}
}