Java实现多层目录打包和解压--解决了压缩不了空文件夹的问题

依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<!-- 处理压缩解压 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

工具类: 

package com.fsc.zip_utils.utils;


import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.zip.*;

public class ZipUtil {

    private static final String TAG = "ZipUtil";

    /**
     * 解压文件到指定文件夹
     *
     * @param zip      源文件
     * @param destPath 目标文件夹路径
     * @throws Exception 解压失败
     */
    public static void decompress(String zip, String destPath) throws
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用 Java 中的 ZipInputStream 类和 ZipEntry 类来实现解压文件,然后再结合上面的获取多层文件夹内所有 Excel 文件的方法,就可以实现获取压缩包内多层文件夹中的所有 Excel 文件了。下面是示例代码: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileExtractor { public static void main(String[] args) { String zipFilePath = "C:\\path\\to\\file.zip"; String destFolderPath = "C:\\path\\to\\destination\\folder"; extractZipFile(zipFilePath, destFolderPath); getExcelFiles(destFolderPath); } public static void extractZipFile(String zipFilePath, String destFolderPath) { byte[] buffer = new byte[1024]; try { // 创建目标文件夹 File destFolder = new File(destFolderPath); if (!destFolder.exists()) { destFolder.mkdir(); } // 打开压缩文件 ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath)); // 解压文件 ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { String fileName = zipEntry.getName(); File newFile = new File(destFolderPath + File.separator + fileName); if (zipEntry.isDirectory()) { newFile.mkdirs(); } else { // 创建父文件夹 newFile.getParentFile().mkdirs(); // 写入文件 FileOutputStream fileOutputStream = new FileOutputStream(newFile); int len; while ((len = zipInputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, len); } fileOutputStream.close(); } zipInputStream.closeEntry(); } zipInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public static void getExcelFiles(String folderPath) { // 与上一个问题中的代码相同 } } ``` 在 `extractZipFile` 方法中,首先创建目标文件夹,然后遍历压缩包内的所有文件文件夹,如果是文件夹则创建对应的文件夹,如果是文件则将其解压到目标文件夹中。最后调用 `getExcelFiles` 方法即可获取目标文件夹多层文件夹中的所有 Excel 文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值