java - 如何按月过滤目录中的文件、按月压缩它们、重命名它们、将它们放在包含 zip 文件的文件夹中?

标签 java directory zip filtering file-rename

我有一个包含超过 1000 个文件的目录,我需要根据月份对它们进行压缩,重命名它们并将压缩的文件放在一个文件夹中。我通常手动执行此操作,但我厌倦了这样做。我编写了一个程序,可以重命名文件并将它们放在新文件夹中,但我不知道如何按月过滤或在 Windows 10 上使用 java 压缩它们。

        String path = "C:\\\\Users\\\\srs\\\\Desktop\\\\Test\\notProcessed";

        File[] filelist = new File(path).listFiles();

        for (File file : filelist) {
            Date d = new Date(file.lastModified());
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            int iyear = c.get(Calendar.YEAR);
            int imonth = c.get(Calendar.MONTH);
            String syear = Integer.toString(iyear);
            String smonth = Integer.toString(imonth);
            System.out.println(syear + "_" + smonth);
            String destpath = "C:\\\\Users\\\\srs\\\\Desktop\\\\Test\\notProcessed\\\\TestZip\\\\";
            byte[] buffer = new byte[1024];
            try {
                FileOutputStream fos = new FileOutputStream(destpath + syear + "_" + smonth + ".zip");
                ZipOutputStream zos = new ZipOutputStream(fos);
                System.out.println("Output to Zip : " + file);
                System.out.println("File Added : " + file.getAbsolutePath().toString());
                ZipEntry ze = new ZipEntry(file.getName());
                zos.putNextEntry(ze);
                FileInputStream in = new FileInputStream(file);
                int len;
                while ((len = in.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
                in.close();
                zos.closeEntry();
                zos.close();
                System.out.println("Done");
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }

    }

这就是我到目前为止所拥有的。该程序运行,但它没有给我所需的结果。它应该给我 3 个标记为 2019_07、2019_08、2019_09 的 zip 文件夹(基于 lastModified()),但我得到的是 2019_06、2019_07、2019_08、2019_10,每个文件夹中只有 1 个文件。

最佳答案

分组

您当前使用的是File API 和旧的日期时间API(例如Date)。我建议你:

  1. 使用 java.nio.file API 而不是 File API。
  2. 使用 Java 8 中添加的 java.time API,而不是旧的日期时间 API。
    • 这一点特别重要。创建新代码时,应不惜一切代价避免使用旧的日期时间 API。

根据我对您问题的理解,您希望按文件上次修改时间的年份和月份对文件进行分组,并将它们放入自己的 ZIP 文件中。对于分组,我们可以使用 YearMonth类和 Files#walkFileTree(Path,Set,int,FileVisitor)方法。这是一个例子:

Map<YearMonth, List<Path>> groupFiles(Path dir, int depth) throws IOException {
  Map<YearMonth, List<Path>> result = new HashMap<>();

  Files.walkFileTree(dir, Set.of(), depth, new SimpleFileVisitor<>() {

    private final ZoneId systemZone = ZoneId.systemDefault();
    private final YearMonth currentYearMonth = YearMonth.now();

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
      YearMonth yearMonth = getYearMonthOfLastModifiedTime(attrs);
      if (yearMonth.isBefore(currentYearMonth)) {
        result.computeIfAbsent(yearMonth, k -> new ArrayList<>()).add(file);
      }
      return FileVisitResult.CONTINUE;
    }

    private YearMonth getYearMonthOfLastModifiedTime(BasicFileAttributes attrs) {
      Instant lastModInstant = attrs.lastModifiedTime().toInstant();
      return YearMonth.from(LocalDate.ofInstant(lastModInstant, systemZone));
    }
  });

  return result;
}

以上是使用系统默认时区。我还添加了选项来指定搜索文件树(根位于 dir)时要到达的最大深度。如果您只想查找 dir 的直接子文件,请使用 1。此外,在最大深度始终1的情况下,您可以使用DirectoryStream而不是FileVisitor >.

请注意,要获取 Path 实例,您可以调用 File#toPath()。然而,由于我们试图避免使用 File 类,因此最好使用 Path#of(String,String...) (或者,如果不使用 Java 11+,则使用 Paths#get)。例如:

Path path = Path.of("C:", "Users", "srs", "Desktop", "Test", "notProcessed"); 

上面的Path将与默认的FileSystem关联。

<小时/>

创建 ZIP

将文件按上次修改时间的年月分组后,您需要将它们放入 ZIP 文件中。在 JDK 中,至少有两个用于创建 ZIP 文件的选项:

  1. java.util.zip API。
  2. ZIP File System Provider (通过 java.nio.file API 使用)。

我相信,第一个选项可以让您更好地控制压缩过程。但是,第二个选项允许您以透明的方式像对待任何其他文件系统一样对待 ZIP 文件。对于这个答案,我将展示第二个选项的示例:

List<Path> compressFiles(Path zipDir, Map<YearMonth, List<Path>> groupedFiles) 
    throws IOException {
  List<Path> zipFiles = new ArrayList<>(groupedFiles.size());

  DateTimeFormatter zipFilenameFormatter = DateTimeFormatter.ofPattern("uuuu_MM'.zip'");
  for (Map.Entry<YearMonth, List<Path>> entry : groupedFiles.entrySet()) {
    Path zipFile = zipDir.resolve(zipFilenameFormatter.format(entry.getKey()));
    zipFiles.add(zipFile);

    URI uri = URI.create("jar:" + zipFile.toUri());
    Map<String, ?> env = Map.of("create", Boolean.toString(Files.notExists(zipFile)));
    try (FileSystem zipFileSystem = FileSystems.newFileSystem(uri, env)) {
      Path zipRoot = zipFileSystem.getRootDirectories().iterator().next();
      for (Path source : entry.getValue()) {
        Files.move(source, zipRoot.resolve(source.getFileName().toString()));
      }
    }
  }

  return zipFiles;
}

我使用DateTimeFormatter因为您的问题表明 ZIP 文件的文件名应该是 year_month.zip (带下划线)。 YearMonth#toString() 方法将返回 year-month(带有破折号),因此 DateTimeFormatter 用于分隔年和月一个下划线。如果您不介意破折号,那么您可以简单地使用 yearMonth.toString() + ".zip" 创建文件名。

上面使用Files#move(Path,Path,CopyOption...)实际将文件添加到 ZIP 文件中。文件将被压缩。请注意,如果 ZIP 文件中已存在具有该名称的条目,此操作将会失败,但您可以使用 REPLACE_EXISTING 更改此设置。调用#move将删除源文件;如果不需要,请考虑使用 Files#copy 代替。

请注意,我使用 Path#resolve(String) 而不是 Path#resolve(Path) ,因为根据我的经验,后者要求两个 Path 实例属于同一提供者。

关于java - 如何按月过滤目录中的文件、按月压缩它们、重命名它们、将它们放在包含 zip 文件的文件夹中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698786/

相关文章:

java - 使用 gradle 分发插件中的 zip/tar

java - executorservice 以 block 的形式从数据库中读取数据并在其上运行进程

java - NavigationDrawerActivity 和 BottomNavigationActivity

java - 如何在给定文件夹名称的情况下创建多个目录

python - 使用 Python 将内容从单个文件夹拆分到多个子文件夹

python - 如何从 zip 文件夹中的 csv 文件读取数据并将 csv 文件中的数据保存到数据库中?

java - Spring Framework中applicationContext.xml和spring-servlet.xml的区别

android-studio - Android Studio build/apk 不可见

python - 使用 python zipfile 归档符号链接(symbolic link)

javascript - 使用 Javascript 如何从使用 PhoneGap 构建的应用程序在移动设备上解压缩文件?