用JAVA语言实现的压缩解压Zip文件

源代码在线查看: mainframe.java~55~

软件大小: 36 K
上传用户: hslhsl123
关键词: JAVA Zip 语言 解压
下载地址: 免注册下载 普通下载 VIP

相关代码

				package jzipdemo;								import java.awt.*;				import java.awt.event.*;				import javax.swing.*;				import java.io.*;				import java.util.*;				import java.util.zip.*;								/**				 * Title: 				 * Description: 				 * Copyright: Copyright (c) 2002				 * Company: 				 * @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 = null;				    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.mkdirs();				      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);				    }				    this.jTextArea1.append("\t"+"unzipped "+ze.getName()+"\n");				    this.jTextArea1.append("Unzip complete.\n");												  }								  void zip()throws Exception{								  }				}			

相关资源