Exploring a zip file

This post explains how to explore a zip file.

Main Code


package ZipDemo;

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

public class ZipDemo1 {
    public static void main(String[] args) {
        try (ZipFile zipFile = new ZipFile("E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\Folder1.zip")){
            Enumeration entries = zipFile.entries();
            for (Enumeration e = entries; e.hasMoreElements();) {
            ZipEntry entry = e.nextElement();
            System.out.println(entry.getName());
        }
        } catch(IOException excep) {
            excep.printStackTrace();
        }
    }
}

In the below code an instance of ZipFile is created which represents the zip file in the disk.
ZipFile zipFile = new ZipFile(“E:\Projects\JavaProjects\JavaConcepts\WorkFolder\Folder1.zip”)

Once the java representation of zip file is obtained, we can go through entries of the zip file as shown in below code. Each entry in the zip file is represented by an instance of ZipEntry. As shown below

for (Enumeration<? extends ZipEntry> e = entries; e.hasMoreElements();) {
ZipEntry entry = e.nextElement();
System.out.println(entry.getName());
}

We can get more information of an entry in the zip file using other get methods. Refer to javadoc api for more information

Output

Folder2/
Folder2\Folder21/
Folder2\Folder21/J2EE developer handbook.pdf
Folder2/spring-framework-reference.pdf
Folder2/springsecurity.pdf
Folder2/Version Control with Subversion.pdf
Folder3/
Folder3/Agile Methodology.pdf
Folder3/Fairley_Managing and Leading Software Projects_0470294558.pdf
Folder4/
Folder4/97-things-every-software-architect-should-know.pdf
Folder4/Wiley – Pattern-Oriented Software Architecture – A System Of Patterns, Volume 1.pdf
learning_uml_2.0.pdf
Logic For Dummies.pdf
UML 2.0 In A Nutshell.pdf
UML.docx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s