springboot—将本地目录中的zip文件放入JavaSpringBoot中的列表中

unguejic  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(380)

在JavaSpringBoot应用程序中,有没有一种方法可以将一个充满zip文件的目录放在一个列表中?例如,在我的本地语言中: /Macintosh HD/Systems/Volumes/Macintosh HD/folder1/profile/sent 有一堆zip文件和trg文件,我想把它们加载到eclipsejava中,查看每一个文件中的内容(我想这就是列表的来源),我该怎么做呢?
我试过了

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”类型的文件?它是一个类型文件,但前面有路径字符串。我想我甚至不能打开这些zip,所以我想知道java中是否有类似zip的类型,然后我可以访问zip中的内容(里面大部分是excel电子表格,但我不需要访问excel电子表格)?

hgtggwj0

hgtggwj01#

到目前为止你所拥有的是好的吗?你有所有的zip文件名。
如果您想访问zip文件的内容(解压缩),那么有几个选项。jdk有java.util.zip.zipfile,或者您可以尝试现有的工具,如zip4j:

new ZipFile("filename.zip").extractAll("/destination_directory");

可能的堆栈溢出:
什么是一个好的java库来压缩/解压文件?
==========编辑:
获取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/

相关问题