1

I'm trying to use a file selector (which is only allowed to select zips) to give me that File and then I will perform another method on every separate File in the zip. However, I'm struggling with the compiler as it is expected generic Files rather than zipFiles. I'm trying to use this example : the marked answer on this post

File zipFile = zfc.getSelectedFile();

        ZipInputStream zis;
        try {
            zis = new ZipInputStream(new FileInputStream(zipFile));

            ZipEntry ze;

            ze = zis.getNextEntry();

            while (ze != null) {

                    InputStream stream =  ((ZipFile) zipFile)
                            .getInputStream(ze);

                //method here that will operate on stream 

                ze = zis.getNextEntry();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Basically how can I read a File from a zip in java or how do I change a zipEntry to a File.

Community
  • 1
  • 1
Steven
  • 93
  • 1
  • 11
  • If I understood you correctly ... you need to extract all zip files you find, then go over each file you extracted. So, in your first while loop you extract all zip files into a temporary folder, then in a second loop you go over all the extracted files and do-something-else. – Eric Oct 27 '14 at 14:37

2 Answers2

2

A java.io.File represents a name of a file physically existing (or not) on some attached storage device.

A ZipEntry on the other hand is just some meta-data that help you access the content of the specific entry inside the .zip file.

To answer your first question: if you MUST have a java.io.File instance, then the quickest way is to extract the whole zip file (or the parts you want) onto the disk, and then use File from then on.

But, quite possibly the thing you want to do doesn't need to use a File, and could work with an InputStream instead. In that case you can just provide it the stream instance you have there. This will deliver better performance, and avoids working with the file system (create files, use them, make sure to clean up, etc).

ankon
  • 4,128
  • 2
  • 26
  • 26
2

You need to create a ZipFile object to iterate over its entries. Try:

ZipFile zipFile = new ZipFile(zfc.getSelectedFile());

...and then continue with the example you linked.

mauretto
  • 3,183
  • 3
  • 27
  • 28