本例子使用ZipInputStream 和ZipOutputStream来读和写zip文件。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.zip.*;
import javax.swing.*;
public class ZipTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
ZipTestFrame frame=new ZipTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
import javax.swing.*;
import java.awt.*;
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;
//A Frame with a text area to show the content of a file inside a ZIP archive,
//a combo box to select different files in the archieve,and a menu to load a new archieve
class ZipTestFrame extends JFrame
{
public ZipTestFrame()
{
setTitle("ZipTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
//add the menu and the Open and Exit items
JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("File");
JMenuItem openItem=new JMenuItem("Open") ;
menu.add(openItem);
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int r = chooser.showOpenDialog(ZipTestFrame.this);
if (r == JFileChooser.APPROVE_OPTION)
{
zipname = chooser.getSelectedFile().getPath();
fileCombo.removeAllItems();
scanZipFile();
}
}
});
JMenuItem exitItem=new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
menuBar.add(menu);
setJMenuBar(menuBar);
//add the text area and combo box
fileText= new JTextArea();
fileCombo=new JComboBox<>();
fileCombo.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
loadZipFile((String) fileCombo.getSelectedItem());
}
});
add(fileCombo, BorderLayout.SOUTH);
add(new JScrollPane(fileText),BorderLayout.CENTER);
}
//Scan the contents of the zip archive and populates the combo box
public void scanZipFile()
{
new SwingWorker<Void,String>()
{
protected Void doInBackground()throws Exception
{
ZipInputStream zin=new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
while ((entry=zin.getNextEntry())!=null)
{
publish(entry.getName());
zin.closeEntry();
}
zin.close();
return null;
}
protected void process(List<String> names)
{
for(String name:names)
fileCombo.addItem(name);
}
}.execute();
}
//Load a file from the ZIP archieve into the text area
public void loadZipFile(final String name)
{
fileCombo.setEnabled(false);
fileText.setText("");
new SwingWorker<Void,Void>()
{
@Override
protected Void doInBackground() throws Exception
{
try
{
ZipInputStream zin=new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
//find entry with matching name in archieve
while ((entry=zin.getNextEntry())!=null)
{
if(entry.getName().equals(name))
{
//read entry into text area
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;
}
protected void done()
{
fileCombo.setEnabled(true);
}
}.execute();
}
public static final int DEFAULT_WIDTH=400;
public static final int DEFAULT_HEIGHT=300;
private JComboBox fileCombo;
private JTextArea fileText;
private String zipname;
}
运行结果: