【问题标题】:How to write the path of file in FileInputStream() where the file exits in a zip file then a jar file如何在 FileInputStream() 中写入文件的路径,其中文件存在于 zip 文件中,然后是 jar 文件
【发布时间】:2018-01-10 00:17:31
【问题描述】:

C:/Data.zip/text.jar/a.xml 是a.xml 的位置,所以当我像这样使用它时

FileInputStream fis = new FileInputStram("C:/Data.zip/text.jar/a.xml"); 

它给了我 FileNotFoundException,当我使用 FileInputStream("/a.xml") 时也会抛出相同的 FileNotFoundException。

【问题讨论】:

  • 您的问题不清楚。输入流旨在从源读取数据,而不是写入它们。你能澄清一下你想在这里实现什么吗?
  • 是的,正确的。我的意思是如何获取 FileInputStrem("fileLocation"); 的文件位置
  • 请注意,您不能简单地访问 存档中的文件,因为存档不是文件夹,它是遵循某种算法的单个文件描述了如何将多个文件写入一个文件。就像如果您有一个文本文件并将其他文件的所有内容写入其中,那么最后您也只有一个文件而不是文件夹。因此,为了访问内容,您需要再次使用解包存档的算法。 Java 库中有一些解决方案可以自动为您执行此操作,例如 @albert_nil 的答案中所见。
  • 所以底线是:对于您的操作系统(以及任何类型的常规FIle 方法或Path 构建),只需像zip(或jar)这样的存档看起来像一个普通的单个文件。如果不解释文件并使用解包算法,它就无法查看内部。这就是为什么你目前的方法行不通的原因(只要他们不添加一些自动完成所有这些事情的魔法)。

标签: java file iostream


【解决方案1】:

您需要在 FileInoutStream 顶部使用 JarInputStream 指向 jar,而不是 xml。然后你需要在 JarInoutStream 中找到那个 xml 文件并进行处理。

JarInputStream jarStream = new JarInputStream(new FileInputStream("your_jar_path"));
    JarEntry entry;
    while ((jarEntry = jarStream.getNextJarEntry()) != null) {
        if (jarEntry.getName().equals("xmlfile_youlookfor")) {
            doyourstuff
        }
     }
}

记得关闭流。

编辑:我现在注意到您说的是拉链内的罐子。但这并没有改变很多解决方案。只需首先使用 ZipFileStream 打开 zip 并以与上述代码在 jar 中使用 xml 类似的方式在其中找到 jar 文件。然后只需使用 zip 中找到的条目执行 JarInputStream。

Edit2:一些示例代码。还没有测试过,但它应该只包含轻微的拼写错误(如果有的话):

public void read() throws IOException {
    ZipInputStream zipStream = null;
    try {
        zipStream = new ZipInputStream(new FileInputStream("C:/Data.zip"));
        ZipEntry zipEntry;
        boolean zipEntryFound = false;
        while ((zipEntry = zipStream.getNextEntry()) != null) {
            if (zipEntry.getName().equals("text.jar")) {
                zipEntryFound=true;
                break;
            }
        }
        if (!zipEntryFound) {
            return;
        }
        JarInputStream jarStream = new JarInputStream(zipStream);
        JarEntry jarEntry;
        boolean jarEntryFound = false;
        while ((jarEntry = jarStream.getNextJarEntry()) != null) {
            if (jarEntry.getName().equals("a.xml")) {
                jarEntryFound = true;
                break;
            }
        }
        if (!jarEntryFound) {
            return;
        }
        // now your jarStream is positioned on the a.xml file entry, just read 
        // the bytes from the stream. you know how many bytes from the 
        // jarEntry info
    } catch (Exception e) {
        if (zipStream != null) {
            zipStream.close();
        }
    }
}

【讨论】:

    【解决方案2】:

    JRE 定期访问 JAR 中的文件,您可以通过 ClassLoader 使用该功能。但是,在 ZIP 中访问 JAR 中的文件超出了它的能力范围。可能有一个自定义框架可以做到这一点,但我还没有听说过。

    假设您知道 JAR 文件的位置,您展示的解决方案应该可以正常工作。如果 JAR 在类路径中,则

    this.getClass().getClassLoader().getResourceAsStream(String name)
    

    “名称”是文件的路径,从类路径的根开始。路径应以“/”开头,否则为相对路径。

    【讨论】:

    • 好的,史蒂夫。你能解释一下如何写一个文件的路径,其中文件只在一个罐子里。像 text.jar->a.xml.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多