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

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

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:

URL url = SasPostman.class.getClassLoader().getResource(POSTMAN_RESOURCES_PATH);
// url output -> jar:file:/home/randomFolder/myJarFile.jar!/postman

try {
     JarURLConnection connection = (JarURLConnection) url.openConnection();
     File myFolder = new File(connection.getJarFileURL().toURI());
     if(myFolder.exists() && myFolder.isDirectory()) {
       // some code
     }

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.

connection.getURL() results in jar:file:/home/randomFolder/myJarFile.jar!/postman

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

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 :

URL url = SasPostman.class.getClassLoader().getResource(POSTMAN_RESOURCES_PATH);
// url output -> jar:file:/home/randomFolder/myJarFile.jar!/postman

try {
     JarURLConnection connection = (JarURLConnection) url.openConnection();
     File myFolder = new File(connection.getJarFileURL().toURI());
     if(myFolder.exists() && myFolder.isDirectory()) {
       // some code
     }

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.

connection.getURL() results to jar:file:/home/randomFolder/myJarFile.jar!/postman

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

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

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

你可以尝试这样做并根据需要进行调整

import java.util.Map;
import java.util.HashMap;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;

public class ZipFSPUser {
    public static void main(String[] args) throws Throwable {
        Map<String, String> env = new HashMap<>();
        env.put("create", "true");
        // 使用 java.net.JarURLConnection 中定义的语法来定位文件系统
        URI uri = URI.create("jar:file:///tmp/zipfstest.zip");

        try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
            Path jsonDir = zipfs.getPath("/root/a");
            try (DirectoryStream<Path> paths = Files.newDirectoryStream(jsonDir, "*.json")) {
                paths.forEach(System.out::println);
            }
        }
    }
}

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

英文:

You could try this and adapt it as necessary:

import java.util.Map;
import java.util.HashMap;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;

public class ZipFSPUser {
    public static void main(String[] args) throws Throwable {
        Map&lt;String, String&gt; env = new HashMap&lt;&gt;();
        env.put(&quot;create&quot;, &quot;true&quot;);
        // locate file system by using the syntax
        // defined in java.net.JarURLConnection
        URI uri = URI.create(&quot;jar:file:///tmp/zipfstest.zip&quot;);

        try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
            Path jsonDir = zipfs.getPath(&quot;/root/a&quot;);
            try (DirectoryStream&lt;Path&gt; paths = Files.newDirectoryStream(jsonDir, &quot;*.json&quot;)) {
                paths.forEach(System.out::println);
            }
        }
    }
}

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:

确定