Wednesday, November 4, 2009

Java Program to list the Contents of a Zip File

Java Sample program to list the contents of a zip file

In this program we need to get the entries of the zip file, since each file in a zip file is represented by an entry. In this program assume that the filename of the zip file is 'Test.zip'. By calling the entries method of the ZipFile object we get an Enumeration back that can be used to loop through the entries of the file. We have to cast each element in the Enumeration to a ZipEntry.


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

public class ListZipFiles
{
public void doListFiles()
{
try
{
ZipFile zipFile = new ZipFile("Test.zip");
Enumeration zipEntries = zipFile.entries();

while (zipEntries.hasMoreElements())
{
//Process the name, here we just print it out
System.out.println(((ZipEntry)zipEntries.nextElement()).getName());
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
// Command line arguments
public static void main(String[] args) {

new Main().doListFiles();
}
}

0 comments:

Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP