Because the project needs, to read a Zip/jar file into the backend server, the front desk users choose a good Zip/jar file
Later, after the file is upload, the Jarentry object is recycled from the Jarfile object and then based on the Jarfile
The API uses Jarfile.getinputstream (Jarentry jarentry) to read the contents of each jarentry.
But lately, they want to read the file into a direct read based on InputStream, and get it from the HTTP request
InputStream can be read directly after. So the question is, how do you know each Jarentry object from the stream?
After some research, there is the following code:
Package com.gloomyfish.image.study;
Import Java.io.BufferedInputStream;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.jar.JarEntry;
Import Java.util.jar.JarInputStream;
Import Java.util.jar.Manifest;
public class Jarfilereader {private Jarinputstream jarinput;
Private hashmap<string, bytearrayoutputstream> Entriesstreammap;
Public Jarfilereader (InputStream in) throws IOException {jarinput = new jarinputstream (in);
Entriesstreammap = new hashmap<string, bytearrayoutputstream> ();
public void Readentries () throws IOException {Jarentry entry = Jarinput.getnextjarentry ();
String manifestentry = null;
while (entry!= null) {System.out.println ("entry Name =" + Entry.getname ()); if ("Jp/co/sony/csl/nielsen/phoenix/objectremoval/mark.class". Equals (E)Ntry.getname ()) {manifestentry = Entry.getname ();
Copyinputstream (Jarinput, Entry.getname ());
} entry = Jarinput.getnextjarentry (); } System.out.println ("now!!
+ Get jar entry InputStream again ... ");
InputStream InputStream = getcopy (manifestentry);
System.out.println (Inputstream.read ()); } public void Copyinputstream (InputStream in, String entryname) throws IOException {if (!entriesstreammap.containskey (EntryName))
{Bytearrayoutputstream _copy = new Bytearrayoutputstream ();
int read = 0;
int chunk = 0;
byte[] data = new BYTE[256];
while ( -1!= (chunk = in.read (data)) {read = = Data.length;
_copy.write (data, 0, chunk);
} entriesstreammap.put (EntryName, _copy);
} public InputStream getcopy (String entryname) {Bytearrayoutputstream _copy = Entriesstreammap.get (entryname);
Return (InputStream) New Bytearrayinputstream (_copy.tobytearray ()); public static void Main (string[] args) {file Jarfile = new File ("D: \\agriculture\\clickremoval.jar ");
try {inputstream in = new Bufferedinputstream (new FileInputStream (Jarfile));
Jarfilereader reader = new Jarfilereader (in);
Reader.readentries ();
catch (IOException e) {e.printstacktrace (); }
}
}