当前位置: 代码网 > it编程>编程语言>Java > SpringBoot将多个文件夹进行压缩的两种方法(浏览器下载和另存为)

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

2024年07月26日 Java 我要评论
1、将多个文件夹压缩成一个压缩包(压缩到固定目录)import java.io.*;import java.util.list;import java.util.zip.zipentry;import

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文件夹压缩的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com