Wednesday, July 9, 2014

Findout number of files in Zip

How to findout number of files in Zip files in java



    /**
     * returns number of files in zip
     * @param zin
     * @return
     */
    public int countNumberOfFileInZip(ZipInputStream zin) {
        int numFiles = 0;
        try {

            while (zin.getNextEntry() != null) {
                numFiles++;
            }
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {

                logger.error(" $$$$$$$$$$$$$  Error on zip file count processing :$$$$$$$$$$$ "+ e.getMessage());
            }
        } finally {
            if (zin != null) {
                try {
                    zin.closeEntry();
                    zin.close();
                } catch (Exception e) {
                    logger.error(" Error on zip file entry close : " + e.getMessage());
                }

            }
        }   

        return numFiles;
    }

Test :
              // where "writeToFile" is the byte array of zip file content
              // get the zip file content
              bis = new ByteArrayInputStream(writeToFile);

            // get the zipped file list entry           
            int zipFileCount = countNumberOfFileInZip(new ZipInputStream(new    
                                          ByteArrayInputStream(writeToFile)));



No comments:

Post a Comment