广告位联系
返回顶部
分享到

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

java 来源:互联网 作者:佚名 发布时间:2024-07-26 21:34:11 人浏览
摘要

1、将多个文件夹压缩成一个压缩包(压缩到固定目录) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

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、将多个文件夹压缩成一个压缩包(通过浏览器下载)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

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);

                  }

              }

          }

      }

  }


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计