Jar/zip content is read in Java using InputStream __java

Source: Internet
Author: User

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 (); }
	}
	
}

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.