How to convert the jar: file URI to a jar file path?

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

+4
source share
2 answers

The following code works for me (based on How to get only the banner URL from jar: URL containing "!" And a specific file in the bank? ):

 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()) 
+7
source

You can do it. These works

 ClassLoader loader = this.getClass().getClassLoader(); URL url = loader.getResource("resource name"); String[] filePath = null; String protocol = url.getProtocol(); if(protocol.equals("jar")){ url = new URL(url.getPath()); protocol = url.getProtocol(); } if(protocol.equals("file")){ String[] pathArray = url.getPath().split("!"); filePath = pathArray[0].split("/",2); } 

Required file = new file (FilePath [1]);

+3
source

Source: https://habr.com/ru/post/1379697/


All Articles