如何检索JAR文件中一个文件夹内包含的所有文件?

huangapple go评论97阅读模式
英文:

How to retrieve all files contained in a folder inside a JAR?

问题

I need to get a folder within a jar file.

The folder is named "postman" and contains some JSON files.
My purpose is to get that folder as a File object, list the files to do some actions on names and parse the content for some other actions.

The problem I encountered is that I can't get that folder and its content.

After reading some documentation and topics from here, I tried many things, increasing my understanding of that kind of stuff.

Now I have this:

  1. URL url = SasPostman.class.getClassLoader().getResource(POSTMAN_RESOURCES_PATH);
  2. // url output -> jar:file:/home/randomFolder/myJarFile.jar!/postman
  3. try {
  4. JarURLConnection connection = (JarURLConnection) url.openConnection();
  5. File myFolder = new File(connection.getJarFileURL().toURI());
  6. if(myFolder.exists() && myFolder.isDirectory()) {
  7. // some code
  8. }

MyFolder exists but isn't a directory.
I added some logs to try to understand each step, but I am not able to draw conclusions from it.

  1. connection.getURL() results in jar:file:/home/randomFolder/myJarFile.jar!/postman
  2. connection.getURL().toURI() results in jar:file:/home/randomFolder/myJarFile.jar!/postman
  3. connection.getJarFileURL().toURI() results in jar:file:/home/randomFolder/myJarFile.jar

Even if I use the paths ending with the folder name, myFolder isn't the expected directory.

I probably missed something, or maybe it should be done in another way. Maybe by copying the whole folder I need into another location? I don't know.

I also looked at some documentation of using ZipFile, but I just see how to use it to retrieve one file. Or I need to get all the files from my folder.

Any help would be appreciated!

英文:

I need to get a folder within a jar file.

The folder is name postman and contains some JSON files.
My purpose is to get that folder as a File object, list the files to do some actions on names and parse the content for some other actions.

The problem I got is that I can't get that folder and his content.

After reading some documentation and topics from here, I tried many things, increase my understanding about that kind of stuff.

Now I got this :

  1. URL url = SasPostman.class.getClassLoader().getResource(POSTMAN_RESOURCES_PATH);
  2. // url output -> jar:file:/home/randomFolder/myJarFile.jar!/postman
  3. try {
  4. JarURLConnection connection = (JarURLConnection) url.openConnection();
  5. File myFolder = new File(connection.getJarFileURL().toURI());
  6. if(myFolder.exists() && myFolder.isDirectory()) {
  7. // some code
  8. }

MyFolder exist but isn't a directory.
I added some log to try to understand each steps, but I am not able to draw conclusion from it.

  1. connection.getURL() results to jar:file:/home/randomFolder/myJarFile.jar!/postman
  2. connection.getURL().toURI() results to jar:file:/home/randomFolder/myJarFile.jar!/postman
  3. connection.getJarFileURL().toURI() results to jar:file:/home/randomFolder/myJarFile.jar

Even if I use the paths endind with the folder name, myFolder isn't the expected directory.

I probably miss something or maybe it should be done in another way. Maybe by coying the whole folder I need into another location ? I don't know..

I also looked at some documentation of using ZipFile but I just see how to use it to retrieve one file. Or I need to get all the files from my folder.

Any help would be appreciated !

答案1

得分: 0

以下是翻译好的代码部分:

  1. 你可以尝试这样做并根据需要进行调整
  2. import java.util.Map;
  3. import java.util.HashMap;
  4. import java.net.URI;
  5. import java.nio.file.Path;
  6. import java.nio.file.DirectoryStream;
  7. import java.nio.file.Files;
  8. import java.nio.file.FileSystem;
  9. import java.nio.file.FileSystems;
  10. public class ZipFSPUser {
  11. public static void main(String[] args) throws Throwable {
  12. Map<String, String> env = new HashMap<>();
  13. env.put("create", "true");
  14. // 使用 java.net.JarURLConnection 中定义的语法来定位文件系统
  15. URI uri = URI.create("jar:file:///tmp/zipfstest.zip");
  16. try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
  17. Path jsonDir = zipfs.getPath("/root/a");
  18. try (DirectoryStream<Path> paths = Files.newDirectoryStream(jsonDir, "*.json")) {
  19. paths.forEach(System.out::println);
  20. }
  21. }
  22. }
  23. }

如果需要进一步的帮助或翻译,请随时提出。

英文:

You could try this and adapt it as necessary:

  1. import java.util.Map;
  2. import java.util.HashMap;
  3. import java.net.URI;
  4. import java.nio.file.Path;
  5. import java.nio.file.DirectoryStream;
  6. import java.nio.file.Files;
  7. import java.nio.file.FileSystem;
  8. import java.nio.file.FileSystems;
  9. public class ZipFSPUser {
  10. public static void main(String[] args) throws Throwable {
  11. Map&lt;String, String&gt; env = new HashMap&lt;&gt;();
  12. env.put(&quot;create&quot;, &quot;true&quot;);
  13. // locate file system by using the syntax
  14. // defined in java.net.JarURLConnection
  15. URI uri = URI.create(&quot;jar:file:///tmp/zipfstest.zip&quot;);
  16. try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
  17. Path jsonDir = zipfs.getPath(&quot;/root/a&quot;);
  18. try (DirectoryStream&lt;Path&gt; paths = Files.newDirectoryStream(jsonDir, &quot;*.json&quot;)) {
  19. paths.forEach(System.out::println);
  20. }
  21. }
  22. }
  23. }

huangapple
  • 本文由 发表于 2023年5月25日 17:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定