JAR文件中的walkFileThree [英] walkFileThree inside JAR-file

查看:90
本文介绍了JAR文件中的walkFileThree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在JAR文件中获取根路径.我正在编写一种使用Java NIO walkFileThree将Jar/Zip文件的内容提取到目标目录的方法.该方法当前如下所示:

I'm having trouble to get the root-path inside a JAR-file. I'm writing a method for extracting the content of a Jar/Zip-file to a target directory using Java NIO walkFileThree. The method currently looks like this:

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;
    }
  });
}
}

但我得到一个例外:

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文件的根路径(/)以便我可以将所有内容递归复制到目标文件夹的正确方法是什么?

What is the proper way to get the root-path(/) of the zip file so that I could recursively copy all the content to the destination folder?

谢谢!

推荐答案

要使其正常运行,只需按照以下建议在relativize()之后添加toString()即可:

Got it working, just added a toString() after relativize() as suggested here: 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;
    }
  });
 }
}

这篇关于JAR文件中的walkFileThree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆