Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an input stream from a ZipEntry

Tags:

java

zip

I have a class which wraps ZipEntrys, but I'm struggling to see how I could then write a method that returns an input stream from any one ZipEntry. I managed to write something that could return an array of input streams for a ZipFile, but I need a way to get an input stream from just one ZipEntry.

like image 917
Stephen Avatar asked Nov 12 '09 15:11

Stephen


People also ask

How do I get input stream 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?

getInputStream() This method returns an InputStream representing the data and throws the appropriate exception if it can not do so. String. getName() Return the name of this object where the name of the object is dependant on the nature of the underlying objects.

What is ZipInputStream?

ZipInputStream is a Java class that implements an input stream filter for reading files in the ZIP file format. It has support for both compressed and uncompressed entries.


2 Answers

How about this?

ZipFile zipFile = new ZipFile("file.zip"); ZipEntry zipEntry = zipFile.getEntry("fileName.txt");        InputStream inputStream = zipFile.getInputStream(zipEntry); 
like image 154
Ovi Tisler Avatar answered Sep 28 '22 07:09

Ovi Tisler


Do you not have the ZipFile instance from which the ZipEntry was sourced? If you do you could use ZipFile.getInputStream(ZipEntry).

https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipFile.html

PS. Just had a quick look at the code and a ZipEntry is not a wrapper for the underlying data in the zip file. It is just a "place holder" for the entry as far as I can see (i.e. zipped file attributes not the data). The actual stream is created through a JNI call in the ZipFile class. Meaning that I do not believe you can do what you are looking to do in a practical way.

like image 33
Malcolm Featonby Avatar answered Sep 28 '22 06:09

Malcolm Featonby