工具类(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);
    }
}
相关推荐
冼紫菜几秒前
Java开发中使用 RabbitMQ 入门到进阶详解(含注解方式、JSON配置)
java·spring boot·后端·rabbitmq·springcloud
DT——1 分钟前
ECMAScript 2018(ES2018):异步编程与正则表达式的深度进化
开发语言·javascript·ecmascript
Kakikori1 分钟前
JSP链接MySQL8.0(Eclipse+Tomcat9.0+MySQL8.0)
java·开发语言
Dr.92713 分钟前
1-10 目录树
java·数据结构·算法
冬日枝丫22 分钟前
【spring】spring学习系列之六:spring的启动流程(下)
java·学习·spring
子豪-中国机器人24 分钟前
C++ 蓝桥 STEMA 省选拔赛模拟测试题(第一套)
开发语言·c++·算法
圈圈编码29 分钟前
LeetCode Hot100刷题——轮转数组
java·算法·leetcode·职场和发展
〆、风神29 分钟前
面试真题 - 高并发场景下Nginx如何优化
java·nginx·面试
酷炫码神34 分钟前
C#数组与集合
开发语言·c#
應呈35 分钟前
FreeRTOS的学习记录(任务创建,任务挂起)
java·linux·学习