package com.xudeyu.stream;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class ZipInput
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
ZipFileTest zt = new ZipFileTest();
zt.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
zt.setVisible(true);
}
});
}
}
class ZipFileTest extends JFrame
{
private int DEFAULT_WIDTH = 400;
private int DEFAULT_HEIGHT = 500;
private String zipFileName;
private JComboBox fileComb;
private JTextArea fileText;
public ZipFileTest()
{
setTitle("Zip file test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
// Open Item add to FileMenu
JMenuItem openItem = new JMenuItem("Open");
menuFile.add(openItem);
// if chooser a file ,add all file name in combox witch in thezip;
openItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser(new File("."));
int r = chooser.showOpenDialog(null);
if (r == JFileChooser.APPROVE_OPTION)
{
zipFileName = chooser.getSelectedFile().getPath();
fileComb.removeAllItems();
scanZipFile();
}
}
});
// exit Item add to FileMenu
JMenuItem exitItem = new JMenuItem("exitt");
menuFile.add(exitItem);
// exit if on click
exitItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
// add the file menu on the menu bar;
menuBar.add(menuFile);
setJMenuBar(menuBar);
fileText = new JTextArea();
fileComb = new JComboBox();
// when selecet in combox ,output a file info in textAreawitch in zip
fileComb.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
loadZipFile((String) fileComb.getSelectedItem());
}
});
// add two complent on frame
add(fileComb, BorderLayout.SOUTH);
add(new JScrollPane(fileText), BorderLayout.CENTER);
}
public void scanZipFile()
{
// 执行后台计算操作
new SwingWorker()
{
@Override
protected Void doInBackground() throws Exception
{
ZipInputStream zin = new ZipInputStream(
new FileInputStream(zipFileName));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null)
{
// see api publish,every publish ,push it to process;
publish(entry.getName());
zin.closeEntry();// only close can read next entry
}
zin.close();
return null;
}
@Override
protected voidprocess(List names)
{
for (String name : names)
{
fileComb.addItem(name);
}
}
}.execute();
}
public void loadZipFile(final String name)
{
fileComb.setEditable(false);
fileText.setText("");
new SwingWorker()
{
@Override
protected Void doInBackground() throws Exception
{
try
{
ZipInputStream zin = new ZipInputStream(
new FileInputStream(zipFileName));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null)
{
if (entry.getName().equals(name))
{
Scanner in = new Scanner(zin);
while (in.hasNextLine())
{
fileText.append(in.nextLine());
fileText.append("\n");
}
}
zin.closeEntry();
}
zin.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void done()
{
fileComb.setEditable(true);
}
}.execute();
}
}