工具类(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 分钟前
基于springboot的新闻资讯系统
java·spring boot·毕业设计·课程设计·源代码·新闻资讯系统
软件开发技术深度爱好者13 分钟前
Python类中方法种类介绍
开发语言·python
麦麦鸡腿堡13 分钟前
Java_LinkedList底层结构
java·开发语言
whatever who cares21 分钟前
android/java中gson的用法
android·java·开发语言
摇滚侠24 分钟前
Spring Boot3零基础教程,为什么有Reactive-Stream 规范,响应式编程,笔记101
java·spring boot·笔记
周杰伦fans33 分钟前
C# 中 Entity Framework (EF) 和 EF Core 里的 `AsNoTracking` 方法
开发语言·c#
小灰灰搞电子37 分钟前
Rust Slint实现控件尺寸的扩展与收缩源码分享
开发语言·后端·rust
☆cwlulu38 分钟前
git分支管理详解
开发语言·git·青少年编程
天天摸鱼的java工程师39 分钟前
八年 Java 开发手敲:SpringBoot+SpringSecurity+JWT 实战,前后分离权限注解落地就能跑
java·后端
冰淇淋@41 分钟前
idea启动项目报错java: OutOfMemoryError: insufficient memory
java·ide·intellij-idea