简体   繁体   中英

Java - Reading files inside jar

I know that there are plenty of ways to do this, however I'm experiencing problems in this task which I can't solve with the solutions that I have already found.

First of, I don't want to read a specific file inside a jar: I want to read all the files contained in a directory inside the jar given the path of the directory. That said, with a bit of researches I found out how to do this and I wrote a test code.

public class StringLocalizer {

    private final List<URL> files;

    public StringLocalizer(final String stringsDirectory) throws URISyntaxException, IOException {

        ClassLoader loader = Thread.currentThread().getContextClassLoader();

        final BufferedReader br = new BufferedReader(new InputStreamReader(loader.getResourceAsStream(stringsDirectory), StandardCharsets.UTF_8));
        files = br.lines()
                .map(l -> stringsDirectory + "/" + l)
                .map(loader::getResource)
                .collect(Collectors.toList());

        // This line has the debug purpose of showing all the url contained in the list
        System.out.println(Arrays.toString(files.toArray(new URL[files.size()]))); 
    }

    public static void main(String[] args) {

        try {
            new StringLocalizer("test/testDirectory/");
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }

}

First I tried the code in my IDE (Eclipse) and I got this output:

[file:/C:/Users/*my_user_name*/neon/workspace/JavaStringLocalizer/bin/test/testDirectory//test.xml]

The code works well, this was my first thought, however when I tried to package the program inside a runnable JAR file I got an unexpected output:

[]

Why is the list empty even if the file is packaged in the JAR file?

(The correct output should be a representation of an array containing all the files in the given directory, like the first output)

EDIT: To understand better my situation I have this files:

>MyJar.jar
    >classFiles
    >test>testDirectory>test.xml // I want to get this file

NOTE: this directory will contain more files, so I don't want to access them statically but I want to dynamically read all the files

EDIT: Code to extract the files using the ZipFile :

public class StringLocalizer {

    private final List<ZipEntry> files = new ArrayList<ZipEntry>();

    public StringLocalizer(final String stringsDirectory) throws URISyntaxException, IOException {

        URL jarUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
        File jarFile = new File(jarUrl.toURI().getPath());

        ZipFile unzipper = new ZipFile(jarFile, ZipFile.OPEN_READ);

        ZipEntry dirEntry = unzipper.getEntry(stringsDirectory);

        Enumeration<? extends ZipEntry> entries = unzipper.entries();

        for(ZipEntry entry = entries.nextElement(); entries.hasMoreElements(); entry = entries.nextElement()) {

            if(entry.getName().startsWith(dirEntry.getName()) && !entry.getName().equals(dirEntry.getName())) {
                files.add(entry);
            }
            System.out.println(entry.getName());
        }

        System.out.println(Arrays.toString(files.toArray(new ZipEntry[files.size()])));

        unzipper.close();
    }

    public static void main(String[] args) {

        try {
            new StringLocalizer("test/testDirectory/");
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }

}

Why is the last entry ignored?

JAR 文件实际上是 ZIP 文件,因此只需将文件视为 ZIP,就像@Jamie建议的那样。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM