Resclone/src/main/java/io/gitlab/jfronny/resclone/processors/RemoveEmptyProcessor.java

33 lines
1.1 KiB
Java
Raw Normal View History

2020-12-29 16:14:53 +01:00
package io.gitlab.jfronny.resclone.processors;
import io.gitlab.jfronny.resclone.io.PathPruneVisitor;
import java.io.IOException;
import java.nio.file.DirectoryStream;
2020-12-29 16:14:53 +01:00
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
2020-12-29 16:14:53 +01:00
public class RemoveEmptyProcessor extends PackProcessor {
2021-04-03 02:40:38 +02:00
2020-12-29 16:14:53 +01:00
@Override
2021-04-03 02:40:38 +02:00
public void process(FileSystem p) throws Exception {
2020-12-29 22:22:23 +01:00
if (Files.exists(p.getPath("/assets"))) {
try {
Files.walkFileTree(p.getPath("/assets"), new PathPruneVisitor(s -> {
if (Files.isDirectory(s)) {
try (DirectoryStream<Path> paths = Files.newDirectoryStream(s)) {
return !paths.iterator().hasNext();
2020-12-29 22:22:23 +01:00
} catch (IOException e) {
e.printStackTrace();
}
2020-12-29 16:14:53 +01:00
}
2020-12-29 22:22:23 +01:00
return false;
}));
} catch (Throwable e) {
2021-04-03 02:40:38 +02:00
throw new Exception("Failed to prune empty directories", e);
2020-12-29 22:22:23 +01:00
}
2020-12-29 16:14:53 +01:00
}
}
2021-04-03 02:40:38 +02:00
}