我写了一些测试,在一些虚拟目录中压缩文件:
public void compressFileToZipTest() throws IOException{
try (DirectoryStream<Path> stream = Files.newDirectoryStream(
Paths.get("src", "test", "resources", "dummy", "files"), new FilesFilter())){
for (Path entry: stream) {
Path dst = entry.getParent().resolve(entry.getFileName().toString() + ".zip");
boolean succeed = FilesUtility.compressToZip(entry, dst);
Assert.assertTrue("Failed to create destination file", succeed);
try(ZipFile zipFile = new ZipFile(dst.toFile())){
Assert.assertTrue("Failed to compress file size", Files.size(entry) > Files.size(dst));
}
dst.toFile().deleteOnExit();
}
}
}
private static final class FilesFilter implements DirectoryStream.Filter<Path>{
@Override
public boolean accept(Path entry) throws IOException {
return !Files.isDirectory(entry);
}
}
测试运行良好,直到我们的构建团队更改了下划线的 NAS 存储,这导致从目录流式传输并写入它时出现无限循环。
如果有人能解释这个问题,我会很高兴(我假设它与路径迭代器实现有关)。