Java NIO Zip Filesystem相当于java.util.zip.ZipEntry中的setMethod()

cas*_*age 7 java compression zip nio

我有一些现有的代码来创建Epub 2格式的zip文件,它可以正常工作.

在尝试更新我的代码以支持Epub 3格式时,我想我会尝试Java NIO Zip文件系统而不是java.util.zip.ZipFile.除了一件小物品外,我几乎就在那里.

mimetypeEpub格式需要一个20字节的文件,必须以未压缩的形式放入zip中.该java.util.zip.ZipEntryAPI提供了setMethod(ZipEntry.STORED)实现这一目标.

我在Java NIO FileSystem API文档中找不到任何对此的引用.有相同的ZipEntry.setMethod()吗?

编辑1

好的,所以我看到如何显示属性,并感谢你的例子,但我找不到任何关于如何创建属性的文档,如(zip:method,0),甚至在Oracle自己的oracle上也是如此.在我看来,Java 7中的NIO增强功能只有大约20%的记录.api doc属性非常稀疏,特别是如何创建属性.

我开始得到的感觉是NIO Zip文件系统可能不是一个改进java.util.zip,并且需要更多的代码来实现相同的结果.

编辑2

我尝试了以下方法:

String contents = "application/epub+zip"; /* contents of mimetype file */
Map<String, String> map = new HashMap<>();
map.put("create", "true");

Path zipPath = Paths.get("zipfstest.zip");
Files.deleteIfExists(zipPath);

URI fileUri = zipPath.toUri(); // here
URI zipUri = new URI("jar:" + fileUri.getScheme(), fileUri.getPath(), null);

try (FileSystem zipfs = FileSystems.newFileSystem(zipUri, map)) {
    Path pathInZip = zipfs.getPath("mimetype");
    Files.createFile(pathInZip, new ZipFileAttribute<Integer>("zip:method", 0));
    byte[] bytes = contents.getBytes();
    Files.write(pathInZip, bytes, StandardOpenOption.WRITE);
    }
Run Code Online (Sandbox Code Playgroud)

ZipFileAttribute类是属性接口的最小实现.我可以发布它,但它只是一个键值对(名称,值)

这段代码成功创建了zipFile,但是当我用7zip打开zipFile时,我看到mimetype文件作为DEFLATED(8)存储在zip中,而不是我需要的STORED(0).所以问题是,我如何正确编码属性,以便存储为STORED.

fge*_*fge 2

这没有很好的记录,但 JDK 的 zip 文件系统提供者支持FileAttributeView名为zip.

这是我的 zip 中的代码:

public static void main(final String... args)
    throws IOException
{
    final Path zip = Paths.get("/home/fge/t.zip");
    final Map<String, ?> env = Collections.singletonMap("readonly", "true");
    final URI uri = URI.create("jar:" + zip.toUri());

    try (
        final FileSystem fs = FileSystems.newFileSystem(uri, env);
    ) {
        final Path slash = fs.getPath("/");
        Files.readAttributes(slash, "zip:*").forEach( (key, val) -> {
            System.out.println("Attribute name: " + key);
            System.out.printf("Value: %s (class: %s)\n", val,
                val != null ? val.getClass(): "N/A");
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Attribute name: size
Value: 0 (class: class java.lang.Long)
Attribute name: creationTime
Value: null (class: N/A)
Attribute name: lastAccessTime
Value: null (class: N/A)
Attribute name: lastModifiedTime
Value: 1969-12-31T23:59:59.999Z (class: class java.nio.file.attribute.FileTime)
Attribute name: isDirectory
Value: true (class: class java.lang.Boolean)
Attribute name: isRegularFile
Value: false (class: class java.lang.Boolean)
Attribute name: isSymbolicLink
Value: false (class: class java.lang.Boolean)
Attribute name: isOther
Value: false (class: class java.lang.Boolean)
Attribute name: fileKey
Value: null (class: N/A)
Attribute name: compressedSize
Value: 0 (class: class java.lang.Long)
Attribute name: crc
Value: 0 (class: class java.lang.Long)
Attribute name: method
Value: 0 (class: class java.lang.Integer)
Run Code Online (Sandbox Code Playgroud)

看起来“zip:method”属性就是您想要的

所以,如果你想改变方法,如果你有一个Pathzip 文件系统,看起来你可以这样做(未经测试!):

Files.setAttribute(thePath, "zip:method", ZipEntry.DEFLATED);
Run Code Online (Sandbox Code Playgroud)