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

📄 frame1.java

📁 &#61548 能够完成基本的压缩与解压缩文件功能 &#61548 能够一次加入多个文件 &#61548 能够给出压缩比等信息
💻 JAVA
字号:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.swing.event.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Frame1
    extends Frame
    implements Runnable {
  long yasuoqian = 0, yasuohou = 0, yasuoshi = 0;
  String filename;
  String baseDir;
  private File file;
  JPanel jPanel1 = new JPanel();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JTextField jTextField1 = new JTextField();
  JLabel jLabel1 = new JLabel();
  JProgressBar jProgressBar1 = new JProgressBar();
  JLabel jLabel2 = new JLabel();
  Thread thread, thread1, thread2, thread3;
  public Frame1() {
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    thread = new Thread(this);
    thread1 = new Thread(this);
    thread2 = new Thread(this);
    thread3 = new Thread(this);
    this.setTitle("数据压缩");
    this.setLayout(null);
    jPanel1.setRequestFocusEnabled(false);
    jPanel1.setBounds(new Rectangle( -2, 0, 352, 215));
    jPanel1.setLayout(null);
    jButton1.setBounds(new Rectangle(59, 102, 80, 29));
    jButton1.setActionCommand("覆盖压缩");
    jButton1.setFocusPainted(false);
    jButton1.setSelected(true);
    jButton1.setText("覆盖压缩");
    jButton1.addMouseListener(new Frame1_jButton1_mouseAdapter(this));
    jButton2.setBounds(new Rectangle(167, 101, 72, 30));
    jButton2.setText("解压缩");
    jButton2.addMouseListener(new Frame1_jButton2_mouseAdapter(this));
    jTextField1.setText("");
    jTextField1.setBounds(new Rectangle(66, 157, 230, 34));
    jLabel1.setText("压缩结果");
    jLabel1.setBounds(new Rectangle(6, 160, 65, 30));
    this.addWindowListener(new Frame1_this_windowAdapter(this));
    jProgressBar1.setRequestFocusEnabled(true);
    jProgressBar1.setString("");
    jProgressBar1.setStringPainted(true);
    jProgressBar1.setBounds(new Rectangle(68, 63, 228, 18));
    jLabel2.setText("进度");
    jLabel2.setBounds(new Rectangle(7, 56, 55, 32));
    this.add(jPanel1, null);
    jPanel1.add(jButton2, null);
    jPanel1.add(jButton1, null);
    jPanel1.add(jTextField1, null);
    jPanel1.add(jLabel1, null);
    jPanel1.add(jProgressBar1, null);
    jPanel1.add(jLabel2, null);
    this.setVisible(true);
    this.setSize(jPanel1.getWidth(), jPanel1.getHeight());
  }

  public static void main(String args[]) {
    Frame1 mm = new Frame1();
  }

  private ArrayList getSubFiles(File baseDir) {
    ArrayList ret = new ArrayList();
    //File base=new File(baseDir);
    File[] tmp = baseDir.listFiles();
    for (int i = 0; i < tmp.length; i++) {
      if (tmp[i].isFile()) {
        ret.add(tmp[i]);
      }
      if (tmp[i].isDirectory()) {
        ret.addAll(getSubFiles(tmp[i]));
      }
    }
    return ret;
  }

  private String getAbsFileName(String baseDir, File realFileName) {
    File real = realFileName;
    File base = new File(baseDir);
    String ret = real.getName();
    while (true) {
      real = real.getParentFile();
      if (real == null) {
        break;
      }
      if (real.equals(base)) {
        break;
      }
      else {
        ret = real.getName() + "/" + ret;
      }
    }
    return ret;
  }

  private File getRealFileName(String baseDir, String absFileName) {

    String[] aa = new String[20];
    int ii = 0;
    while (absFileName.indexOf("/") != -1) {
      aa[ii] = (String) absFileName.substring(0, absFileName.indexOf("/"));
      absFileName = absFileName.substring(absFileName.indexOf("/") + 1);
      ii++;
    }
    aa[ii] = absFileName;

    int xx = 0;
    for (int x = 0; x < 20 && aa[x] != null; x++) {
      xx++;
    }
    String[] dirs = new String[xx];
    for (int x = 0; x < dirs.length; x++) {
      dirs[x] = aa[x];
    }
    System.out.println(dirs.length);
    for (int a = 0; a < dirs.length; a++) {
      System.out.println(dirs[a]);
    }
    File ret = new File(baseDir);
    if (dirs.length > 1) {
      for (int i = 0; i < dirs.length - 1; i++) {
        ret = new File(ret, dirs[i]);
      }
    }
      if (!ret.exists()) {
        ret.mkdirs();
      }


    ret = new File(ret, dirs[dirs.length - 1]);
    System.out.println(ret.getPath());
    return ret;
  }

  void this_windowClosing(WindowEvent e) {
    System.exit(0);
  }

  void jButton1_mouseClicked(MouseEvent e) {
    yasuohou = 0;
    yasuoqian = 0;
    yasuoshi = 0;
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = fileChooser.showOpenDialog(this);
    if (result == JFileChooser.CANCEL_OPTION) {
      return;
    }
    file = fileChooser.getSelectedFile();
    baseDir = file.getPath();
    System.out.println(baseDir);
    String aa = "";
    /*while (baseDir.indexOf("\\") != -1) {
      aa = aa + baseDir.substring(0, baseDir.indexOf("\\")) + "\\" + "\\";
      baseDir = baseDir.substring(baseDir.indexOf("\\") + 1);
    }
    aa = aa + baseDir;
    baseDir = aa;
    System.out.println(baseDir);*/
    FileDialog dlg = new FileDialog(this, "Save", FileDialog.SAVE);
    dlg.show();
    filename = dlg.getDirectory() + dlg.getFile();
    aa = "";
    System.out.println(filename);

    while (filename.indexOf("\\") != -1) {
      aa = aa + filename.substring(0, filename.indexOf("\\")) + "\\" + "\\";
      filename = filename.substring(filename.indexOf("\\") + 1);
    }
    aa = aa + filename;
    filename = aa;

    thread1.start();

  }

  public void testCreateZip() throws Exception {
    File f1 = new File(filename);
    ArrayList fileList = getSubFiles(new File(baseDir));
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(f1
        ));
    for (int i = 0; i < fileList.size(); i++) {
      File f = (File) fileList.get(i);
      yasuoqian = yasuoqian + f.length();
    }

    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    int readLen = 0;
    thread.start();
    for (int i = 0; i < fileList.size(); i++) {

      File f = (File) fileList.get(i);
      ze = new ZipEntry(getAbsFileName(baseDir, f));
      ze.setSize(f.length());
      ze.setTime(f.lastModified());
      zos.putNextEntry(ze);
      InputStream is = new BufferedInputStream(new FileInputStream(f));

      while ( (readLen = is.read(buf, 0, 1024)) != -1) {

        zos.write(buf, 0, readLen);
        yasuoshi = yasuoshi + readLen;
      }
      is.close();
    }
    zos.close();
    yasuohou = f1.length();
  }

  public void testReadZip() throws Exception {

    ZipFile zfile;
    File f = new File(filename);
    yasuohou = f.length();
    thread3.start();
    zfile = new ZipFile(filename);
  //  System.out.println(zfile.getName());
    Enumeration zList = zfile.entries();
    zfile.getName();
    ZipEntry ze = null;
    while (zList.hasMoreElements()) {
      ze = (ZipEntry) zList.nextElement();
     if (ze.isDirectory()) {
    //   System.out.println("Dir: " + ze.getName() + " skipped..");
       continue;
     }
     yasuoqian = yasuoqian + ze.getSize();

   }
    zList = zfile.entries();
    ze = null;
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
//从ZipFile中得到一个ZipEntry
      ze = (ZipEntry) zList.nextElement();
      if (ze.isDirectory()) {
     //   System.out.println("Dir: " + ze.getName() + " skipped..");
        continue;
      }
   //   System.out.println("Extracting: " + ze.getName() + "\t" + ze.getSize() +
  //                       "\t" + ze.getCompressedSize());
  //以ZipEntry为参数得到一个InputStream,并写到OutputStream中
      OutputStream os = new BufferedOutputStream(new FileOutputStream(
          getRealFileName(baseDir, ze.getName())));

      InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
      int readLen = 0;
      while ( (readLen = is.read(buf, 0, 1024)) != -1) {
        os.write(buf, 0, readLen);
        yasuoshi = yasuoshi + readLen;
      }
      is.close();
      os.close();
//      System.out.println("Extracted: " + ze.getName());
    }
    zfile.close();
  }


  void jButton2_mouseClicked(MouseEvent e) {
    yasuoqian = 0;
    yasuohou = 0;
    yasuoshi = 0;
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result = fileChooser.showOpenDialog(this);
    if (result == JFileChooser.CANCEL_OPTION) {
      return;
    }
    file = fileChooser.getSelectedFile();
    filename = file.getPath();
    System.out.println(filename);

    String aa = "";
    /*while (filename.indexOf("\\") != -1) {
      aa = aa + filename.substring(0, filename.indexOf("\\")) + "\\" + "\\";
      filename = filename.substring(filename.indexOf("\\") + 1);
    }
    aa = aa + filename;
    filename = aa;
    System.out.println(filename);*/
    FileDialog dlg = new FileDialog(this, "Save", FileDialog.SAVE);
    dlg.show();
    baseDir = dlg.getDirectory() + dlg.getFile();
    aa = "";
    System.out.println(baseDir);

    /*while (baseDir.indexOf("\\") != -1) {
      aa = aa + baseDir.substring(0, filename.indexOf("\\")) + "\\" + "\\";
      baseDir = baseDir.substring(filename.indexOf("\\") + 1);
    }
    aa = aa + baseDir;
    baseDir = aa;
    System.out.println(baseDir);*/
    thread2.start();
  }

  public void run() {
    if (Thread.currentThread() == thread1) {
      try {
        testCreateZip();
      }
      catch (Exception e1) {
        System.out.println(e1.getMessage());
      }
      jTextField1.setText("压缩前" + ":" + yasuoqian + "压缩后" + ":" + yasuohou +
                          "压缩比例" + ":" + (int) (yasuoqian / yasuohou));
      thread1.stop();
    }
    if (Thread.currentThread() == thread) {
      try {
        for (; ; ) {
          thread.sleep(100);
          int yasuoqian1 = (int) (yasuoqian / 1024);
          int yasuoshi1 = (int) (yasuoshi / 1024);
          double bili = ( (double) yasuoshi1) / yasuoqian1;
          int state = (int) (bili * 100);
          jProgressBar1.setValue(state);
          jPanel1.updateUI();
          jPanel1.updateUI();
          System.out.println(state);
          if (state == 100) {
            thread.stop();
          }

        }
      }
      catch (Exception w) {}
    }
    if (Thread.currentThread() == thread2) {
      try {
        testReadZip();
      }
      catch (Exception e1) {
        System.out.println(e1.getMessage());
      }

      jTextField1.setText("解压后" + ":" + yasuoqian + "解压前" + ":" + yasuohou +
                          "压缩比例" + ":" + (int) (yasuoqian / yasuohou));
      thread2.stop();
    }
    if (Thread.currentThread() == thread3) {
      try {
        for (; ; ) {
          thread.sleep(100);
          //System.out.println(yasuoqian);
          //System.out.println(yasuoshi);
          int yasuoqian1 = (int) (yasuoqian / 1024);
          int yasuoshi1 = (int) (yasuoshi / 1024);
          double bili = ( (double) yasuoshi1) / yasuoqian1;
          int state = (int) (bili * 100);
          jProgressBar1.setValue(state);
          jPanel1.updateUI();
          jPanel1.updateUI();
          System.out.println(state);
          if (state == 100) {
            thread3.stop();
          }

        }
      }
      catch (Exception w) {}
    }
  }


}

class Frame1_this_windowAdapter
    extends java.awt.event.WindowAdapter {
  Frame1 adaptee;

  Frame1_this_windowAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void windowClosing(WindowEvent e) {
    adaptee.this_windowClosing(e);
  }
}

class Frame1_jButton1_mouseAdapter
    extends java.awt.event.MouseAdapter {
  Frame1 adaptee;

  Frame1_jButton1_mouseAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseClicked(MouseEvent e) {
    adaptee.jButton1_mouseClicked(e);
  }
}

class Frame1_jButton2_mouseAdapter
    extends java.awt.event.MouseAdapter {
  Frame1 adaptee;

  Frame1_jButton2_mouseAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseClicked(MouseEvent e) {
    adaptee.jButton2_mouseClicked(e);
  }
}

⌨️ 快捷键说明

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