java创建文件夹和遍历压缩文件夹或者文件

谁践踏了优雅 2024-03-24 08:50 196阅读 0赞

1:遍历压缩文件中的文件夹(包括空文件)和文件

  1. package org.xqf.common.es;
  2. /**
  3. * @Author xu
  4. * @create 2023/3/24 17
  5. */
  6. import lombok.SneakyThrows;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipOutputStream;
  15. public class ZipFolderExampleTest2 {
  16. @SneakyThrows
  17. public static void main(String[] args) {
  18. //创建临时temp文件
  19. // File zipFile = File.createTempFile("example", ".zip");
  20. List<String> sourceFolderList=new ArrayList<>();
  21. sourceFolderList.add("aa");
  22. sourceFolderList.add("bb");
  23. String sourceFolder = "C:\\Users\\dell\\Desktop\\folder\\";
  24. String zipFilePath = "C:\\Users\\dell\\Desktop\\folder.zip";
  25. try {
  26. FileOutputStream fos = new FileOutputStream(zipFilePath);
  27. ZipOutputStream zos = new ZipOutputStream(fos);
  28. File sourceFile = new File(sourceFolder);
  29. sourceFile.mkdirs();
  30. if (sourceFile.exists()) {
  31. for (String floder : sourceFolderList) {
  32. File file = new File(sourceFolder + floder);
  33. file.mkdir();
  34. System.out.println(file.getName());
  35. }
  36. addFolderToZip(sourceFile, sourceFile.getName(), zos);
  37. zos.finish();
  38. zos.close();
  39. fos.close();
  40. System.out.println("Folder has been compressed successfully!");
  41. } else {
  42. System.out.println("Source folder does not exist!");
  43. }
  44. // zipFile.delete();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. private static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
  50. for (File file : folder.listFiles()) {
  51. //file.list().length 判断目录下面是否有文件或者目录,没有直接创建,有继续循环
  52. if (file.isDirectory() && file.list().length > 0) {
  53. addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
  54. continue;
  55. }
  56. if (!file.isDirectory()) {
  57. zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
  58. FileInputStream fis = new FileInputStream(file);
  59. byte[] buffer = new byte[1024];
  60. int length;
  61. while ((length = fis.read(buffer)) > 0) {
  62. zos.write(buffer, 0, length);
  63. }
  64. fis.close();
  65. } else {
  66. zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName() + "/"));
  67. }
  68. zos.closeEntry();
  69. }
  70. }
  71. }

2:如果需要过滤空文件夹,修改方法addFolderToZip

  1. rivate static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws IOException {
  2. for (File file : folder.listFiles()) {
  3. if (file.isDirectory()) {
  4. addFolderToZip(file, parentFolder + "/" + file.getName(), zos);
  5. continue;
  6. }
  7. zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
  8. FileInputStream fis = new FileInputStream(file);
  9. byte[] buffer = new byte[1024];
  10. int length;
  11. while ((length = fis.read(buffer)) > 0) {
  12. zos.write(buffer, 0, length);
  13. }
  14. fis.close();
  15. } else {
  16. zos.closeEntry();
  17. }
  18. }

3:创建空文件夹并压缩

  1. 在这里插入代码片package org.jeecg.common.es;
  2. /**
  3. * @Author xu
  4. * @create 2023/3/24 17
  5. */
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipOutputStream;
  15. public class ZipFolderExampleTest3 {
  16. public static void main(String[] args) throws IOException {
  17. // 定义要创建的空文件夹
  18. List<String> folders = new ArrayList<>();
  19. folders.add("folder1");
  20. folders.add("folder2");
  21. folders.add("folder3");
  22. // 创建空文件夹
  23. for (String folderName : folders) {
  24. File folder = new File(folderName);
  25. if (!folder.exists()) {
  26. folder.mkdir();
  27. }
  28. }
  29. // 压缩文件夹
  30. Path zipFilePath = Paths.get("folders.zip");
  31. ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zipFilePath));
  32. for (String folderName : folders) {
  33. File folder = new File(folderName);
  34. if (folder.exists()) {
  35. String folderPath = folder.getPath();
  36. if (folderPath.endsWith("/")) {
  37. folderPath = folderPath.substring(0, folderPath.length() - 1);
  38. }
  39. ZipEntry zipEntry = new ZipEntry(folderPath.substring(folderPath.lastIndexOf("/") + 1) + "/");
  40. zipOutputStream.putNextEntry(zipEntry);
  41. zipOutputStream.closeEntry();
  42. }
  43. folder.delete();
  44. }
  45. zipOutputStream.close();
  46. }
  47. }

发表评论

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

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

相关阅读