📄 mywinzipframe.java~1~
字号:
package mywinzip;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;import java.util.*;import java.util.zip.*;/** * <p>Title: My WinZip Demo</p> * <p>Description: This is My WinZip demo</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: d6-125</p> * @author Liujun * @version 1.0 */public class MyWinZipFrame extends JFrame { JPanel contentPane; XYLayout xYLayout1 = new XYLayout(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JTextArea jTextArea1 = new JTextArea(); JLabel jLabel1 = new JLabel(); //定义要压缩的源文件集和目标文件 File [] source ; File target = null; //Construct the frame public MyWinZipFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(MyWinZipFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); jButton1.setText("选择需要压缩的文件"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("My WinZip Frame Demo"); jButton2.setText("使用Zip压缩"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); jTextArea1.setBackground(Color.gray); jTextArea1.setLineWrap(true); jLabel1.setText("jLabel1"); contentPane.add(jTextArea1, new XYConstraints(162, 17, 127, 115)); contentPane.add(jButton1, new XYConstraints(9, 25, 146, 21)); contentPane.add(jButton2, new XYConstraints(10, 74, 144, 21)); contentPane.add(jLabel1, new XYConstraints(23, 142, 270, 24)); } //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 jButton2_actionPerformed(ActionEvent e) { JFileChooser JFileCh = new JFileChooser();//创一个文件对话框选择目标文件 String filepath = null; int returnVal = JFileCh.showSaveDialog(this);//显示文件对话框选择目标文件 if(returnVal == JFileChooser.APPROVE_OPTION){//当选择好目标文件后 target = JFileCh.getSelectedFile();//得到目标文件 filepath = target.getPath();//给filepath赋值为目标文件串 if(source !=null){ try{ jLabel1.setText("正在压缩文件:"+target.getPath()+"..."); FileOutputStream f = new FileOutputStream(target);//定义输出流 ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f));//使用Zip压缩流 for(int k=0;k<source.length;k++){//根据输入的文件集,创建输出流 FileInputStream FileIn=new FileInputStream(source[k]); DataInputStream in = new DataInputStream(FileIn); out.putNextEntry(new ZipEntry(source[k].getPath()));//创建zip压缩文档条目 int c; while((c = in.read()) != -1)//写入流 out.write(c); in.close();//关闭一个输入流 } out.close();//关闭输出流,压缩完成 jLabel1.setText("文件压缩成功:"+target.getPath()); }catch(Exception ei){ jLabel1.setText("文件压缩失败:"+target.getPath()); } } else jLabel1.setText("请先选择一个需要压缩的文件"); } } void jButton1_actionPerformed(ActionEvent e) { JFileChooser JFileCh = new JFileChooser();//创建文件对话框 JFileCh.setMultiSelectionEnabled(true);//设置可多选文件属性 int returnVal = JFileCh.showOpenDialog(this);//显示文件对话框 if(returnVal == JFileChooser.APPROVE_OPTION){ source = JFileCh.getSelectedFiles();//得到源文件集 jTextArea1.setText("选择了文件:\n"); for(int i=0;i<source.length;i++){//显示选择了的文件列表 jTextArea1.append(source[i].getPath()+"\n"); } } else jTextArea1.setText("你没有选择文件"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -