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.
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.
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.
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.
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.
How about this?
ZipFile zipFile = new ZipFile("file.zip"); ZipEntry zipEntry = zipFile.getEntry("fileName.txt"); InputStream inputStream = zipFile.getInputStream(zipEntry);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With