?? zipdemo.java
字號(hào):
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.*;
//用ZIP壓縮多個(gè)文件
public class ZipDemo extends JFrame{
JFileChooser fileChooser; //文件選擇器
JList fileList; //待壓縮的文件列表
Vector files; //文件數(shù)據(jù)(待壓縮文件)
JButton jbAdd; //增加文件按鈕
JButton jbDelete; //刪除文件按鈕
JButton jbZip; //壓縮按鈕
JTextField target; //目標(biāo)文件文本域
public ZipDemo(){
super("用ZIP壓縮多個(gè)文件"); //調(diào)用父類構(gòu)造函數(shù)
fileChooser=new JFileChooser(); //實(shí)例化文件選擇器
files=new Vector(); //實(shí)例化文件數(shù)據(jù)Vector
fileList=new JList(files); //實(shí)例化已選擇文件列表
jbAdd=new JButton("增加"); //實(shí)例化按鈕組件
jbDelete=new JButton("刪除");
jbZip=new JButton("壓縮");
target=new JTextField(18);
JPanel panel=new JPanel(); //實(shí)例化面板,用于容納按鈕
panel.add(jbAdd); //增加組件到面板上
panel.add(jbDelete);
panel.add(jbZip);
JPanel panel2=new JPanel();
panel2.add(new JLabel("目標(biāo)文件"));
panel2.add(target);
JScrollPane jsp=new JScrollPane(fileList);
Container container=getContentPane(); //得到容器
container.add(panel2,BorderLayout.NORTH); //增加組件到容器
container.add(jsp,BorderLayout.CENTER);
container.add(panel,BorderLayout.SOUTH);
jsp.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //設(shè)置邊界
jbAdd.addActionListener(new ActionListener(){ //增加文件按鈕事件處理
public void actionPerformed(ActionEvent event) {
if (fileChooser.showOpenDialog(ZipDemo.this)==JFileChooser.APPROVE_OPTION){ //彈出文件選擇器,并判斷是否點(diǎn)擊了打開按鈕
String fileName=fileChooser.getSelectedFile().getAbsolutePath(); //得到選擇文件的絕對(duì)路徑
files.add(fileName); //增加文件到Vector
fileList.setListData(files); //設(shè)置文件選擇列表的數(shù)據(jù)
}
}
});
jbDelete.addActionListener(new ActionListener(){ //刪除文件按鈕事件處理
public void actionPerformed(ActionEvent event) {
files.remove(fileList.getSelectedValue()); //從Vector中移除選擇文件
fileList.setListData(files); //設(shè)置文件選擇列表的數(shù)據(jù)
}
});
jbZip.addActionListener(new ActionListener(){ //壓縮按鈕事件處理
public void actionPerformed(ActionEvent event) {
zipFiles(files.toArray(),target.getText()); //調(diào)用壓縮文件方法
}
});
setSize(330,250); //設(shè)置窗口尺寸
setVisible(true); //設(shè)置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時(shí)退出程序
}
public void zipFiles(Object[] sources,String target){ //壓縮文件
try{
FileOutputStream fout=new FileOutputStream(target); //得到目標(biāo)文件輸出流
ZipOutputStream zout=new ZipOutputStream(fout); //得到壓縮輸出流
byte[] buf=new byte[1024];//設(shè)定讀入緩沖區(qū)尺寸
int num;
FileInputStream fin=null;
ZipEntry entry=null;
for (int i=0;i<sources.length;i++){
String filename=sources[i].toString(); //得到待壓縮文件路徑名
String entryname=filename.substring(filename.lastIndexOf("\\")+1); //得到文件名
entry=new ZipEntry(entryname); //實(shí)例化條目列表
zout.putNextEntry(entry); //將ZIP條目列表寫入輸出流
fin=new FileInputStream(filename); //從源文件得到文件輸入流
while ((num=fin.read(buf))!=-1){ //如果文件未讀完
zout.write(buf,0,num); //寫入緩沖數(shù)據(jù)
}
}
zout.close(); //關(guān)閉壓縮輸出流
fout.close(); //關(guān)閉文件輸出流
fin.close(); //關(guān)閉文件輸入流
showMessage("壓縮成功"); //顯示操作信息
}
catch (Exception ex){
ex.printStackTrace(); //打印出錯(cuò)信息
showMessage("壓縮失敗");
}
}
class SelectFileListener implements ActionListener { //文件選擇的事件處理
public void actionPerformed(ActionEvent event) {
if (fileChooser.showOpenDialog(ZipDemo.this)==JFileChooser.APPROVE_OPTION){ //彈出文件選擇器,并判斷是否點(diǎn)擊了打開按鈕
String fileName=fileChooser.getSelectedFile().getAbsolutePath(); //得到選擇文件的絕對(duì)路徑
}
}
}
private void showMessage(String message){
JOptionPane.showMessageDialog(ZipDemo.this,message); //顯示信息
}
public static void main(String[] args){
new ZipDemo();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -