Listing 14: List the contents of a zip file

import java.util.*;
import java.util.zip.*;

class ListZip
{
    public static void main(String[] args)
        throws Exception
    {
        ZipFile zf = new ZipFile(args[0]);
        Enumeration files = zf.entries();
        while (files.hasMoreElements())
        {
            ZipEntry z = (ZipEntry)files.nextElement();
            System.out.println(z.getName() + "," +
                               z.getSize() + "," +
                               z.getCompressedSize() + "," +
                               new Date(z.getTime()));
        }
    }
}

/* Output from 'java ListZip cuj.zip':
ViewFile.java,1573,533,Sat Nov 18 18:03:42 MST 2000
Employee.java,2433,811,Sat Nov 18 17:57:38 MST 2000
FileViewer.java,1830,533,Sat Nov 18 18:03:22 MST 2000
ListFiles.java,1785,523,Thu Nov 09 17:29:50 MST 2000
ListZip.java,295,204,Sat Nov 18 23:42:56 MST 2000
ProcessRecords.java,1370,519,Sat Nov 18 17:59:20 MST 2000
PropTest.java,216,126,Wed Nov 08 23:26:46 MST 2000
records.c,974,398,Tue Nov 07 22:22:50 MST 2000
Stack.java,463,206,Wed Nov 08 12:00:04 MST 2000
*/
— End of Listing —