質問

I want to copy a directory (including content) from a URL I get from

Some.class.getClassLoader().getResource("folder");

which returns either jar:file:/D:/../some.jar!/someparent or file:/D:/../someparent

I thought of a FileSystem (for jar intern dirs) and recursively copying with Files.walkFileTree(..).

Is it possible to modify jar:file:/D:/../some.jar!/folder to get a FileSystem NIO2 only?

役に立ちましたか?

解決 2

What I was looking for was something like:

JarURLConnection connection = (JarURLConnection) sourceUrl
     .openConnection();

Paths.get(connection.getJarFileURL().toURI());

他のヒント

    URL url = new URL("file:/C:/projektoj/ĝeneralaj");
    Path path = Paths.get(url.toURI());

C:\projektoj\ĝeneralaj

If the URL points into a jar/zip ("jar:file:/... .zip!.../...") then one may use the Path in the zip FileSystem, see: java: change ZipEntry name

Take a look at :
http://docs.oracle.com/javase/tutorial/essential/io/pathClass.html

Version Note: If you have pre-JDK7 code that uses java.io.File, you can still take advantage of the Path class functionality by using the File.toPath method. See Legacy File I/O Code for more information.

Edited: I guess File.toPath() method should be what you want, it returns a Path, in this case Paths.get(URI) is not necessary.

Edit again: You can take advantage of Paths.get(URL.toURI()) if you start with an URL, but need to handle URISyntaxException thrown from URL.toURI().

As mentioned by @Stephen C, Path is a nio2 feature.

In reality, NIO.2 is really just a convenient label for new NIO features added in the Java 7 overhaul. That is ... unless you are placing some other meaning on the term.

If you want a solution that doesn't involve anything that is not NIO.2, then (strictly speaking) that is not possible. Any solution will involve using String, and String predates NIO.2. It is therefore "not NIO.2"

If alternatively, you are going to allow use of classes that are in NIO.2 but not NIO, then File is neither NIO.2 or NIO. It predates both of them.


Is it possible to modify "jar:file:/D:/../some.jar!/folder" to get a FileSystem NIO2 only?

If you are asking if it is possible to create a FileSystem provider that could handle that, the answer is Yes in theory, and it has probably already been done ... modulo restrictions on what you can do to a JAR file. (In-place update via the Path / FileSystem would be really difficult to implement, and expensive in terms of runtime resources.)

If you are constraining the above problem by saying that only NIO.2 APIs should be used to implement the provider and/or use it, then the answer is probably No. (But it depends on what you really mean by "only NIO.2" ... and that depends on what you are really trying to achieve ...)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top