I'm receiving a ZipInputStream from another source, and I need to provide the first entry's InputStream to another source.
I was hoping to be able to do this without saving a temp file on a device, however the only way I know of getting an InputStream for an individual entry is via ZipFile.getInputStream(entry) and since I have a ZipInputStream and not a ZipFile, that is not possible.
So the best solution I have is
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.
String zipName = file. getName(); String zipType = file. getMimeType(); InputStream zipStream = file. getInputStream(); ZipInputStream zis = new ZipInputStream(zipStream); System.
public class ZipEntry extends Object implements Cloneable. This class is used to represent a ZIP file entry.
The ZipInputStream's getNextEntry () reads the next ZIP file entry and positions the stream at the beginning of the entry data. The following example reads the contents of a ZIP file. The example reads the given ZIP file with ZipInputStream and prints its contents to the terminal. We print the file names, their size, and the last modification time.
A ZipInputStream is created from the buffered FileInputStream . The try-with-resources closes the streams when they are not needed anymore. In a while loop, we go through the entries of the ZIP file with getNextEntry () method. It returns null if there are no more entries.
A ZipInputStream is created from the buffered FileInputStream. The try-with-resources closes the streams when they are not needed anymore. while ((ze = zis.getNextEntry ()) != null) { In a while loop, we go through the entries of the ZIP file with getNextEntry () method.
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.
figured:
it's entirely possible, the call to ZipInputStream.getNextEntry()
positions the InputStream
at the start of the entry and therefore supplying the ZipInputStream
is the equivalent of supplying a ZipEntry
's InputStream
.
the ZipInputStream
is smart enough to handle the entry's EOF downstream, or so it seems.
p.
In addition to @pstanton post here is an example of code. I solved the problem using the following code. It was difficult to understand what the previous answer without an example.
//If you just want the first file in the zipped InputStream use this code. //Otherwise loop through the InputStream using getNextEntry() //till you find the file you want. private InputStream convertToInputStream(InputStream stream) throws IOException { ZipInputStream zis = new ZipInputStream(stream); zis.getNextEntry(); return zis; }
Using this code you can return an InputStream of the file that is zipped.
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