英文:
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<String, String> env = new HashMap<>();
env.put("create", "true");
// locate file system by using the syntax
// defined in 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);
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论