工具类(zip包解压)

java 复制代码
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UnzipUtils {
    public static void unzip(String zipFile, String extractFolder) throws IOException {
        File file = new File(zipFile);
        ZipFile zip = null;

        try {
            int BUFFER = 8192;
            zip = new ZipFile(file);
            File newPath = new File(extractFolder);
            newPath.mkdirs();
            Enumeration zipFileEntries = zip.entries();

            while(true) {
                ZipEntry entry;
                File destFile;
                do {
                    if (!zipFileEntries.hasMoreElements()) {
                        return;
                    }

                    entry = (ZipEntry)zipFileEntries.nextElement();
                    String currentEntry = entry.getName();
                    destFile = new File(newPath, currentEntry);
                    if (!isSubFile(newPath, destFile)) {
                        throw new IOException("Bad zip entry: " + currentEntry);
                    }

                    File destinationParent = destFile.getParentFile();
                    destinationParent.mkdirs();
                } while(entry.isDirectory());

                BufferedInputStream is = null;
                BufferedOutputStream dest = null;

                try {
                    is = new BufferedInputStream(zip.getInputStream(entry));
                    byte[] data = new byte[BUFFER];
                    FileOutputStream fos = new FileOutputStream(destFile);
                    dest = new BufferedOutputStream(fos, BUFFER);

                    int currentByte;
                    while((currentByte = is.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, currentByte);
                    }

                    dest.flush();
                } finally {
                    close((OutputStream)dest);
                    close((InputStream)is);
                }
            }
        } finally {
            close(zip);
        }
    }

    private static IOException close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }

            return null;
        } catch (IOException var2) {
            return var2;
        }
    }

    private static IOException close(ZipFile zip) {
        try {
            if (zip != null) {
                zip.close();
            }

            return null;
        } catch (IOException var2) {
            return var2;
        }
    }

    private static boolean isSubFile(File parent, File child) throws IOException {
        return child.getCanonicalPath().startsWith(parent.getCanonicalPath() + File.separator);
    }
}
相关推荐
I'm a winner4 分钟前
第五章:Python 数据结构:列表、元组与字典(一)
开发语言·数据结构·python
葵野寺8 分钟前
【RelayMQ】基于 Java 实现轻量级消息队列(九)
java·开发语言·rabbitmq·java-rabbitmq
代码不停26 分钟前
MySQL联合查询
java·数据库·mysql
nightunderblackcat28 分钟前
新手向:C语言、Java、Python 的选择与未来指南
java·c语言·python
纯真时光31 分钟前
Maven高级
java
大白同学4211 小时前
【C++】C++11介绍(Ⅱ)
开发语言·c++
你怎么知道我是队长1 小时前
C语言---存储类
c语言·开发语言
XIAOYU6720131 小时前
金融数学专业需要学哪些数学和编程内容?
开发语言·matlab·金融
油炸自行车1 小时前
【Qt】编写Qt自定义Ui控件步骤
开发语言·c++·qt·ui·自定义ui控件·qt4 自定义ui控件
好多171 小时前
《微服务事务管理》
java·微服务·架构