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

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

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

Epub格式需要一个20字节的mimetype文件,必须以未压缩的形式放入zip中. java.util.zip.ZipEntry api提供了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);
    }

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

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

解决方法

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

这是我的拉链代码:

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");
        });
    }
}

输出:

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)

看起来像“zip:method”属性就是你想要的.

所以,如果你想改变方法,如果你有一个进入你的zip文件系统的路径,它看起来像你可以做(​​UNTESTED!):

Files.setAttribute(thePath,"zip:method",ZipEntry.DEFLATED);

相关文章

摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠...
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个...
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:...
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程...