簡體   English   中英

Java - 讀取 jar 中的文件

[英]Java - Reading files inside jar

我知道有很多方法可以做到這一點,但是我在這個任務中遇到了一些問題,我無法用我已經找到的解決方案來解決。

首先,我不想讀取 jar 中的特定文件:我想讀取給定目錄路徑的 jar目錄中包含的所有文件。 也就是說,通過一些研究,我發現了如何做到這一點,並編寫了一個測試代碼。

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();
        }
    }

}

首先,我在我的 IDE (Eclipse) 中嘗試了代碼,我得到了這個輸出:

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

代碼運行良好,這是我的第一個想法,但是當我嘗試將程序打包到一個可運行的 JAR 文件中時,我得到了一個意外的輸出:

[]

為什么即使文件打包在JAR文件中,列表也是空的?

(正確的輸出應該是包含給定目錄中所有文件的數組的表示,如第一個輸出)

編輯:為了更好地了解我的情況,我有以下文件:

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

注意:此目錄將包含更多文件,因此我不想靜態訪問它們,但我想動態讀取所有文件

編輯:使用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();
        }
    }

}

為什么最后一個條目被忽略?

JAR 文件實際上是 ZIP 文件,因此只需將文件視為 ZIP,就像@Jamie建議的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM