package windows.Frame;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Zipper extends JFrame implements ActionListener, DocumentListener {
/**
*
*/
private static final long serialVersionUID = -2673835885705306448L;
private JButton jbtSelectInput;
private JButton jbtSelectOutput;
private JButton jbtZip;
private JList list;
private JTextField jtfOutput;
private File[] fs;
public Zipper() {
JToolBar toolbar = new JToolBar();
toolbar.add(jbtSelectInput = new JButton("选择输入文件"));
toolbar.add(jbtSelectOutput = new JButton("选择压缩文件"));
toolbar.add(jbtZip = new JButton("压缩"));
jbtZip.setEnabled(false);
this.add(toolbar, BorderLayout.NORTH);
JPanel jpInput = new JPanel(new BorderLayout());
jpInput.add(new JLabel("待压缩文件"), BorderLayout.NORTH);
jpInput.add(new JScrollPane(list = new JList()), BorderLayout.CENTER);
this.add(jpInput, BorderLayout.CENTER);
JPanel jpOutput = new JPanel(new GridLayout(0, 1));
jpOutput.add(new JLabel("输出压缩文件"));
jpOutput.add(jtfOutput = new JTextField());
this.add(jpOutput, BorderLayout.SOUTH);
jbtSelectInput.addActionListener(this);
jbtSelectOutput.addActionListener(this);
jbtZip.addActionListener(this);
jtfOutput.getDocument().addDocumentListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtSelectInput) {
// 选择多个待压缩文件
JFileChooser fc = new JFileChooser(".");
fc.setMultiSelectionEnabled(true);
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
fs = fc.getSelectedFiles();
list.setListData(fs);
setZipEnabled();
}
} else if (e.getSource() == jbtSelectOutput) {
// 选择一个输出压缩文件
JFileChooser fc = new JFileChooser(".");
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("zip");
fc.setFileFilter(filter);
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
jtfOutput.setText(fc.getSelectedFile().getAbsolutePath()+".zip");
setZipEnabled();
}
} else if (e.getSource() == jbtZip) {
ZipOutputStream zip=null;
try {
FileOutputStream output = new FileOutputStream(jtfOutput
.getText());
zip = new ZipOutputStream(output);
for (int i = 0; i < fs.length; i++) {
ZipEntry zipEntry = new ZipEntry(fs[i].getName());
zip.putNextEntry(zipEntry);
FileInputStream input = new FileInputStream(fs[i]);
int value;
while ((value = input.read()) != -1) {
zip.write(value);
}
input.close();
}
} catch (FileNotFoundException e1) {
//e1.printStackTrace();
} catch (IOException e1) {
//e1.printStackTrace();
}
finally{
if(zip!=null){
try {
zip.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
public void changedUpdate(DocumentEvent e) {
setZipEnabled();
}
public void insertUpdate(DocumentEvent e) {
setZipEnabled();
}
public void removeUpdate(DocumentEvent e) {
setZipEnabled();
}
private void setZipEnabled() {
this.jbtZip.setEnabled(fs.length != 0
&& !jtfOutput.getText().trim().equals(""));
}
public static void main(String[] args) {
Zipper frame = new Zipper();
frame.setTitle("Zipper");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}