下载文件夹

小鱼儿 2022-08-08 00:56 269阅读 0赞

下载文件夹,我们可以先将文件夹打包,然后进行下载,可以保留包,不删除,用时间戳方式判断是否删除后重建。

文件下载代码:

  1. response.setHeader("Content-Type", "application/zip");
  2. response.setHeader("Content-Disposition", "attachment;filename=" + zipFile.getName());
  3. try {
  4. OutputStream to = response.getOutputStream();
  5. Files.copy(zipFile, to);
  6. to.flush();
  7. response.flushBuffer();
  8. } catch (IOException e) {
  9. e.getStackTrace();
  10. }

打包:

  1. /*
  2. * Copyright 2010.
  3. *
  4. * This document may not be reproduced, distributed or used
  5. * in any manner whatsoever without the expressed written
  6. * permission of Boventech Corp.
  7. *
  8. * $Rev: Rev $
  9. * $Author: Author $
  10. * $LastChangedDate: LastChangedDate $
  11. *
  12. */
  13. package com.boventech.zyk.util;
  14. import java.io.File;
  15. import org.apache.tools.ant.Project;
  16. import org.apache.tools.ant.taskdefs.Expand;
  17. import org.apache.tools.ant.taskdefs.Zip;
  18. import org.apache.tools.ant.types.FileSet;
  19. /**
  20. * 提供对文件进行压缩的操作
  21. */
  22. public class Ziper {
  23. public static File doZip(File srcDir) {
  24. if (!srcDir.exists()) {
  25. throw new RuntimeException(srcDir + " isn't exist!");
  26. }
  27. File zipFile = new File(srcDir.getPath() + ".zip");
  28. /*
  29. * 使用Ant的zip工具类,这里进行实例化
  30. */
  31. Project project = new Project();
  32. Zip zip = new Zip();
  33. zip.setProject(project);
  34. zip.setDestFile(zipFile);
  35. /*
  36. * 获取目标文件夹的文件集合
  37. */
  38. FileSet fileSet = new FileSet();
  39. fileSet.setProject(project);
  40. fileSet.setDir(srcDir);
  41. /*
  42. * 执行压缩操作
  43. */
  44. zip.addFileset(fileSet);
  45. zip.execute();
  46. return zipFile;
  47. }
  48. public static String unZip(File srcFile, String destDir) {
  49. if (!srcFile.exists()) {
  50. throw new RuntimeException(srcFile + " isn't exist!");
  51. }
  52. Expand expand = new Expand();
  53. expand.setProject(new Project());
  54. expand.setSrc(srcFile);
  55. if (destDir == null) {
  56. destDir = srcFile.getPath().substring(0, srcFile.getPath().lastIndexOf("."));
  57. }
  58. expand.setDest(new File(destDir));
  59. expand.execute();
  60. return destDir;
  61. }
  62. }

发表评论

表情:
评论列表 (有 0 条评论,269人围观)

还没有评论,来说两句吧...

相关阅读

    相关 下载文件夹

    下载文件夹,我们可以先将文件夹打包,然后进行下载,可以保留包,不删除,用时间戳方式判断是否删除后重建。 文件下载代码: response.setHeader("Co