中以编程方式读取

中以编程方式读取

本文介绍了有没有办法在 Java 中以编程方式读取 .jmod 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 7-zip 打开了一个 .jmod 文件,我可以看到内容.我试图以编程方式使用 ZipInputStream 读取它,但它不起作用:有人知道怎么做吗?

I opened a .jmod file with 7-zip and I can see the contents. I tried to read it with ZipInputStream programmatically but it doesn't work: does someone know how to do it?

推荐答案

JEP 261 中没有文档: Module System 关于 JMOD 文件使用的格式.据我所知,这不是疏忽,因为将格式作为实现细节意味着他们可以随时更改格式,无需通知.话虽如此,目前 JMOD 文件似乎以 ZIP 格式打包;这个其他答案引用了JEP 261中的以下内容:

There is no documentation in JEP 261: Module System regarding the format used by JMOD files. That's not an oversight, as far as I can tell, because leaving the format as an implementation detail means they can change the format, without notice, whenever they want. That being said, currently JMOD files appear to be packaged in the ZIP format; this other answer quotes the following from JEP 261:

JMOD 文件的最终格式是一个未解决的问题,但目前它基于 ZIP 文件.

但是,我在 JEP 261 中的任何地方都找不到该引用.它看起来来自旧版本的规范——至少,我在 JDK-8061972(与 JEP 相关的问题).

However, I can't find that quote anywhere in JEP 261. It looks to be from an older version of the specification—at least, I found similar wording in the history of JDK-8061972 (the issue associated with the JEP).

这意味着您应该——暂时——能够通过使用任何允许读取 ZIP 文件的 API 来读取 JMOD 文件.例如,您可以使用以下方法之一:

What this means is you should—for the time being—be able to read a JMOD file by using any of the APIs which allow reading ZIP files. For instance, you could use one of the following:

  1. java.util.zip API:

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;

public class Main {

  public static void main(String[] args) throws IOException {
    var jmodFile = new File(args[0]).getAbsoluteFile();
    System.out.println("Listing entries in JMOD file: " + jmodFile);

    try (var zipFile = new ZipFile(jmodFile)) {
      for (var entries = zipFile.entries(); entries.hasMoreElements(); ) {
        System.out.println(entries.nextElement());
      }
    }
  }
}

ZIP FileSystemProvider API:

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {

  public static void main(String[] args) throws IOException {
    var jmodFile = Path.of(args[0]).toAbsolutePath().normalize();
    System.out.println("Listing entries in JMOD file: " + jmodFile);

    try (var fileSystem = FileSystems.newFileSystem(jmodFile)) {
      Files.walk(fileSystem.getRootDirectories().iterator().next())
          .forEachOrdered(System.out::println);
    }
  }
}

但是,重申一下:JMOD 文件使用的格式没有记录,可能会更改,恕不另行通知.

这篇关于有没有办法在 Java 中以编程方式读取 .jmod 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:18