Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getInputStream for a ZipEntry from ZipInputStream (without using the ZipFile class)

How can I get an InputStream for a ZipEntry from a ZipInputStream without using the ZipFile class?

like image 779
Mohammad Hassany Avatar asked Jan 30 '13 11:01

Mohammad Hassany


People also ask

How do I get files from ZipEntry?

To read the file represented by a ZipEntry you can obtain an InputStream from the ZipFile like this: ZipEntry zipEntry = zipFile. getEntry("dir/subdir/file1.

How do I get InputStream from ZipEntry?

Solution: open input stream from zip file ZipInputStream zipInputStream = ZipInputStream(new FileInputStream(zipfile) , run cycle zipInputStream. getNextEntry() . For every round you have the inputstream for current entry (opened for zip file before); .. This method is more generic than ZipFile.

How do I read a ZipInputStream file?

ZipInputStream. read(byte[] buf, int off, int len) method reads from the current ZIP entry into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

What is getInputStream() in Java?

This method returns the MIME type of the data in the form of a string. InputStream. getInputStream() This method returns an InputStream representing the data and throws the appropriate exception if it can not do so.


3 Answers

it works this way

static InputStream getInputStream(File zip, String entry) throws IOException {
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zip));
    for (ZipEntry e; (e = zin.getNextEntry()) != null;) {
        if (e.getName().equals(entry)) {
            return zin;
        }
    }
    throw new EOFException("Cannot find " + entry);
}

public static void main(String[] args) throws Exception {
    InputStream in = getInputStream(new File("f:/1.zip"), "launch4j/LICENSE.txt");
    Scanner sc = new Scanner(in);
    while(sc.hasNextLine()) {
        System.out.println(sc.nextLine());
    }
    in.close();
}
like image 184
Evgeniy Dorofeev Avatar answered Oct 24 '22 18:10

Evgeniy Dorofeev


Err, the ZipInputStream already is an InputStream. You don't need another one. Getting the next ZipEntry positions the stream at the beginning of the entry. See the Javadoc.

like image 23
user207421 Avatar answered Oct 24 '22 17:10

user207421


To return a List of Input Streams that can be used later I used the following

public static List<InputStream> listResourcesInJar(URL jar) throws IOException{
    ZipInputStream zipInputStream = new ZipInputStream(jar.openStream());
    ZipEntry zipEntry = null;

    List<InputStream> inputStreams = new ArrayList<>();

    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        String entryName = zipEntry.getName();
        if (entryName.endsWith(".xsd")) {
            inputStreams.add(convertToInputStream(zipInputStream));
        }
    }
    return inputStreams;
}

private static InputStream convertToInputStream(final ZipInputStream inputStreamIn) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(inputStreamIn, out);
    return new ByteArrayInputStream(out.toByteArray());
}
like image 1
Grant Avatar answered Oct 24 '22 18:10

Grant