简体   繁体   中英

How to speed up parsing of entries in zip file in Java

I have a web service parsing zip files for their internal file structure on the fly (file names, directories etc...) and sending the result back as a json. It works fine except that the zip files contain some very large files and it can take up to several tens of seconds to get the answer back, even when only a few files are in the zip file. I am suspecting the unzip algorithm has to parse the whole file to get to the different zip entries. I am using the standard java zip library. Here is some basic code:

        ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(myFilePath)));
        ZipEntry zipEntry = zis.getNextEntry();
        while(zipEntry != null){
            // code to generate the json file tree ---
            // --- end of code
            zipEntry = zis.getNextEntry();
        }

I am not reading the content of the files or anything, just iterating through the entries. In fact I could run it empty like above and it would still take a lot of time (and CPU).

Is there a way to speed up this process? a better library maybe?

Thanks!

As per comments, the better way of doing this is, ie use ZipFile and not ZipInputStream

   final ZipFile file = new ZipFile(filePath);
       final Enumeration<? extends ZipEntry> entries = file.entries();
       while ( entries.hasMoreElements() ) {
           final ZipEntry zipEntry = entries.nextElement();
           // code to generate the json file tree ---
           // --- end of code
       }
   }

All credits should go to Wasim Wani's answer to the following question Reading zip file efficiently in Java That's where I found the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM