ZipFileViewer.java Source code

Java tutorial

Introduction

Here is the source code for ZipFileViewer.java

Source

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class ZipFileViewer extends JFrame {
    private JTable m_zipTable;

    private ZipTableModel m_zipTableModel;

    public ZipFileViewer(String zipFile, List zipEntries) {
        m_zipTableModel = new ZipTableModel(zipEntries);
        m_zipTable = new JTable(m_zipTableModel);
        JScrollPane scrollPane = new JScrollPane(m_zipTable);
        m_zipTable.setShowVerticalLines(true);
        m_zipTable.setShowHorizontalLines(false);
        getContentPane().add(scrollPane);
        setSize(800, 400);
        setVisible(true);
    }

    public static void main(String[] arg) throws Exception {
        String zipFileName = arg[0];
        List zipFileList = null;
        ZipFile zipFile = new ZipFile(zipFileName);
        Enumeration zipEntries = zipFile.entries();
        zipFileList = new ArrayList();
        while (zipEntries.hasMoreElements()) {
            zipFileList.add((ZipEntry) (zipEntries.nextElement()));
        }
        ZipFileViewer zipFileViewer = new ZipFileViewer(zipFileName, zipFileList);
    }
}

class ZipTableModel extends AbstractTableModel {

    public static final int NAME = 0;

    public static final int SIZE = 1;

    public static final int COMP_SIZE = 2;

    public static final int TYPE = 3;

    public static final int LAST_MODI = 4;

    public String[] m_colNames = { "File Name", "Size", "Compressed Size", "Type", "Last Modified" };

    private List m_zipEntries;

    public ZipTableModel(List zipEntries) {
        super();
        m_zipEntries = zipEntries;
    }

    public int getColumnCount() {
        return m_colNames.length;
    }

    public int getRowCount() {

        return m_zipEntries.size();
    }

    public String getColumnName(int col) {
        return m_colNames[col];
    }

    public Object getValueAt(int row, int col) {
        ZipEntry zipEntry = (ZipEntry) (m_zipEntries.get(row));
        switch (col) {
        case NAME:
            return zipEntry.getName();
        case SIZE:
            if (zipEntry.isDirectory()) {
                return "";
            } else {
                return String.valueOf(zipEntry.getSize() / 1000) + " KB";
            }
        case COMP_SIZE:
            if (zipEntry.isDirectory()) {
                return "";
            } else {
                return String.valueOf(zipEntry.getCompressedSize() / 1000) + " KB";
            }
        case TYPE:
            if (zipEntry.isDirectory()) {
                return "Directory";
            } else {
                return "File";
            }
        case LAST_MODI:
            return String.valueOf(new Date(zipEntry.getTime()));
        }
        return new String();
    }
}