SpringBoot将多个文件夹进行压缩的两种方法(浏览器下载和另存为)

 更新时间:2024年07月26日 09:57:40   作者:一纸红尘轻似梦  
Spring Boot项目通常不会自动对文件夹进行压缩,不过,在打包应用时,如果你使用了Maven或Gradle这样的构建工具,并且配置了相应的插件,可以在打成jar或war包的时候将依赖的库文件合并并压缩,本文介绍了SpringBoot将多个文件夹进行压缩的两种方法

1、将多个文件夹压缩成一个压缩包(压缩到固定目录)

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileZipper {
    public static void main(String[] args) {
        // 示例使用
        String zipFilePath = "C:\\Users\\guohu\\Desktop\\archive.zip";
        List<File> fileList = List.of(
                new File("C:\\Users\\guohu\\Desktop\\新建文件夹 (8)\\1657269583419039746"),
                new File("C:\\Users\\guohu\\Desktop\\新建文件夹 (8)\\1657269583419039747"),
                new File("C:\\Users\\guohu\\Desktop\\新建文件夹 (8)\\1657269583419039748")
        );

        // 将文件列表压缩成压缩包
        boolean result = zipFiles(fileList, zipFilePath);

        if (result) {
            System.out.println("文件压缩成功: " + zipFilePath);
        } else {
            System.out.println("压缩文件失败");
        }
    }

    public static boolean zipFiles(List<File> fileList, String zipFilePath) {
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
            for (File file : fileList) {
                if (file.exists()) {
                    compress(file, zos, file.getName(), true);
                }
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure)
            throws IOException {
        byte[] buffer = new byte[4096];
        if (sourceFile.isFile()) {
            try (FileInputStream fis = new FileInputStream(sourceFile)) {
                ZipEntry zipEntry;
                if (KeepDirStructure) {
                    zipEntry = new ZipEntry(name);
                } else {
                    zipEntry = new ZipEntry(sourceFile.getName());
                }
                zos.putNextEntry(zipEntry);
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
            }
        } else if (sourceFile.isDirectory()) {
            File[] files = sourceFile.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (KeepDirStructure) {
                        compress(file, zos, name + File.separator + file.getName(), KeepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), KeepDirStructure);
                    }
                }
            }
        }
    }
}

2、将多个文件夹压缩成一个压缩包(通过浏览器下载)


  List<File> fileList = Arrays.asList(
                new File("C:\\Users\\guohu\\Desktop\\新建文件夹 (8)\\1657269583419039746"),
                new File("C:\\Users\\guohu\\Desktop\\新建文件夹 (8)\\1657269583419039747"),
                new File("C:\\Users\\guohu\\Desktop\\新建文件夹 (8)\\1657269583419039748")
        );
        zipFiles(fileList, response,"学生资料");

    /**
     * 多个文件压缩成压缩包并下载工具类
     *
     * @param fileList
     * @param
     */
    public static void zipFiles(List<File> fileList, HttpServletResponse response, String zipFileName) {
        try {
            // 设置响应的头部信息
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFileName+".zip", "utf-8"));
            // 设置响应内容的类型
            response.setContentType("application/octet-stream");

            // 将压缩文件写入输出流
            try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
                for (File file : fileList) {
                    if (file.exists()) {
                        compress(file, zos, file.getName(), true);
                    }
                }
            }

            response.flushBuffer();
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean keepDirStructure)
            throws IOException {
        byte[] buffer = new byte[4096];
        if (sourceFile.isFile()) {
            try (FileInputStream fis = new FileInputStream(sourceFile)) {
                ZipEntry zipEntry;
                if (keepDirStructure) {
                    zipEntry = new ZipEntry(name);
                } else {
                    zipEntry = new ZipEntry(sourceFile.getName());
                }
                zos.putNextEntry(zipEntry);
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
            }
        } else if (sourceFile.isDirectory()) {
            File[] files = sourceFile.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (keepDirStructure) {
                        compress(file, zos, name + File.separator + file.getName(), keepDirStructure);
                    } else {
                        compress(file, zos, file.getName(), keepDirStructure);
                    }
                }
            }
        }
    }

以上就是SpringBoot将多个文件夹进行压缩的两种方法(浏览器下载和另存为)的详细内容,更多关于SpringBoot文件夹压缩的资料请关注脚本之家其它相关文章!

相关文章

  • MyBatis 参数绑定的具体实现

    MyBatis 参数绑定的具体实现

    本文主要介绍了MyBatis 参数绑定的具体实现,包括默认参数名、@Param注解和POJO/DTO对象三种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-01-01
  • mybatis利用association或collection传递多参数子查询

    mybatis利用association或collection传递多参数子查询

    今天小编就为大家分享一篇关于mybatis利用association或collection传递多参数子查询,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Java中无参构造方法的一些核心作用总结

    Java中无参构造方法的一些核心作用总结

    构造方法是一个特殊的方法,用于创建类的实例,构造方法的名称必须与类名相同,并且没有返回类型,这篇文章主要介绍了Java中无参构造方法的一些核心作用,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2026-06-06
  • SpringBoot整合Mybatis-plus实现多级评论功能

    SpringBoot整合Mybatis-plus实现多级评论功能

    本文介绍了如何使用SpringBoot整合Mybatis-plus实现多级评论功能,同时提供了数据库的设计和详细的后端代码,前端界面使用的Vue2,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-05-05
  • SpringBoot 整合mybatis+mybatis-plus的详细步骤

    SpringBoot 整合mybatis+mybatis-plus的详细步骤

    这篇文章主要介绍了SpringBoot 整合mybatis+mybatis-plus的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 在SSM中配置了事务控制但没生效的问题

    在SSM中配置了事务控制但没生效的问题

    这篇文章主要介绍了在SSM中配置了事务控制但没生效的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Maven的安装+配置本地仓库路径方式

    Maven的安装+配置本地仓库路径方式

    这篇文章主要介绍了Maven的安装+配置本地仓库路径方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-09-09
  • springboot基于注解实现去重表消息防止重复消费

    springboot基于注解实现去重表消息防止重复消费

    本文主要介绍了springboot基于注解实现去重表消息防止重复消费,通过记录消息ID、使用分布式锁和设置过期时间,可以确保消息只会被处理一次,具有一定的参考价值,感兴趣的可以了解一下
    2025-05-05
  • spring boot 配置Filter过滤器的方法

    spring boot 配置Filter过滤器的方法

    本篇文章主要介绍了spring boot 配置Filter过滤器的方法,实例分析了spring boot 配置Filter过滤器的技巧,有兴趣的可以了解一下。
    2017-03-03
  • javascript fetch 用法讲解

    javascript fetch 用法讲解

    fetch 是一个现代化的 JavaScript API,用于发送网络请求并获取资源,它是浏览器提供的全局方法,可以替代传统的 XMLHttpRequest,这篇文章主要介绍了javascript fetch 用法讲解,需要的朋友可以参考下
    2025-05-05

最新评论