工具类(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);
    }
}
相关推荐
微露清风12 分钟前
系统性学习C++-第五讲-内存管理
java·c++·学习
计算机毕业设计木哥16 分钟前
计算机毕业设计选题推荐:基于SpringBoot和Vue的快递物流仓库管理系统【源码+文档+调试】
java·vue.js·spring boot·后端·课程设计
qiuiuiu41316 分钟前
正点原子RK3568学习日志-编译第一个驱动程序helloworld
linux·c语言·开发语言·单片机
2351620 分钟前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
聪明的笨猪猪24 分钟前
Java Redis “运维”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
FIavor.41 分钟前
怎么办这是Apifox里执行http://localhost:9002/goods/getByUserName?name=“张三“为什么我改了还是500?
java·网络·网络协议·http
编程饭碗42 分钟前
【Java集合】
java
岁岁岁平安43 分钟前
Java的双重检查锁机制(DCL)与懒加载的单例模式
java·单例模式·synchronized·
Jabes.yang1 小时前
Java面试场景:从Spring Boot到Kubernetes的技术问答
java· 面试· spring boot· 微服务· kubernetes· 技术栈· redis
molong9311 小时前
Kotlin 内联函数、高阶函数、扩展函数
android·开发语言·kotlin