WalkFileThree inside JAR file

I am having trouble getting the root path inside a JAR file. I am writing a method to extract the contents of a Jar / Zip file to a target directory using Java NIO walkFileThree. The method now 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;
    }
  });
}
}

      

But I am getting an 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]

      

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

Thank!

+3


source to share


1 answer


Got this, just added toString () after relativize () as suggested here: FileSystemNotFoundException

The working code now looks like this:



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

      

+3


source







All Articles