gpt4 book ai didi

java - JAR 文件中的 walkFileThree

转载 作者:搜寻专家 更新时间:2023-11-01 03:02:39 25 4
gpt4 key购买 nike

我无法获取 JAR 文件中的根路径。我正在编写一种使用 Java NIO walkFileThree 将 Jar/Zip 文件的内容提取到目标目录的方法。该方法目前看起来像这样:

public static void unzip(Path filePath, Path destination) throws IOException {
Map<String, String> zipProperties = new HashMap<>();
/* We want to read an existing ZIP File, so we set this to False */
zipProperties.put("create", "false");
zipProperties.put("encoding", "UTF-8");
URI zipFile = URI.create("jar:file:" + filePath.toUri().getPath());

try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, zipProperties)) {
Path rootPath = zipfs.getPath("/");
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

Path targetPath = destination.resolve(rootPath.relativize(dir));
if (!Files.exists(targetPath)) {
Files.createDirectory(targetPath);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

Files.copy(file, destination.resolve(rootPath.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
}

但我得到一个异常(exception):

java.nio.file.ProviderMismatchException
at sun.nio.fs.UnixPath.toUnixPath(UnixPath.java:200) ~[?:1.8.0_45]
at sun.nio.fs.UnixPath.resolve(UnixPath.java:397) ~[?:1.8.0_45]
at sun.nio.fs.UnixPath.resolve(UnixPath.java:43) ~[?:1.8.0_45]

获取 zip 文件的根路径 (/) 以便我可以递归地将所有内容复制到目标文件夹的正确方法是什么?

谢谢!

最佳答案

得到它的工作,只是按照此处的建议在 relativize() 之后添加了一个 toString():FileSystemNotFoundException

现在的工作代码如下所示:

public static void unzip(Path filePath, Path destination) throws IOException {
//Path filePath = Paths.get( zipFilePath );
Map<String, String> zipProperties = new HashMap<>();
/* We want to read an existing ZIP File, so we set this to False */
zipProperties.put("create", "false");
zipProperties.put("encoding", "UTF-8");
URI zipFile = URI.create("jar:file:" + filePath.toUri().getPath());

try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, zipProperties)) {
Path rootPath = zipfs.getPath("/");
Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

Path targetPath = destination.resolve(rootPath.relativize(dir).toString());
if (!Files.exists(targetPath)) {
Files.createDirectory(targetPath);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

Files.copy(file, destination.resolve(rootPath.relativize(file).toString()), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
}
}

关于java - JAR 文件中的 walkFileThree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com