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

📄 extractdialog.java~66~

📁 文件压缩功能:压缩指定单个文件
💻 JAVA~66~
字号:
package zipCompress;

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

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

public class ExtractDialog
    extends JDialog {
  //解压缩用变量:
  byte[] cache = new byte[1024 * 1024]; //1MB大小的缓冲区
  File fileToExtract;
  InputStream fin;
  ZipInputStream zin;
  String defaultPath;
  FileOutputStream fout;
  ZipEntry entry;
  int numberOfItems = 0;
  ArrayList items = new ArrayList(10);
  CompressFrame parent;

  JPanel panel1 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextField jTextFieldPath = new JTextField();
  JButton jButton1 = new JButton();
  JLabel jLabel2 = new JLabel();
  JScrollPane jScrollPane2 = new JScrollPane();
  JPanel jPanelChoice = new JPanel();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();

  public ExtractDialog(Frame frame, String title, boolean modal) {
    super(frame, title, modal);
    this.parent = (CompressFrame) frame;
    try {
      jbInit();
      pack();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public ExtractDialog() {
    this(null, "", false);
  }

  private void jbInit() throws Exception {
    panel1.setLayout(null);
    jLabel1.setFont(new java.awt.Font("新細明體", 0, 11));
    jLabel1.setToolTipText("");
    jLabel1.setHorizontalAlignment(SwingConstants.LEADING);
    jLabel1.setHorizontalTextPosition(SwingConstants.LEFT);
    jLabel1.setIcon(new ImageIcon(ExtractDialog.class.getResource(
        "extractTo.png")));
    jLabel1.setText("解压到文件夹:");
    jLabel1.setBounds(new Rectangle(12, 15, 121, 28));
    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
                                              HORIZONTAL_SCROLLBAR_ALWAYS);
    jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.
                                            VERTICAL_SCROLLBAR_NEVER);
    jScrollPane1.setBounds(new Rectangle(12, 52, 180, 41));
    jTextFieldPath.setEditable(false);
    jTextFieldPath.setSelectionStart(11);
    jTextFieldPath.setText("");
    jButton1.setBounds(new Rectangle(196, 52, 65, 22));
    jButton1.setFont(new java.awt.Font("新細明體", 0, 11));
    jButton1.setText("浏览...");
    jButton1.addActionListener(new ExtractDialog_jButton1_actionAdapter(this));
    jLabel2.setFont(new java.awt.Font("新細明體", 0, 11));
    jLabel2.setText("选择要解压的文件:");
    jLabel2.setBounds(new Rectangle(12, 101, 103, 23));
    jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.
                                            VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane2.setBounds(new Rectangle(12, 132, 253, 163));
    jButton2.setBounds(new Rectangle(46, 315, 71, 25));
    jButton2.setFont(new java.awt.Font("新細明體", 0, 11));
    jButton2.setText("确定");
    jButton2.addActionListener(new ExtractDialog_jButton2_actionAdapter(this));
    jButton3.setBounds(new Rectangle(155, 315, 71, 25));
    jButton3.setFont(new java.awt.Font("新細明體", 0, 11));
    jButton3.setText("取消");
    jButton3.addActionListener(new ExtractDialog_jButton3_actionAdapter(this));
    this.setModal(true);
    this.setResizable(false);
    getContentPane().add(panel1);
    panel1.add(jLabel1, null);
    panel1.add(jScrollPane1, null);
    panel1.add(jButton1, null);
    panel1.add(jLabel2, null);
    panel1.add(jScrollPane2, null);
    panel1.add(jButton2, null);
    panel1.add(jButton3, null);
    /**************************************************/
    this.initializeBasicStreams();
    this.intializeChoicePanel();
    jScrollPane2.getViewport().add(jPanelChoice, null);
    jScrollPane1.getViewport().add(jTextFieldPath, null);
  }

  private void initializeBasicStreams() {
    try {
      this.defaultPath = this.parent.getCurrentDirectry();
      if (this.defaultPath.endsWith("\\")) {
        this.defaultPath = this.defaultPath.substring(0,
            this.defaultPath.length() - 1);
      }
      if(this.defaultPath.endsWith(":")){
        this.defaultPath+="\\";
      }
      this.jTextFieldPath.setText(this.defaultPath);
      this.fileToExtract = parent.getZipFile();
      this.fin = new FileInputStream(this.fileToExtract);
      this.zin = new ZipInputStream(fin);
      JCheckBox box;
      while ( (entry = zin.getNextEntry()) != null) {
        box = new JCheckBox(entry.getName(), true);
        this.numberOfItems++;
        items.add(items.size(), box);
      }
      fin.close();
      zin.close();
      this.fin = new FileInputStream(this.fileToExtract);
      this.zin = new ZipInputStream(fin);
    }
    catch (IOException ex) {
      JOptionPane.showMessageDialog(this,
                                    "不能打开文件\n" + this.fileToExtract.getAbsolutePath(),
                                    "严重错误", JOptionPane.ERROR_MESSAGE);
      ex.printStackTrace();
    }
  }

  private void intializeChoicePanel() {
    int rows = this.numberOfItems;
    if (rows < 10) {
      rows = 10;
    }
    this.jPanelChoice.setLayout(new GridLayout(rows, 1));
    Iterator itr = items.iterator();
    while (itr.hasNext()) {
      this.jPanelChoice.add( (JCheckBox) itr.next());
    }
  }

  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      this.dispose();
    }
  }

  void jButton3_actionPerformed(ActionEvent e) {
    this.dispose();
  }

  void jButton2_actionPerformed(ActionEvent e) {
    String pathName;
    String entryName;
    File path;
    int i=0;
    try {
      while ( (entry = zin.getNextEntry()) != null) {
        if(!((JCheckBox)items.get(i++)).isSelected()){
          continue;
        }
        entryName = entry.getName();
        pathName = this.defaultPath + entry.getName();
        if (pathName.indexOf(".")!=-1) {
          pathName = pathName.substring(0, pathName.lastIndexOf("\\"));
        }
        path = new File(pathName);
        if (!path.exists()) {
           path.mkdirs();
        }
         fout=new FileOutputStream(this.defaultPath+entry.getName());
         int amount=zin.read(this.cache);
          while(amount!=-1){
            fout.write(cache,0,amount);
            amount=zin.read(cache);
          }
          fout.close();
      }
      zin.close();
      this.dispose();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  void jButton1_actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File(this.defaultPath));
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    chooser.showOpenDialog(this);
    if (chooser.getSelectedFile() == null) {
      return;
    }
    this.defaultPath = chooser.getSelectedFile().getAbsolutePath();
    this.jTextFieldPath.setText(this.defaultPath);
  }
}

class ExtractDialog_jButton3_actionAdapter
    implements java.awt.event.ActionListener {
  ExtractDialog adaptee;

  ExtractDialog_jButton3_actionAdapter(ExtractDialog adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

class ExtractDialog_jButton2_actionAdapter
    implements java.awt.event.ActionListener {
  ExtractDialog adaptee;

  ExtractDialog_jButton2_actionAdapter(ExtractDialog adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class ExtractDialog_jButton1_actionAdapter implements java.awt.event.ActionListener {
  ExtractDialog adaptee;

  ExtractDialog_jButton1_actionAdapter(ExtractDialog adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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