⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipfile.java

📁 纯java实现的压缩程序
💻 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("选择目标文件");
    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("目标文件为:" + 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);//创建输入流

          FileOutputStream fileOutput = new FileOutputStream(object);
          DataOutputStream dataOut=new DataOutputStream(fileOutput);//创建输出流
          ZipOutputStream out = new ZipOutputStream(dataOut);//创建压缩数据流
          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("请选择目标文件!");
      }
    }
    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);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -