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

Zip File 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/ZipFileEntries.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.zip;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipFileEntries {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] args) {

    String srcZipFilePath = "C:\\abc.zip";

    try {
      InputStream in = new FileInputStream(srcZipFilePath);
      ZipInputStream zin = new ZipInputStream(in);
      ZipEntry entry = null;
      String str = null;
      
      while ((entry = zin.getNextEntry()) != null) {
        
        str = "[" (entry.isDirectory() " DIR" "FILE""] " + entry.getName();
        
        if (!entry.isDirectory()) {
          str+= " [" + entry.getCompressedSize() "/" + entry.getSize() " bytes]";
        }
        
        System.out.println(str);
        zin.closeEntry();
      }
      
      zin.close();
      
    catch (FileNotFoundException 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