Java动态解压zip压缩包

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
public class Zip {
    public static void main(String args[]) throws IOException {  
        String sourceFilePath = "F:\fund_bill_20161120.zip"; //zip文件存放位置 
        String saveRootDirectory = "F://test/";  //解压后存放位置
        File file = new File(sourceFilePath);
        Zip.zipFileRead(file, saveRootDirectory);  
    }  
    /** 
     *  
     * @Description: TODO(读取Zip信息,获得zip中所有的目录文件信息) 
     * @param设定文件 
     * @return void 返回类型 
     * @throws 
     */  
    public static List<String> zipFileRead(File file, String saveRootDirectory) {
        List<String> list = new ArrayList<String>();
        try {  
            // 获得zip信息  
            ZipFile zipFile = new ZipFile(file,"GBK");  
            @SuppressWarnings("unchecked")  
            Enumeration<ZipArchiveEntry> enu = (Enumeration<ZipArchiveEntry>) zipFile.getEntries();
            while (enu.hasMoreElements()) {  
                ZipArchiveEntry zipElement = (ZipArchiveEntry) enu.nextElement();
                InputStream read = zipFile.getInputStream(zipElement);  
                String fileName = zipElement.getName();  
                System.out.println(fileName);
                list.add(fileName);
                if (fileName != null && fileName.indexOf(".") != -1) {// 是否为文件  
                    unZipFile(zipElement, read, saveRootDirectory);  
                }  
            }  
            return list;
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return list;
    }  
  
    /** 
     *  
     * @Description: TODO(找到文件并读取解压到指定目录) 
     * @param 设定文件 
     * @return void 返回类型 
     * @throws 
     */  
    public static void unZipFile(ZipArchiveEntry ze, InputStream read, String saveRootDirectory) throws FileNotFoundException, IOException {  
        // 如果只读取图片,自行判断就OK.  
        String fileName = ze.getName();
       
        // 判断文件是否符合要求或者是指定的某一类型  
//      if (fileName.equals("WebRoot/WEB-INF/web.xml")) {  
            // 指定要解压出来的文件格式(这些格式可抽取放置在集合或String数组通过参数传递进来,方法更通用)  
            File file = new File(saveRootDirectory + fileName);  
            if (!file.exists()) {  
                File rootDirectoryFile = new File(file.getParent());  
                // 创建目录  
                if (!rootDirectoryFile.exists()) {  
                    boolean ifSuccess = rootDirectoryFile.mkdirs();  
                    if (ifSuccess) {  
                        System.out.println("文件夹创建成功!");  
                    } else {  
                        System.out.println("文件创建失败!");  
                    }  
                }  
                // 创建文件  
                try {  
                    file.createNewFile();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            // 写入文件  
            BufferedOutputStream write = new BufferedOutputStream( new FileOutputStream(file));  
            int cha = 0;  
            while ((cha = read.read()) != -1) {  
                write.write(cha);  
            }  
            // 要注意IO流关闭的先后顺序  
            write.flush();  
            write.close();  
            read.close();  
            // }  
//      }  
    }  
}
原文地址:https://www.cnblogs.com/guokai870510826/p/6089062.html