读jar包里面多个文件

程序有一堆文件要读,并且要打jar包。让其他程序调用
this.class.getClassLoader().getResource只能读到文件名
this.class.getClassLoader().getResourceAsStream也只能读到单独一个文件
于是想到一个办法,把这堆文件打zip包。通过getResourceAsStream读到zip,每个文件已文件名为key,文件内容为value放入内存。
只限于较小的文件

        ZipInputStream zis = new ZipInputStream(Round.class.getClassLoader().getResourceAsStream("round_2/round.zip"));

        ZipEntry entry = null;
        Map<String, String> map = new HashMap<String, String>();

        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory()) {
            } else {
                long size = entry.getSize();
                if (size > 0) {
                    map.put(entry.getName().replace("round_2/", ""), new String(IOUtils.toByteArray(zis)));
                }
            }
        }

        return map;

猜你喜欢

转载自blog.csdn.net/jshazhang/article/details/79208305