当前位置: 首页 > news >正文

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);  // 检查文件夹是否存在且是目录}
}

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

http://www.xdnf.cn/news/204877.html

相关文章:

  • DeepSeek主动学习系统:低质量数据炼金术的工程化实践
  • runpod team 怎么设置自己的ssh key呢?
  • LLamaFactory如何在Windows系统下部署安装训练(保姆级教程)
  • 松下机器人快速入门指南(2025年更新版)
  • Kotlin-高阶函数,Lambda表达式,内联函数
  • IntelliJ IDEA 2024.3.1 for Mac 中文 Java开发工具
  • 狼人杀中的智能策略:解析AI如何理解复杂社交游戏
  • 语音合成之十韵律之美:TTS如何模拟语音的节奏和语调
  • 23种设计模式-行为型模式之中介者模式(Java版本)
  • 第10次:电商项目配置开发环境
  • 初探RAG
  • SQLyog中DELIMITER执行存储过程时出现的前置缩进问题
  • 在 Windows 的终端安装并使用 azd 命令
  • Windows权限与icacls命令详解
  • taro小程序如何实现大文件(视频、图片)后台下载功能?
  • 小结: 接口类型和路由优先级
  • 如何用python脚本把一个表格有4万多条数据分为两个文件表,每个2万条数据?
  • stm32wb55rg (4) 启用usart串口
  • PDM是什么?PDM有什么用?怎么选PDM?2025制造PDM/PLM系统盘点(4000字)
  • 针对信息过载问题的解决方案
  • 【Prometheus-MySQL Exporter安装配置指南,开机自启】
  • uni-app中使用RenderJs 使用原生js
  • 抱佛脚之学SSMSpringMVC数据绑定
  • 微服务学习笔记
  • 时序数据库 TDengine × Perspective:你需要的可视化“加速器”
  • OpenGL ES 3.0 第二章总结:你好,三角形(Hello Triangle)
  • AlDente Pro for Mac电脑 充电限制保护工具 安装教程【简单,轻松上手】
  • 您好,当前系统不允许绑定该店,具体原因您可咨询系统服务商,我们将尽力为您解决
  • DevExpressWinForms-TreeList-设置不可编辑
  • 大数据学习(115)-hive与impala