【问题标题】:How do I filter files in a directory by month, zip them based on month, rename them, place them in a folder that contains the zipfiles?如何按月过滤目录中的文件,按月压缩它们,重命名它们,将它们放在包含 zipfiles 的文件夹中?
【发布时间】:2020-03-01 02:03:36
【问题描述】:

我有一个包含 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 个标记为(基于 lastModified())2019_07、2019_08、2019_09 的 zip 文件夹,但我得到的是 2019_06、2019_07、2019_08、2019_10,每个文件夹只有 1 个文件。

【问题讨论】:

  • 请添加有关您的操作系统的信息。另外,请向我们展示您的程序代码,也许有人可以添加此功能。
  • 按月过滤”是什么意思?是否应该只包含某个月份之前的文件?还是按月对文件进行分组,每个组都放在自己的 ZIP 文件中? cutoff 对此有何影响?
  • 只有上个月的文件,而不是当前月份的文件,按月分组,每个组进入自己的 zip。 cutoff 以前用于获取超过 2 天的文件。我不确定我是否会在尝试编写的程序中使用它。

标签: java directory zip filtering file-rename


【解决方案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

将文件按上次修改时间的YearMonth 分组后,您需要将它们放入 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 实例属于同一个提供者。

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2020-09-02
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多