• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To List Contents Of Zip File using Java

April 21, 2014 //  by Krishna Srinivasan//  Leave a Comment

This example shows how to list the files inside a zip file. Zip is the most widely used utility for compressing the files and it is the Windows platform standard. In cases where you would need to work on the Zip file in Java for reading and writing into zip file. Here I write a simple example to print the number of entries in the zip file and how to list them.  We use the following API for this task.

  • java.util.zip.ZipFile – This API used for creating the ZIp file instance and reading it
  • java.util.zip.ZipEntry – This API holds the details of the each entry in the ZIp file

Lets look at the example on how to list the files inside zip file PrimeFaces-Collector.zip. This example first get the number of entries in the file using the size() method and then get the list of entries using the method entries().

ListFilesZipExample.java

package javabeat.net.zip;

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

/**
 * List Zip File Example
 *
 * @author krishna
 *
 */
public class ListFilesZipExample {
	public static void main(String[] args) {
		ZipFile zipFile = null;
		try {
			// Create ZipFile instance
			zipFile = new ZipFile("PrimeFaces-Collector.zip");

			// Get the zip entries
			Enumeration<? extends ZipEntry> e = zipFile.entries();
			int counter = 1;
			System.out.println("Number of Entries : " + zipFile.size());
			while (e.hasMoreElements()) {
				ZipEntry entry = e.nextElement();
				// Get the entry
				String entryName = entry.getName();
				System.out.println("ZIP Entry " + counter + " : " + entryName);
				counter++;
			}

		} catch (IOException ioe) {
			System.out.println("Error opening zip file" + ioe);
		} finally {
			try {
				if (zipFile != null) {
					zipFile.close();
				}
			} catch (IOException ioe) {
				System.out.println("Error while closing zip file" + ioe);
			}
		}

	}

}

Output…

Number of Entries : 50
ZIP Entry 1 : PrimeFaces-Collector/src/main/java/net/javabeat/primefaces/data/User.java
ZIP Entry 2 : PrimeFaces-Collector/src/main/java/net/javabeat/primefaces/RegistrationBean.java
ZIP Entry 3 : PrimeFaces-Collector/.metadata/src/main/webapp/WEB-INF/faces-config.pageflow
ZIP Entry 4 : PrimeFaces-Collector/src/main/webapp/resources/images/ajax-loader.gif
ZIP Entry 5 : PrimeFaces-Collector/src/main/webapp/resources/js/autocomplete.js
ZIP Entry 6 : PrimeFaces-Collector/src/main/webapp/WEB-INF/faces-config.xml
ZIP Entry 7 : PrimeFaces-Collector/src/main/webapp/WEB-INF/web.xml
ZIP Entry 8 : PrimeFaces-Collector/src/main/webapp/index.xhtml
ZIP Entry 9 : PrimeFaces-Collector/.settings/.jsdtscope
ZIP Entry 10 : PrimeFaces-Collector/.settings/org.codehaus.groovy.eclipse.core.prefs
ZIP Entry 11 : PrimeFaces-Collector/.settings/org.eclipse.core.resources.prefs
ZIP Entry 12 : PrimeFaces-Collector/.settings/org.eclipse.jdt.core.prefs
ZIP Entry 13 : PrimeFaces-Collector/.settings/org.eclipse.jdt.groovy.core.prefs
ZIP Entry 14 : PrimeFaces-Collector/.settings/org.eclipse.jpt.core.prefs
ZIP Entry 15 : PrimeFaces-Collector/.settings/org.eclipse.m2e.core.prefs
ZIP Entry 16 : PrimeFaces-Collector/.settings/org.eclipse.wst.common.component
ZIP Entry 17 : PrimeFaces-Collector/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml
ZIP Entry 18 : PrimeFaces-Collector/.settings/org.eclipse.wst.common.project.facet.core.xml
ZIP Entry 19 : PrimeFaces-Collector/.settings/org.eclipse.wst.jsdt.ui.superType.container
ZIP Entry 20 : PrimeFaces-Collector/.settings/org.eclipse.wst.jsdt.ui.superType.name
ZIP Entry 21 : PrimeFaces-Collector/.settings/org.eclipse.wst.validation.prefs
ZIP Entry 22 : PrimeFaces-Collector/.settings/org.scala-ide.sdt.core.prefs
ZIP Entry 23 : PrimeFaces-Collector/.cache
ZIP Entry 24 : PrimeFaces-Collector/.classpath
ZIP Entry 25 : PrimeFaces-Collector/.project
ZIP Entry 26 : PrimeFaces-Collector/pom.xml
ZIP Entry 27 : PrimeFaces-Collector/src/main/java/net/javabeat/primefaces/data/
ZIP Entry 28 : PrimeFaces-Collector/src/main/java/net/javabeat/primefaces/
ZIP Entry 29 : PrimeFaces-Collector/.metadata/src/main/webapp/WEB-INF/
ZIP Entry 30 : PrimeFaces-Collector/src/main/java/net/javabeat/
ZIP Entry 31 : PrimeFaces-Collector/src/main/webapp/resources/images/
ZIP Entry 32 : PrimeFaces-Collector/src/main/webapp/resources/js/
ZIP Entry 33 : PrimeFaces-Collector/src/main/webapp/resources/css/
ZIP Entry 34 : PrimeFaces-Collector/.metadata/src/main/webapp/
ZIP Entry 35 : PrimeFaces-Collector/src/main/java/net/
ZIP Entry 36 : PrimeFaces-Collector/src/main/webapp/resources/
ZIP Entry 37 : PrimeFaces-Collector/src/main/webapp/WEB-INF/
ZIP Entry 38 : PrimeFaces-Collector/.metadata/src/main/
ZIP Entry 39 : PrimeFaces-Collector/src/main/java/
ZIP Entry 40 : PrimeFaces-Collector/src/main/webapp/
ZIP Entry 41 : PrimeFaces-Collector/src/main/resources/
ZIP Entry 42 : PrimeFaces-Collector/src/test/java/
ZIP Entry 43 : PrimeFaces-Collector/src/test/resources/
ZIP Entry 44 : PrimeFaces-Collector/.metadata/src/
ZIP Entry 45 : PrimeFaces-Collector/src/main/
ZIP Entry 46 : PrimeFaces-Collector/src/test/
ZIP Entry 47 : PrimeFaces-Collector/.metadata/
ZIP Entry 48 : PrimeFaces-Collector/.settings/
ZIP Entry 49 : PrimeFaces-Collector/src/
ZIP Entry 50 : PrimeFaces-Collector/

Category: JavaTag: Java Zip

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Install MongoDB On Windows
Next Post: How To Create Zip File Using Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact