工具类(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);
    }
}
相关推荐
okseekw9 分钟前
Java网络编程从入门到实战:吃透三要素,玩转CS/BS架构
java·后端·http
xing-xing18 分钟前
Java大模型开发框架Spring AI
java·人工智能·spring
Coder_Boy_21 分钟前
【DDD领域驱动开发】基础概念和企业级项目规范入门简介
java·开发语言·人工智能·驱动开发
小付爱coding24 分钟前
本地部署dify教程【windows11版本】
java·ai·dify
morning_judger25 分钟前
JavaScript封装演进史:从全局变量到闭包
开发语言·javascript
y1y1z27 分钟前
Spring Security教程
java·后端·spring
MYMOTOE629 分钟前
ISC-3000S的U-Boot 镜像头部解析
java·linux·spring boot
CoderYanger32 分钟前
A.每日一题——3606. 优惠券校验器
java·开发语言·数据结构·算法·leetcode
飛67939 分钟前
玩转 Flutter 自定义 Painter:从零打造丝滑的仪表盘动效与可视化图表
开发语言·javascript·flutter
谷哥的小弟40 分钟前
Spring Framework源码解析——ConfigurableEnvironment
java·spring·源码