获取jar内部文件夹内的文件

时间:2013-11-05 16:16:10

标签: java jar inputstream java-web-start embedded-resource

我正在创建一个Webstart应用程序,我需要检索放在jar中文件夹内的一些文件。 我需要将包含内容的整个文件夹复制到本地驱动器。我该怎么做? 似乎Webstart只批准输入流来从jar读取。所以文件夹必须以某种方式读取和输入流。

谢谢!

2 个答案:

答案 0 :(得分:2)

正如info 中所述,您可以使用getResource()为每个文件获取URL。这个简单的example缓存getImage()中的图片;这个更精细的example构建了RCImage中的图像集。两者都从JAR文件中名为images的文件夹中读取。

答案 1 :(得分:1)

使用zipentry Zipinputstream和FileOutPutStream解决它。

注意:只有在Eclipse JVM之外运行它时才有效(例如在运行Java Webstart的情况下)。

CodeSource src = MAINCLASS.class.getProtectionDomain().getCodeSource();
                if (src != null) {
                    URL jar = src.getLocation();
                    ZipInputStream zip = new ZipInputStream(jar.openStream());
                    ZipEntry e = null;
                    while ((e = zip.getNextEntry()) != null) {
                        System.out.println("Entry name: " + e.getName());
                        if (e.getName().endsWith(".db")) {
                            FileOutputStream outPutFile = new FileOutputStream(folderDest + File.separator
                                    + e.getName().substring(e.getName().indexOf("INSERT-CHAR-WHERE-TO-BEGIN-RETRIVE-FILE -NAME")));
                            int data = 0;
                            while ((data = zip.read()) != -1) {
                                outPutFile.write(data);
                            }
                            outPutFile.close();
                        }
                    }
                    zip.close();
                } else {
                    Log.error("Can't retrive running jar.");
                }