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文件夹压缩的资料请关注脚本之家其它相关文章!

相关文章

  • Kafka日志清理实现详细过程讲解

    Kafka日志清理实现详细过程讲解

    这篇文章主要为大家介绍了Kafka日志清理实现详细过程讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • SpringBoot+Vue跨域配置(CORS)问题得解决过程

    SpringBoot+Vue跨域配置(CORS)问题得解决过程

    在使用 Spring Boot 和 Vue 开发前后端分离的项目时,跨域资源共享(CORS)问题是一个常见的挑战,接下来,我将分享我是如何一步步解决这个问题的,包括中间的一些试错过程,希望能够帮助到正在经历类似问题的你
    2024-08-08
  • SpringBoot整合Ldap的实现示例

    SpringBoot整合Ldap的实现示例

    本文主要介绍了SpringBoot整合Ldap的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11
  • MybatisPlus EntityWrapper如何自定义SQL

    MybatisPlus EntityWrapper如何自定义SQL

    这篇文章主要介绍了MybatisPlus EntityWrapper如何自定义SQL,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 一文详解Redisson分布式锁底层实现原理

    一文详解Redisson分布式锁底层实现原理

    这篇文章主要详细介绍了Redisson分布式锁底层实现原理,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • java实现日期拆分的方法

    java实现日期拆分的方法

    这篇文章主要介绍了java实现日期拆分的方法,基于java日期类实现对日期字符串的拆分功能,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • JavaWeb实现学生信息管理系统(2)

    JavaWeb实现学生信息管理系统(2)

    这篇文章主要介绍了JavaWeb实现学生信息管理系统的第二篇,实现学生管理系统的查找和添加功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • idea安装插件找不到的问题及解决

    idea安装插件找不到的问题及解决

    这篇文章主要介绍了idea安装插件找不到的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • 分享java中设置代理的两种方式

    分享java中设置代理的两种方式

    有时候我们的程序中要提供可以使用代理访问网络,代理的方式包括http、https、ftp、socks代理。比如在IE浏览器设置代理,这里简单介绍下,需要的朋友可以参考下
    2015-12-12
  • Java的MyBatis框架中关键的XML字段映射的配置参数详解

    Java的MyBatis框架中关键的XML字段映射的配置参数详解

    将XML文件的schema字段映射到数据库的schema是我们操作数据库的常用手段,这里我们就来整理一些Java的MyBatis框架中关键的XML字段映射的配置参数详解,需要的朋友可以参考下
    2016-06-06

最新评论