JAVA --解压缩

package com.DecompressFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
 *可以解压包含多种文件多层目录的压缩文件 
 */
public class Test3 {
    private Test3(){}
    public synchronized void unZip(String zipPath,String outPath) throws IOException{
        File zipFile = new File(zipPath);
        File outFile = new File(outPath);
        if(!zipFile.exists() || !zipFile.isFile()){
            System.err.println("压缩文件错误!");
        }else{
            boolean f = isName(zipFile);
            if(!f){
                System.err.println("要解压的文件格式错误");
                System.exit(0);
            }
            
            if(! outFile.exists() && !outFile.isDirectory()){
                outFile.mkdir();
            }
            ZipFile zip = new ZipFile(zipPath);
            Enumeration en = zip.getEntries();
            ZipEntry entity = null ;
            while(en.hasMoreElements()){
                entity = (ZipEntry)en.nextElement();
                if(entity.isDirectory()){
                    String folder = entity.getName();
                    folder = folder.substring(0, folder.length() - 1);
                    File file = new File(outFile.getPath() + File.separator + folder);
                    file.mkdir();
                }else{
                    String sname = outFile.getPath() + File.separator + entity.getName();
                    File file = new File(sname);
                    if(!file.exists()){
                        String names[] = entity.getName().split("/");
                        String ss = "";
                        for(int i = 0;i < names.length - 1;i++){
                            ss += names[i] + File.separator;
                        }
                        ss = outFile.getPath() + File.separator + ss;
                        File ssfile = new File(ss);
                        ssfile.mkdir();
                    }
                    file.createNewFile();
                    InputStream in = zip.getInputStream(entity);
                    FileOutputStream fos = new FileOutputStream(file);
                    try{
                        int c;
                        byte bytes[] = new byte[1024];
                        while((c = in.read(bytes)) != -1){
                            fos.write(bytes, 0, c);
                        }
                    }catch (Exception e) {
                        e.printStackTrace();
                    }finally{
                        fos.close();
                        in.close();
                    }
                }
            }
        }
    }
    
    public static boolean isName(File file){
        String path = file.getPath();
        String name = path.substring(path.lastIndexOf("."),path.length());
        if(name.equals(".zip") || name.equals(".tgz") || name.equals(".tar")){
            return true;
        }
        return false;
    }
    
    public static void main(String args[]){
        String zipPath = "E:/test.zip";
        String outPath = "E:/test12345";
        Test3 t3 = new Test3();
        try {
            t3.unZip(zipPath, outPath);
            System.out.println("OK");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/zqzdong/p/4840356.html