How do I convert a "jar:file" URI to the path of Jar file?

java, uri

Solution

The following code works for me (based on How do I get just the jar URL from a jar: URL containing a "!" and a specific file in the jar?):

URL url = new URL("jar:file:/C:/Program%20Files/test.jar!/foo/bar");
JarURLConnection connection = (JarURLConnection) url.openConnection();
File file = new File(connection.getJarFileURL().toURI())

Problem

How do I go from `jar:file:/C:/Program%20Files/test.jar!/foo/bar` to a `File` that points to `C:/Program Files/test.jar`?

Original source

Related problems