【问题标题】:Get retrive files inside folder inside jar获取jar内文件夹内的文件
【发布时间】:2013-11-16 14:24:25
【问题描述】:

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

谢谢!

【问题讨论】:

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


    【解决方案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.");
                    }
    

    【讨论】:

      【解决方案2】:

      正如info 中针对 所讨论的,您可以使用getResource() 为每个文件获取URL。这个简单的example 将图像缓存在getImage() 中;这个更精细的exampleRCImage 中构造图像集。两者都从 JAR 文件中名为 images 的文件夹中读取。

      【讨论】:

      • 谢谢!我昨天使用 zipEntry 解决了它,在整个 jar 中搜索我的文件并检索它们。
      猜你喜欢
      • 2017-04-08
      • 2014-12-31
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2015-01-25
      • 2017-01-26
      相关资源
      最近更新 更多