SpringBoot 中 zip 文件解压工具类

发布于:2024-05-11 ⋅ 阅读:(173) ⋅ 点赞:(0)

SpringBoot 中 zip 文件解压工具类

zip 文件解压(不支持密码)

相关 Maven 依赖

<dependency>  
    <groupId>org.apache.commons</groupId>  
    <artifactId>commons-lang3</artifactId>  
    <version>3.6</version>  
</dependency>  
<dependency>  
    <groupId>org.apache.commons</groupId>  
    <artifactId>commons-compress</artifactId>  
    <version>1.21</version>  
</dependency>  
<dependency>  
    <groupId>com.googlecode.juniversalchardet</groupId>  
    <artifactId>juniversalchardet</artifactId>  
    <version>1.0.3</version>  
</dependency>

工具类

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;  
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;  
import org.apache.commons.lang3.StringUtils;  
import org.mozilla.universalchardet.UniversalDetector;  
  
import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.IOException;  
import java.nio.file.Files;  
import java.nio.file.Path;  
import java.nio.file.Paths;  
  
public class CompressUtils {  
  
    public static void main(String[] args) throws IOException {  
        String filePath = "C:\\Users\\MAC\\Desktop\\Desktop.zip";  
        String targetPath = "C:\\Users\\MAC\\Desktop";  
        File zipFile = new File(filePath);  
        File targetDir = new File(targetPath);  
        decompressZipFileWithoutPassword(zipFile, targetDir);  
    }  
  
    public static Boolean decompressZipFileWithoutPassword(File zipFile, File targetDir) throws IOException {  
        if (!zipFile.exists() || !zipFile.getName().endsWith("zip")) {  
            return false;  
        }  
        if (!targetDir.exists()) {  
            targetDir.mkdirs();  
        }  
        // 获取文件的编码格式  
        String encoding = FileUtils.detectFileEncoding(zipFile);  
        try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new BufferedInputStream(Files.newInputStream(zipFile.toPath())), encoding)) {  
            ZipArchiveEntry entry = null;  
            while ((entry = zis.getNextZipEntry()) != null) {  
                // 如果时文件夹则忽略,也可以创建文件夹  
                if (entry.isDirectory()) continue;  
                String name = entry.getName();  
                Path unzipFilePath = Paths.get(targetDir.getAbsolutePath(), name);  
                Path parent = unzipFilePath.getParent();  
                // 判断父文件夹存不存在,不存在则创建  
                if (!Files.exists(parent)) parent.toFile().mkdirs();  
                try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(unzipFilePath))) {  
                    byte[] buf = new byte[4096];  
                    int len;  
                    while ((len = zis.read(buf)) != -1) {  
                        bos.write(buf, 0, len);  
                    }  
                }  
            }  
        }  
        return true;  
    }  
}  
  
class FileUtils {  
    /**  
     * 自动检测文件的编码格式  
     * @param file 需要检测的文件  
     * @return encoding 编码格式,默认为 UTF-8  
     * @throws IOException  
     */    
     public static String detectFileEncoding(File file) throws IOException {  
        String encoding = null;  
        try (BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(file.toPath()))) {  
            UniversalDetector detector = new UniversalDetector(null);  
            byte[] buf = new byte[4096];  
            int len;  
            while ((len = bis.read(buf)) != -1 && !detector.isDone()) {  
                detector.handleData(buf, 0, len);  
            }  
            detector.dataEnd();  
            encoding = detector.getDetectedCharset();  
            detector.reset();  
        }  
        return !StringUtils.isBlank(encoding) ? encoding : "UTF-8";  
    }  
}

网站公告

今日签到

点亮在社区的每一天
去签到