Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to get a zipentry's inputstream from a zipinputstream?

Tags:

java

zip

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

  1. save incoming InputStream to a file
  2. read file as ZipFile
  3. use first entry's InputStream
  4. delete temp file.
like image 658
pstanton Avatar asked Jul 13 '10 01:07

pstanton


People also ask

How do I get input stream from ZIP entry?

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 to read from ZipInputStream In java?

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.

How do you convert InputStream to ZipInputStream?

String zipName = file. getName(); String zipType = file. getMimeType(); InputStream zipStream = file. getInputStream(); ZipInputStream zis = new ZipInputStream(zipStream); System.

What is ZipEntry in Java?

public class ZipEntry extends Object implements Cloneable. This class is used to represent a ZIP file entry.

How to read the contents of a zip file using zipinputstream?

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.

How do I get next entry in ZIP input stream?

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.

How to create a zipinputstream using try-with-resources in Java?

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.

What is the difference between a zipentry and a zipfile?

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.


2 Answers

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.

like image 96
pstanton Avatar answered Sep 22 '22 19:09

pstanton


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.

like image 26
Whitecat Avatar answered Sep 21 '22 19:09

Whitecat