Java provides support to manipulate the compressed zip files. This can be achieved by using classes in java.util.zip package. The ZipFile class of this package represents a compressed zip file and provides methods to get the entries in it. The method, entries() of a ZipFile class, returns the Enumeration of all the entries it contains. The ZipEntry class represents a single entry in the zip file. The ZipEntry object contains all information about a particular entry like; name, size, compressed size, type, etc.
Example Code:
Following example creates a ZipFileViewer, which takes a zip file as parameter and shows its contents in a JTable. The method getZipFileContents(String) of class ZipFileViewer takes a zip file name and returns the List of ZipEntry objects which is used to render the JTable.
/*
* This example is from javareference.com
* for more information visit,
* https://www.javareference.com
*/
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.util.zip.*;
import java.io.IOException;
/**
* This class takes a zip file as a parameter
* and display the contents of this file in a JTable
*
* @author Rahul Sapkal(rahul@javareference.com)
*/
public class ZipFileViewer extends JFrame {
//JTable table
private JTable m_zipTable;
//JTable model
private ZipTableModel m_zipTableModel;
/**
* Constructor
*/
public ZipFileViewer(String zipFile, List zipEntries) {
//Show the Zip file name and number of items on the title
super("Zip File Viewer " + "[" + zipFile + "] [" + zipEntries.size() + " Items]");
//creating the JTable by passing the table model
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);
//add the scroll pane to the frame
getContentPane().add(scrollPane);
setSize(800, 400);
setVisible(true);
}
/**
* This method takes the zip file name
* and returns the List of entries in the zip name
* That is List of ZipEntry objects
*
* @param zipFileName
* @return List of ZipEntry objects
*/
public static List getZipFileContents(String zipFileName) {
ZipFile zipFile = null;
List zipFileList = null;
try {
//create ZipFile object
zipFile = new ZipFile(zipFileName);
//get the entries
Enumeration zipEntries = zipFile.entries();
zipFileList = new ArrayList();
while (zipEntries.hasMoreElements()) {
//add the ZipEntry object to the list
zipFileList.add((ZipEntry) (zipEntries.nextElement()));
}
} catch (ZipException zipExp) {
System.out.println("Zip Exception: " + zipExp);
} catch (IOException ioExp) {
System.err.println("IO Exception: " + ioExp);
}
return zipFileList;
}
/**
* Main method to run as an Application
*
* @param argv
*/
public static void main(String[] arg) {
if (arg.length == 1) {
String zipFileName = arg[0];
List zipFileList = getZipFileContents(zipFileName);
if (zipFileList != null) {
ZipFileViewer zipFileViewer = new ZipFileViewer(zipFileName, zipFileList);
} else {
System.out.println("Cannot Extract File");
}
} else {
System.out.println("Invalid Syntax");
System.out.println("Usage : java ZipFileViewer <ZipFileName>");
}
}
/**
* This class is the model for the above JTable
* It gets data from the ZipEntry object and provides
* it to the table
*
* @author Rahul Sapkal(rahul@javareference.com)
*/
public class ZipTableModel extends AbstractTableModel {
//Columns Number.
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;
//Names of the columns
public String[] m_colNames = {"File Name",
"Size",
"Compressed Size",
"Type",
"Last Modified"};
private List m_zipEntries;
/**
* Constructor
*/
public ZipTableModel(List zipEntries) {
super();
//store the data
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];
}
/**
* This method gets data from the ZipEntry object
* and provides it to the table
*/
public Object getValueAt(int row, int col) {
ZipEntry zipEntry = (ZipEntry) (m_zipEntries.get(row));
boolean isDir = zipEntry.isDirectory();
switch (col) {
case NAME:
return zipEntry.getName();
case SIZE:
if (isDir) {
return "";
} else {
return String.valueOf(zipEntry.getSize() / 1000) + " KB";
}
case COMP_SIZE:
if (isDir) {
return "";
} else {
return String.valueOf(zipEntry.getCompressedSize() / 1000) + " KB";
}
case TYPE:
if (isDir) {
return "Directory";
} else {
return "File";
}
case LAST_MODI:
return String.valueOf(new Date(zipEntry.getTime()));
}
return new String();
}
}
//End ZipTableModel
}
//End ZipFileViewer
Demonstration
Following image shows the ZipFileViewer, when the above program is executed.
