?? zipfile.java
字號:
package zipfile;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.zip.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ZipFile
extends JFrame {
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JTextArea jTextArea1 = new JTextArea();
File source;
File object;
JButton jButton4 = new JButton();
//Construct the frame
public ZipFile() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(241, 258));
this.setTitle("壓縮文件");
jScrollPane1.setBounds(new Rectangle(21, 18, 197, 144));
jButton1.setBounds(new Rectangle(21, 170, 82, 25));
jButton1.setMargin(new Insets(2, 1, 2, 1));
jButton1.setText("選擇源文件");
jButton1.addActionListener(new ZipFile_jButton1_actionAdapter(this));
jButton2.setText("選擇目標(biāo)文件");
jButton2.addActionListener(new ZipFile_jButton2_actionAdapter(this));
jButton2.setMargin(new Insets(2, 1, 2, 1));
jButton2.setBounds(new Rectangle(136, 170, 82, 25));
jButton3.setText("壓 縮");
jButton3.addActionListener(new ZipFile_jButton3_actionAdapter(this));
jButton3.setMargin(new Insets(2, 1, 2, 1));
jButton3.setBounds(new Rectangle(21, 199, 82, 25));
jButton3.setToolTipText("");
jTextArea1.setText("");
jButton4.setBounds(new Rectangle(136, 199, 82, 25));
jButton4.setMargin(new Insets(2, 1, 2, 1));
jButton4.addActionListener(new ZipFile_jButton4_actionAdapter(this));
jButton4.setText("退 出");
jButton4.addActionListener(new ZipFile_jButton4_actionAdapter(this));
contentPane.add(jScrollPane1, null);
contentPane.add(jButton1, null);
contentPane.add(jButton3, null);
contentPane.add(jButton2, null);
contentPane.add(jButton4, null);
jScrollPane1.getViewport().add(jTextArea1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
JFileChooser fileChooser1 = new JFileChooser(); //定義一個JFileChooser對象
fileChooser1.setCurrentDirectory(new File("."));
fileChooser1.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return f.isFile() || f.isDirectory();
}
public String getDescription() {
return "All files";
}
});
int t = fileChooser1.showOpenDialog(this);
if (t == JFileChooser.APPROVE_OPTION) {
//得到文件后綴名
source = fileChooser1.getSelectedFile();
jTextArea1.append("源文件為:" + source.toString() + "\n");
}
}
void jButton2_actionPerformed(ActionEvent e) {
JFileChooser fileChooser1 = new JFileChooser(); //定義一個JFileChooser對象
fileChooser1.setCurrentDirectory(new File("."));
fileChooser1.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return f.isFile() || f.isDirectory();
}
public String getDescription() {
return "All files";
}
});
int t = fileChooser1.showOpenDialog(this);
if (t == JFileChooser.APPROVE_OPTION) {
//得到文件后綴名
object = fileChooser1.getSelectedFile();
jTextArea1.append("目標(biāo)文件為:" + object.toString() + "\n");
}
}
void jButton3_actionPerformed(ActionEvent e) {
if (source != null) {
if (object != null) {
try {
FileInputStream fileInput = new FileInputStream(source);
DataInputStream in = new DataInputStream(fileInput);//創(chuàng)建輸入流
FileOutputStream fileOutput = new FileOutputStream(object);
DataOutputStream dataOut=new DataOutputStream(fileOutput);//創(chuàng)建輸出流
ZipOutputStream out = new ZipOutputStream(dataOut);//創(chuàng)建壓縮數(shù)據(jù)流
out.putNextEntry(new ZipEntry(source.getPath()));
int i;
while ( (i = in.read()) != -1) {
out.write(i);
}
in.close();
out.close();
jTextArea1.append("文件壓縮成功!" + "\n");
}
catch (Exception err) {
jTextArea1.append(err + "\n");
}
}
else {
jTextArea1.append("請選擇目標(biāo)文件!");
}
}
else {
jTextArea1.append("請選擇源文件!");
}
}
void jButton4_actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class ZipFile_jButton1_actionAdapter
implements java.awt.event.ActionListener {
ZipFile adaptee;
ZipFile_jButton1_actionAdapter(ZipFile adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class ZipFile_jButton2_actionAdapter
implements java.awt.event.ActionListener {
ZipFile adaptee;
ZipFile_jButton2_actionAdapter(ZipFile adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class ZipFile_jButton3_actionAdapter
implements java.awt.event.ActionListener {
ZipFile adaptee;
ZipFile_jButton3_actionAdapter(ZipFile adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class ZipFile_jButton4_actionAdapter
implements java.awt.event.ActionListener {
ZipFile adaptee;
ZipFile_jButton4_actionAdapter(ZipFile adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -