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

📄 mainframe.java~58~

📁 用JAVA语言实现的压缩解压Zip文件
💻 JAVA~58~
字号:
package jzipdemo;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.util.*;import java.util.zip.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class MainFrame extends JFrame {  private JPanel contentPane;  private BorderLayout borderLayout1 = new BorderLayout();  private JScrollPane jScrollPane1 = new JScrollPane();  private JTextArea jTextArea1 = new JTextArea();  private JButton jButton1 = new JButton();  private JPanel jPanel1 = new JPanel();  private JTextField jTextField1 = new JTextField();  private JTextField jTextField2 = new JTextField();  private JLabel jLabel1 = new JLabel();  private JLabel jLabel2 = new JLabel();  private GridLayout gridLayout1 = new GridLayout();  //Construct the frame  public MainFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  //Component initialization  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    contentPane.setLayout(borderLayout1);    this.setSize(new Dimension(461, 307));    this.setTitle("JZipDemo");    jTextArea1.setText("unzip and zip information");    jButton1.setFont(new java.awt.Font("Dialog", 0, 14));    jButton1.setToolTipText("");    jButton1.setText("start");    jButton1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton1_actionPerformed(e);      }    });    jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));    jLabel1.setHorizontalAlignment(SwingConstants.CENTER);    jLabel1.setText("source path :");    jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));    jLabel2.setHorizontalAlignment(SwingConstants.CENTER);    jLabel2.setText("dictation path :");    jPanel1.setLayout(gridLayout1);    gridLayout1.setColumns(2);    gridLayout1.setHgap(10);    gridLayout1.setRows(2);    contentPane.add(jScrollPane1, BorderLayout.CENTER);    jScrollPane1.getViewport().add(jTextArea1, null);    contentPane.add(jButton1, BorderLayout.SOUTH);    contentPane.add(jPanel1, BorderLayout.NORTH);    jPanel1.add(jLabel1, null);    jPanel1.add(jTextField1, null);    jPanel1.add(jLabel2, null);    jPanel1.add(jTextField2, 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) {    String sour = this.jTextField1.getText();    String dest = this.jTextField2.getText();    String sourExt=sour.substring(sour.length()-4,sour.length()).toLowerCase();    String destExt=dest.substring(dest.length()-4,dest.length()).toLowerCase();    if(sourExt.equals(".zip")){      try{        unzip();      }catch(Exception err){        err.printStackTrace();      }    }    if(destExt.equals(".zip")){      try{        zip();      }catch(Exception err){        err.printStackTrace();      }    }  }  void unzip()throws Exception{    byte[] buffer = new byte[1024];    DataInputStream dis = null;    DataOutputStream dos = null;    FileOutputStream fos = null;    File file = new File(this.jTextField2.getText());    if (!file.exists())      file.mkdirs();    ZipEntry ze =null;    ZipFile zf = new ZipFile(new File(this.jTextField1.getText()));    Enumeration enum = zf.entries();    if (enum.hasMoreElements()){      ze = (ZipEntry)enum.nextElement();    }    System.out.println(ze.getName());    file = new File(this.jTextField2.getText()+File.separator+ze.getName());    if (!file.exists()){      file.createNewFile();    }    InputStream is = zf.getInputStream(ze);    dis = new DataInputStream(is);    fos = new FileOutputStream(file);    dos = new DataOutputStream(fos);    this.jTextArea1.append("Starting unzip ......\n");    int bytes;    while((bytes=dis.read(buffer,0,buffer.length))!=-1){      dos.write(buffer,0,bytes);    }    dis.close();    dos.close();    this.jTextArea1.append("\t"+"unzipped "+ze.getName()+"\n");    this.jTextArea1.append("Unzip complete.\n");  }  void zip()throws Exception{    byte[] buffer = new byte[1024];    File file = new File(this.jTextField1.getText());    FileInputStream fis = new FileInputStream(file);    DataInputStream dis = new DataInputStream(fis);    File zipfile = new File(this.jTextField2.getText());    if (!zipfile.exists()){      File zipdir = new File(zipfile.getAbsolutePath());      if (!zipdir.exists()){        zipdir.mkdirs();      }      zipfile.createNewFile();    }    ZipEntry ze = new ZipEntry(this.jTextField1.getText());   // ZipFile zf = new ZipFile(zipfile);    FileOutputStream fos = new FileOutputStream(zipfile);    ZipOutputStream zos = new ZipOutputStream(fos);    zos.setMethod(ZipOutputStream.DEFLATED);    zos.putNextEntry(ze);    DataOutputStream dos = new DataOutputStream(zos);    this.jTextArea1.append("Starting zip ......\n");    int bytes;    if ((bytes=dis.read(buffer,0,buffer.length))!=-1){      dos.write(buffer,0,bytes);    }    dis.close();    dos.close();    this.jTextArea1.append("\t"+"Zipped"+this.jTextField1.getText()+"\n");    this.jTextArea1.append("Zip complete.\n");  }}

⌨️ 快捷键说明

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