Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取目录';java中jar文件中的_Java - Fatal编程技术网

获取目录';java中jar文件中的

获取目录';java中jar文件中的,java,Java,假设我的项目的位置是“some/random/guy”,在项目中有一个路径为“some/random/guy/versions”的目录,我想获取该文件夹中的所有文件/目录,以便获取所有可用的版本。 但是versions目录只在编译项目后创建,所以它不在我的IDE中 搜索了大约两个小时后,我找不到我的答案 目前我正在使用oracle文档中的代码,但它也不起作用: try (DirectoryStream<Path> stream = Files.newDirectoryStream(P

假设我的项目的位置是“some/random/guy”,在项目中有一个路径为“some/random/guy/versions”的目录,我想获取该文件夹中的所有文件/目录,以便获取所有可用的版本。 但是versions目录只在编译项目后创建,所以它不在我的IDE中

搜索了大约两个小时后,我找不到我的答案

目前我正在使用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);
    }
try(DirectoryStream=Files.newDirectoryStream(path.get(“some/random/guy/versions”)){
用于(路径文件:流){
System.out.println(file.getFileName());
}
}捕获(IOException | DirectoryIteratorException e){
系统错误打印(e);
}
如何获取jar文件中任何文件夹中的所有文件/目录


如果可能,我希望避免解压缩jar文件,谢谢。

此代码将使用NIO文件系统处理显示任何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);
        }
    }
}
publicstaticvoidmain(字符串[]args)引发IOException{
Path jar=Path.of(args[0]);
字符串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(“扫描”+jar+”从路径“+path”开始);
try(Stream str=Files.find(path,Integer.MAX_VALUE,(p,a)->true)){
str.forEach(System.out::println);
}
}
}

jar
文件是纯旧的
zip
存档文件。您不需要解压缩
jar
来获取所有条目的名称,因为
zip
文件包含
CentralDirectory
。您只需使用标准
ZipInputStream
或框架之一,例如:

导入ru.olegcherdenik.zip4jvm.ZipMisc;
公共静态列表getAllEntryNames(路径jar)引发IOException{
返回ZipMisc.zip(jar.getEntries())
.map(ZipFile.Entry::getFileName)
.collect(Collectors.toList());
}

此代码段检索所有已存在的条目名。目录(如果它们作为单独的条目存在)以
/

结尾,您提到的jar在哪里?路径“some/random/guy/versions”是在一个jar中还是在一个包含jar的文件夹中?谢谢,这解决了这个问题,尽管我不得不更改一些代码,但这成功了!
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());
}