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)
The java.util.zip.* 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. I zipped up a few files into a file called test.zip. The test.zip file at the root of the project contains file1.txt, file2.txt, folder, folder/file3.txt, and folder/file4.txt. The DisplayZipContents class displays the contents of the test.zip file. It creates a ZipFile object based on test.zip. 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. DisplayZipContents.javapackage test; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class DisplayZipContents { public static void main(String[] args) { try { ZipFile zipFile = new ZipFile("test.zip"); 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) { e.printStackTrace(); } } } (Continued on page 2) Related Tutorials:
|