java解压全部zip压缩包,并删除压缩包

编程入门 行业动态 更新时间:2024-10-26 19:33:15

java解压全部zip<a href=https://www.elefans.com/category/jswz/34/1750560.html style=压缩包,并删除压缩包"/>

java解压全部zip压缩包,并删除压缩包

 引入的依赖

 <!--zip 解压文件需要--><dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.9.3</version></dependency>

zip工具类

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;@Slf4j
@Component
public class UnZipUtils {/*** 递归解压zip文件* @param zipPath       zip文件路径* @param targetPath    解压后存放的文件路径* @return void*/public static void unZipRecursion(String zipPath, String targetPath) {long start = System.currentTimeMillis();// 第一次解压boolean flag = unZip(new File(zipPath),targetPath);if (flag){// 后续递归解压scanFilesWithRecursion(targetPath);}else {log.info("解压失败");}long end = System.currentTimeMillis();log.info("解压完成, 耗时:{} ms",(end - start));}/*** 解压zip文件* @param srcFile       zip文件路径* @param destDirPath   解压后存放的文件路径* @return boolean*/public static boolean unZip(File srcFile, String destDirPath) {// 判断源文件是否存在if (!srcFile.exists()) {log.error("此文件不存在:{}",srcFile.getPath());return false;}// 开始解压try (ZipFile zipFile = new ZipFile(srcFile, Charset.forName("GBK"))){Enumeration<?> entries = zipFile.entries();while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();// 如果是文件夹,就创建个文件夹if (entry.isDirectory()) {String dirPath = destDirPath + File.separator + entry.getName();File dir = new File(dirPath);boolean mkdirs = dir.mkdirs();} else {// 如果是文件,就先创建一个文件,然后用io流把内容copy过去File targetFile = new File(destDirPath + File.separator + entry.getName());// 保证这个文件的父文件夹必须要存在if(!targetFile.getParentFile().exists()){boolean mkdirs = targetFile.getParentFile().mkdirs();log.info("保证这个文件的父文件夹必须要存在:{}",mkdirs);}boolean newFile = targetFile.createNewFile();if (newFile){// 将压缩文件内容写入到这个文件中// try-with-resources 自动关闭流try( InputStream is = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(targetFile)) {int len;byte[] buf = new byte[2048];while ((len = is.read(buf)) != -1) {fos.write(buf, 0, len);}} catch (Exception e){log.error("解压失败",e);}}}}return true;} catch (Exception e) {log.error("解压失败",e);return false;}}/*** 遍历文件夹,有压缩文件就进行解压* @param folderPath    需要解压的文件夹路径* @return void*/public static void scanFilesWithRecursion(String folderPath){File directory = new File(folderPath);if(!directory.isDirectory()){log.error("不是一个文件夹:{}",folderPath);}// 遍历文件夹if(directory.isDirectory()){File [] filelist = directory.listFiles();for(int i = 0; i < Objects.requireNonNull(filelist).length; i ++){String name = filelist[i].getAbsolutePath().substring(filelist[i].getAbsolutePath().lastIndexOf(".") + 1);// 如果是zip文件,解密if ("zip".equals(name)) {//sum--;// 压缩文件名称String zipFolderName = filelist[i].getName().substring(0, filelist[i].getName().lastIndexOf("."));// 创建解压后的存放文件目录,文件名称为压缩包名称String nowUnZipPath = directory.getPath() + File.separator + zipFolderName;File nowUnZipPathFile = new File(nowUnZipPath);nowUnZipPathFile.mkdirs();boolean flag = unZip(new File(filelist[i].getAbsolutePath()), nowUnZipPath);if (flag) {// 解压成功,删除压缩包boolean deleteFlag = filelist[i].delete();log.info("解压成功,删除临时压缩包,路径:{},是否删除成功:{}", filelist[i].getPath(), deleteFlag);}// 递归scanFilesWithRecursion(nowUnZipPathFile.getPath());} else if (new File(filelist[i].getPath()).isDirectory()) {// 递归scanFilesWithRecursion(filelist[i].getPath());}}}}}

参考项目elezip111

更多推荐

java解压全部zip压缩包,并删除压缩包

本文发布于:2023-11-15 08:01:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:压缩包   java   zip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!