tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Zip Utilities > Show Zip Entries

Show Zip Entries 

The following example shows how to iterate through the entries of a zip file. We can check whether the current entry is file or directory using entry.isDirectory() method. For files we can get the actual file size and compressed file size in bytes using entry.getSize() and entry.getCompressedSize() methods.

File Name  :  
com/bethecoder/tutorials/utils/zip/ShowZipEntries.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.zip;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class ShowZipEntries {

  public static void main(String[] args) {
  
    String zipFilePath = "C:\\abc.zip";
    showZipEntries(zipFilePath);  
  }

  public static void showZipEntries(String zipFilePath) {
    
    try {
      ZipFile zip = new ZipFile(new File(zipFilePath));
      ZipEntry entry = null;
      String str = null;
      
      for (Enumeration<?> e = zip.entries(); e.hasMoreElements();) {

        entry = (ZipEntrye.nextElement();
        str = "[" (entry.isDirectory() " DIR" "FILE""] " + entry.getName();
        
        if (!entry.isDirectory()) {
          str+= " [" + entry.getCompressedSize() "/" + entry.getSize() " bytes]";
        }
        
        System.out.println(str);
      }

    catch (ZipException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}
   

It gives the following output,
[FILE] prop_xml_file.xml [193/328 bytes]
[FILE] test.txt [32/32 bytes]
[FILE] software.tsv [4699/19424 bytes]
[FILE] abc.txt [48/91 bytes]
[ DIR] TWO/FOUR/
[ DIR] TWO/FOUR/FIVE/
[FILE] TWO/FOUR/FIVE/abc.txt [21/28 bytes]
[FILE] TWO/FOUR/FIVE/software.tsv [4699/19424 bytes]
[FILE] TWO/FOUR/prop_xml_file.xml [193/328 bytes]
[FILE] TWO/prop_xml_file.xml [193/328 bytes]
[FILE] TWO/software.tsv [4699/19424 bytes]
[FILE] TWO/test.txt [32/32 bytes]
[ DIR] TWO/
[FILE] ONE/test.txt [32/32 bytes]
[ DIR] ONE/



 
  


  
bl  br