springboot 框架把 resources下的zip压缩包, springboot 项目启动后解压到项目根目录工具类

最近有一个需求,在开发的时候 有一些c++的扩展文件 需要放到服务器上,如果手动放上去,给用户部署项目就很麻烦,就根据这个需求,先把项目需要的 扩展文件 打包成zip压缩包 然后项目启动的时候 把resources文件夹下的 zip压缩包 解压到 项目根目录,这样就很方便。下面的工具类,在什么时机调用,大家可以根据自己的需求自行调整,我是在springboot启动的 run生命周期里 调用的
今天就分享一下 我封装的这个java版的 zip 解压工具类
直接上代码,有需要的小伙伴,复制直接可以使用:

package com.xx.xxx.utils;

import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.nio.file.Paths;

/**
 * User:Json
 * Date: 2025/4/27
 **/
public class ZipUtils {

    public static boolean extractZipFromJar(String zipFileInJar, String destinationDirectory) {
        ClassPathResource resource = new ClassPathResource(zipFileInJar);
        Path targetDir = Paths.get(destinationDirectory); // 目标目录是根目录
        try (InputStream inputStream = resource.getInputStream();
             ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
            // 创建目标目录,如果不存在
            if (!Files.exists(targetDir)) {
                Files.createDirectories(targetDir);
            }
            ZipEntry entry;
            while ((entry = zipInputStream.getNextEntry()) != null) {
                Path filePath = targetDir.resolve(entry.getName());
                // 如果是目录,则创建目录
                if (entry.isDirectory()) {
                    Files.createDirectories(filePath);
                } else {
                    if (Files.exists(filePath)) {
                        System.out.println("文件已经存在,跳过解压: " + filePath);
                        continue;
                    }
                    try (OutputStream outputStream = Files.newOutputStream(filePath)) {
                        byte[] buffer = new byte[2048]; // 调整缓冲区大小以提高解压速度
                        int length;
                        while ((length = zipInputStream.read(buffer)) > 0) {
                            outputStream.write(buffer, 0, length);
                        }
                    }
                }
                zipInputStream.closeEntry();
            }
            // 解压完成,输出日志
            System.out.println("ZIP 文件解压完成,已解压到:" + targetDir.toString());
            return true;
        } catch (IOException e) {
            // 记录详细的错误信息
            System.out.println("解压失败: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 检查根目录是否存在某个文件夹
     * @param folderName 要检查的文件夹名称
     * @return 如果文件夹存在,返回 true;否则返回 false
     */
    public static boolean isFolderExists(String folderName) {
        Path path = Paths.get(System.getProperty("user.dir"), folderName);  // 获取根目录下的文件夹路径
        return Files.exists(path) && Files.isDirectory(path);  // 检查文件夹是否存在且是目录
    }
}

下方是封面图:略过
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Json____

您的鼓励是我创作的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值