How do I display the contents of a zip file?
Author: Deron Eriksson
Description: This Java tutorial describes how to display the contents of a zip file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)

Page:    1 2 >

The* package makes it easy to handle working with zip files. This tutorial will demonstrate how to display the contents of a zip file. To do this, I'll utilize a project with the following structure.

'testing' project

I zipped up a few files into a file called The file at the root of the project contains file1.txt, file2.txt, folder, folder/file3.txt, and folder/file4.txt. contents viewed in WinZip

The DisplayZipContents class displays the contents of the file. It creates a ZipFile object based on It enumerates over the ZipEntry objects within the ZipFile object. For each ZipEntry, it reads the name, size, and compressed size of the entry. It displays these to standard output using System.out.printf() to format the results.

package test;

import java.util.Enumeration;

public class DisplayZipContents {

	public static void main(String[] args) {

		try {
			ZipFile zipFile = new ZipFile("");
			Enumeration<?> enu = zipFile.entries();
			while (enu.hasMoreElements()) {
				ZipEntry zipEntry = (ZipEntry) enu.nextElement();
				String name = zipEntry.getName();
				long size = zipEntry.getSize();
				long compressedSize = zipEntry.getCompressedSize();
				System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", 
					name, size, compressedSize);
		} catch (IOException e) {

(Continued on page 2)

Page:    1 2 >