英文:
Get all files inside directory's in jar file in java
问题
假设我的项目位置是“some/random/guy”,在项目内部有一个路径为“some/random/guy/versions”的目录,我想获取该文件夹内的所有文件/目录,以便获取所有可用版本。
但是版本目录只有在项目编译一次后才会创建,所以它不在我的集成开发环境中。
在已经搜索了大约2小时后,我仍然没有找到答案。
目前我正在使用来自Oracle文档的代码,但它也不起作用:
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("some/random/guy/versions"))) {
for (Path file : stream) {
System.out.println(file.getFileName());
}
} catch (IOException | DirectoryIteratorException e) {
System.err.print(e);
}
我如何才能获取位于jar文件内部的任何文件夹中的所有文件/目录?
如果可能的话,我想避免解压缩jar文件,谢谢。
英文:
Lets say the location of my project is "some/random/guy", and inside the project there is a directory located with the path "some/random/guy/versions", I want to get all files/directory's inside that folder so I can get all the availible versions.
But the versions dir only gets created once the project is compiled, so its not in my IDE.
After searching like 2 hours already I couldn't find my answer.
Currently Im using the code from oracle docs but it also doesn't work:
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("some/random/guy/versions"))) {
for (Path file: stream) {
System.out.println(file.getFileName());
}
} catch (IOException | DirectoryIteratorException e) {
System.err.print(e);
}
How can I get all files/directory's inside any folder thats located inside a jar file?
I would like to avoid unzipping the jar file if possible, thanks.
答案1
得分: 1
以下是翻译好的内容:
这段代码将使用NIO FileSystem处理显示任何jar/zip存档的内容,您需要传入jar的位置和可选的jar内路径:
public static void main(String[] args) throws IOException {
Path jar = Path.of(args[0]);
String relPath = args.length > 1 ? args[1] : "/";
System.out.println("ZIP/JAR: " + jar + " isRegularFile()=" + Files.isRegularFile(jar));
try (FileSystem fs = FileSystems.newFileSystem(jar)) {
Path path = fs.getPath(relPath);
System.out.println("SCAN " + jar + " starting at path: " + path);
try(Stream<Path> str = Files.find(path, Integer.MAX_VALUE, (p,a) -> true)) {
str.forEach(System.out::println);
}
}
}
英文:
This code will show contents of any jar/zip archive using NIO FileSystem handling, you need to pass in a location of the jar and optional path under the jar:
public static void main(String[] args) throws IOException {
Path jar = Path.of(args[0]);
String relPath = args.length > 1 ? args[1] : "/";
System.out.println("ZIP/JAR: "+jar+" isRegularFile()="+Files.isRegularFile(jar));
try (FileSystem fs = FileSystems.newFileSystem(jar)) {
Path path = fs.getPath(relPath);
System.out.println("SCAN "+ jar+" starting at path: "+path);
try(Stream<Path> str = Files.find(path, Integer.MAX_VALUE, (p,a) -> true)) {
str.forEach(System.out::println);
}
}
}
答案2
得分: 0
jar
文件就是普通的zip
压缩文件。你不需要解压jar
文件就可以获取所有条目的名称,因为zip
文件包含了CentralDirectory
。你只需要使用标准的ZipInputStream
或者其中一个框架,比如zip4jvm:
import ru.olegcherednik.zip4jvm.ZipMisc;
public static List<String> getAllEntryNames(Path jar) throws IOException {
return ZipMisc.zip(jar).getEntries()
.map(ZipFile.Entry::getFileName)
.collect(Collectors.toList());
}
这段代码可以检索出所有已存在的条目名称。目录(如果它们作为单独的条目存在)以/
结尾。
英文:
jar
file is plain old zip
archive. You do not neet unzip jar
to get all entries' names, because zip
files contains CentralDirectory
. All you need is to use standard ZipInputStream
or one of framework, e.g. zip4jvm:
import ru.olegcherednik.zip4jvm.ZipMisc;
public static List<String> getAllEntryNames(Path jar) throws IOException {
return ZipMisc.zip(jar).getEntries()
.map(ZipFile.Entry::getFileName)
.collect(Collectors.toList());
}
This snippet retrieves all existed entry names. Directory (if they exists as separate entry) ends with /
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论