41

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Stephen
  • 6,027
  • 4
  • 37
  • 55
  • 3
    Look at: http://stackoverflow.com/questions/3233555/is-it-possible-to-get-a-zipentrys-inputstream-from-a-zipinputstream – diyism Jun 21 '12 at 03:45

3 Answers3

34

How about this?

ZipFile zipFile = new ZipFile("file.zip");
ZipEntry zipEntry = zipFile.getEntry("fileName.txt");       
InputStream inputStream = zipFile.getInputStream(zipEntry);
Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Ovi Tisler
  • 6,425
  • 3
  • 38
  • 63
27

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.

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • Malcolm, Thanks for the help, I just found that method. My problem now is that the code that originally 'opens' the zip file (when you do ZipFile zip = new ZipFile("path");), then closes it. I can stop it closing it if I want, but I was wondering - what are the consequences of not closing zips/jars? Obviously if I do remove the close() command I will leave a method so the user can close it later, but I was wondering what would happen if the user forgot - just greedy on memory? I know it isn't a wrapper for the data - what I'm writing is a wrapper for dealing with Zip/Jar files. – Stephen Nov 12 '09 at 16:28
  • 1
    Leaving it open doesn't feel like a good idea to me. Equally so for deferring responsibility for closing to the user of your solution. An option (possibly - I am guessing at the problem you are solving) is to wrap the ZipEntry and include with it a handle to the ZipFile in the wrapping class. This seems a little heavy but does mean that you can open\close a stream to the wrapped zip entry whenever you need. – Malcolm Featonby Nov 13 '09 at 05:49
  • 2
    What if I don't have it as a File, but I can always re-create the ZipInputStream (from a Uri, for example) ? Is it possible to have a way for each of the ZipInputStream's entries to create an InputStream, easily? – android developer Feb 29 '20 at 22:28
17
 static void printInputStream(File zip) throws IOException
    {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zip));
        for (ZipEntry zipEntry;(zipEntry = zin.getNextEntry()) != null; )
        {
            System.out.println("reading zipEntry " + zipEntry.getName());
            Scanner sc = new Scanner(zin);
            while (sc.hasNextLine())
            {
                System.out.println(sc.nextLine());
            }
            System.out.println("reading " + zipEntry.getName() + " completed");
        }
       zin.close();
    }

It was found here:
getInputStream for a ZipEntry from ZipInputStream (without using the ZipFile class)

Misunderstanding in what is the input stream that is opened from zip file. 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); ..

Community
  • 1
  • 1
  • 2
    This method is more generic than `ZipFile.getInputStream()` method since it doesn't require input coming from an actual file. So, it can be used to read an in-memory zip file content. – mmdemirbas May 17 '18 at 09:09
  • 2
    Don't forget to close the Scanner (`sc`). – Stephan Aug 18 '19 at 08:08