有没有办法将一个充满 zip 文件的目录放入 java springboot 应用程序的列表中?以我本地为例:
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent
有一堆 zip 文件和 trg 文件,我想将它们加载到 eclipse java 中并查看每个文件内部的内容(我假设这是列表的来源),我该怎么做?
我尝试过这样做
String folder1path = "/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent";
List<File> fileList = Arrays.asList(new File(folder1path).listFiles());
logger.info(fileList);
但这会产生一个如下所示的列表:
[
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip1.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip2.trg
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip5.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip3.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip4.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip1.trg
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip2.zip
/Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent/testZip4.trg
...
]
我以前没有使用过此类内容,但我认为这不是“Zip”类型文件?它是一个 File 类型,但前面有路径字符串。我不认为我什至可以打开这些zip,所以我想知道java中是否有一个Zip类型,然后我可以访问zip内的内容(里面主要是excel电子表格,但我不这样做)不需要访问 Excel 电子表格)?
最佳答案
到目前为止你所拥有的还不错吗?您拥有所有 zip 文件名。
如果您想访问 zip 文件的内容(解压缩),那么有几个选项。 JDK 有 java.util.zip.ZipFile 或者您可以尝试现有的工具,例如 zip4j:
new ZipFile("filename.zip").extractAll("/destination_directory");
可能有用的 stackoverflow:
What is a good Java library to zip/unzip files?
========= 编辑:
获取FileAttributes,然后使用lastModifiedTime方法:
try {
BasicFileAttributes fileAttributes = Files.readAttributes(new File("C:\\some_file.zip").toPath(),BasicFileAttributes.class);
System.out.println(fileAttributes.lastModifiedTime());
} catch (IOException e) {
e.printStackTrace();
}
还有其他列出目录的方法:
列出所有以 .java 结尾的文件
try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) {
List<String> result = walk.map(x -> x.toString())
.filter(f -> f.endsWith(".java")).collect(Collectors.toList());
result.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
https://mkyong.com/java/java-how-to-list-all-files-in-a-directory/
关于java - 将本地目录中的 zip 文件放入 java spring boot 中的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65115579/