Sunday, March 13, 2016

Java ZIP Examples : How To Read A Contents Of The ZIP File

Intro

This examples will demonstrates how to read a contents of the zip file using Java program.

Examples


package com.freesamplecode.java.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 ReadZipFileDemo {
 public static void main(String[] args){
  File file = new File("D:/temp/data.zip");
  
  if(!file.exists()){
   System.out.println("File " +file.getName() +" does not exists");
  }
  
  System.out.println("Reading content a zip file....");
  System.out.println("File Name "+"\t"+"\t"+"\t"+"\t"+"Size");
  System.out.println("-----------------------------------------------");
  
  try {
   ZipFile zipFile = new ZipFile(file);
   Enumeration enumeration = zipFile.entries();
   
   while(enumeration.hasMoreElements()){
    ZipEntry ze = (ZipEntry) enumeration.nextElement();
    
    if(!ze.isDirectory()){
     System.out.println(ze.toString()+"\t"+"\t"+ze.getSize()+" byte");
     
    }
   }
  } catch (ZipException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
}


Output


Reading content a zip file....
File Name     Size
-----------------------------------------------
D:\temp\data\aku - Copy (2).txt  0 byte
D:\temp\data\aku - Copy (3).txt  0 byte
D:\temp\data\aku - Copy (4).txt  0 byte
D:\temp\data\aku - Copy (5).txt  0 byte
D:\temp\data\aku - Copy.txt  0 byte
D:\temp\data\aku.txt  0 byte


Screenshot

How To Read A Contents Of The ZIP File

Related Posts:

0 comments: