- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.nio.CharBuffer;
- import java.nio.charset.Charset;
- import java.nio.charset.CharsetDecoder;
- import javax.swing.*;
- import javax.swing.filechooser.FileFilter;
- import java.util.Vector;
- import java.util.zip.*;
- public class unZip extends JFrame{
- private File file;
- private JTextField field;
- private JButton buttonSD;
- private JButton buttonOK;
- private Container container;
- private ZipOutputStream zipos;
- private ZipInputStream zin ;
- public unZip() {
- super("unZip");
- container=getContentPane();
- container.setLayout(new FlowLayout());
- field=new JTextField(30);
- buttonSD=new JButton("...");
- buttonOK=new JButton("OK");
- buttonSD.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- ShowOpenDialog();
- }
- });
- buttonOK.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent arg0) {
- startUnzip();;
- }
- });
- container.add(field);
- container.add(buttonSD);
- container.add(buttonOK);
- setSize(400,150);
- setVisible(true);
- }
- private void startUnzip() {
- File zfp=new File(field.getText());
- try {
- zin = new ZipInputStream(new FileInputStream(zfp));
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(this, "文件无法读取","提示",JOptionPane.INFORMATION_MESSAGE );
- }
- ZipEntry outZipEntry = null;
- try {
- outZipEntry = zin.getNextEntry();
- } catch (IOException e) {
- JOptionPane.showMessageDialog(this, "无法取得条目","提示",JOptionPane.INFORMATION_MESSAGE );
- }
- int r=0;
- byte []b=new byte[4096];
- FileOutputStream out = null;
- while(outZipEntry!=null){
- try {
- File file=new File(zfp.getParent()+"//"+outZipEntry.getName());
- File parentPath=new File(file.getParent());
- if(!parentPath.exists()){
- parentPath.mkdirs();
- }
- out=new FileOutputStream(file);
- System.err.println(System.getProperties().getProperty("user.dir")+"//"+outZipEntry.getName());
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(this, System.getProperties().getProperty("user.dir")+"//"+outZipEntry.getName(),"a cue",JOptionPane.INFORMATION_MESSAGE );
- }
- try {
- while((r=zin.read(b, 0, 4096))>-1){
- out.write(b, 0, r);
- }
- out.close();
- } catch (IOException e) {
- JOptionPane.showMessageDialog(this, "读取文件失败","提示",JOptionPane.INFORMATION_MESSAGE );
- }
- try {
- outZipEntry=zin.getNextEntry();
- } catch (IOException e) {
- JOptionPane.showMessageDialog(this, "无法取得条目","提示",JOptionPane.INFORMATION_MESSAGE );
- }
- }
- JOptionPane.showMessageDialog(this, "解压完成","提示",JOptionPane.INFORMATION_MESSAGE );
- }
- protected void ShowOpenDialog() {
- JFileChooser filechooser=new JFileChooser();
- filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
- int result=filechooser.showOpenDialog(this);
- if(result==JFileChooser.CANCEL_OPTION)return;
- field.setText(filechooser.getSelectedFile().toString());
- }
- public static void main(String[] args) {
- unZip app=new unZip();
- app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
简单的解压小程序
最新推荐文章于 2024-02-06 11:44:43 发布