Use inputstream in Java to read JAR/zip content

Source: Internet
Author: User

To read a zip/JAR file to the backend server as required by the project, the front-end user selects the zip/JAR file.

Later, after the Upload File, read the jarentry object cyclically from the jarfile object, and then

The API uses jarfile. getinputstream (jarentry) to read the content of each jarentry.

However, recently they want to convert the File Reading to direct reading based on inputstream, which is obtained from the HTTP request.

Inputstream can be read directly. In this case, how can we know each jarentry object from the stream?

After some research, we have 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(entry.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();}}}
Related Article

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.