- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一些现有代码可以创建 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 文档非常稀疏,尤其是如何创建属性。
我开始觉得 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 类是属性接口(interface)的最小实现。我可以发布它,但它只是一个键值对(名称,值)
此代码成功创建了 zipFile,但是当我使用 7zip 打开 zipFile 时,我看到 mimetype 文件以 DEFLATED (8) 的形式存储在 zip 中,而不是我需要的 STORED (0)。所以问题是,我如何正确编码该属性,以便它存储为 STORED。
最佳答案
这没有很好的文档记录,但 JDK 的 zip 文件系统提供程序支持名为 zip
的 FileAttributeView
。
这是我的 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");
});
}
}
输出:
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”属性就是您想要的。
所以,如果你想改变方法,如果你有一个 Path
到你的 zip 文件系统,看起来你可以做(未经测试!):
Files.setAttribute(thePath, "zip:method", ZipEntry.DEFLATED);
关于Java NIO Zip 文件系统相当于 java.util.zip.ZipEntry 中的 setMethod(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28239621/
在 Chapel 中以固定增量遍历一系列实数的最惯用方法是什么? C 等效代码为: for (x = 0.0; x start, "Stop must be greater than start");
在编写我的 VBA 宏时,我经常使用“GoTo”以便在不离开 Sub 的情况下跳转到宏的前一部分。现在我正在将我所有的宏转换为 Google Apps 脚本,我正试图找到“GoTo”的等效项。 Sub
作为一个(不幸)对 jQuery 的了解多于 raw javascript 的人,我现在正在学习是时候用原始 javascript 替换我的所有代码了。不,这不是必需的,但对我来说这是一种更简单的学习
当我运行 git help -a它向我显示了内部命令列表、我所有的别名和我所有的外部 git 命令(即我的路径中以 git- 开头的任何可执行文件)。我想要的是一个可以作为 git which 运行的
我正在使用的查询: SELECT COUNT(*), SUM(amount) AS amount, FROM_UNIXTIME(added, '%W (%e/%m)') AS dail
我有一堆我正在调试的脚本,都是嵌套的并且非常讨厌。 只是想知道我是否能够设置一些与 bash 的 -x 选项等效的环境变量。这将为我节省大量时间。 我已经寻找答案,但似乎它不存在 - 希望你们聪明的人
ObjC [MyObject doThisWithString:string?: [MyObject otherString]]; 我如何在 Swift 中执行此操作? extension MyObj
我目前正在运行 Sonar 来对我的代码进行静态分析。当我在分析java文件并想抑制某个警告时,我使用了@SuppressWarnings(nameOfTheWarningOnSonar)注解。我想知
我最近一直在研究 Elixir 和 Akka,这让我想到:Clojure 中的等价物是什么? 我发现了几篇关于代理与 Actor 的“消息吞吐量比较”帖子,但它们来自 8 年前 一个答案曾经是agen
我以前工作的地方,我们使用 Mercurial 进行版本控制。我有一份新工作,我们在那里使用 Subversion。我是 Subversion 的新手。 我发现自己想知道自从我在远程仓库上结帐以来 c
寻找一种等效的剪切和粘贴策略来复制 vim 的“cut til”。如果我真的知道它在 vim 中的名称,我敢肯定这是 googleable,但这是我要找的: 如果我有一个像这样的文本块: foo ba
我有一段 .NET 代码,我想将其移植到 64 位。这些代码基本上是一组对其他 C dll 的 P/Invoke 调用。 C dll 中的函数之一具有参数“size_t”。我应该在我的 P/Invok
开发 iPhone 应用程序的标准开发者平台是什么,例如相当于 Eclipse? 最佳答案 Xcode 是 iOS 开发的标准且唯一(由 Apple 支持)IDE。它也是必需的,因为如果您想要任何开发
我想将某些内容推送到 iPhone 的响应者链上。也就是说,我想将选择器发送到 UIResponder子类,如果它不响应所述选择器,则将其传递给其 nextResponder . 有什么想法吗? 最佳
我需要一个与 SQL 中的此查询等效的 Firebase 查询: select * from your_table where id in (123, 345, 679) 你会如何在 firebase
我有一个很好的解决方案: $.get('getdbstuff.php?type=meta,'.$var_id, function(data){ $(data).appendTo("head")
我正处于 Cassandra 应用程序数据建模的初始阶段。此应用程序具有现有的关系持久层,必须用 Cassandra 替换。 应用程序为用户使用一个名为login_log 的表,它提供所有应用程序中任
如标题所述,TensorFlow 是否存在与 numpy.all() 函数等效的函数来检查 bool 张量中的所有值是否为 True?实现此类检查的最佳方法是什么? 最佳答案 使用tf.reduce_
在 Stata 中,如果我有以下变量:var1、var2、var3、var4、var5 和 var6,我可以使用命令 var* 选择所有它们。 R 有类似的功能吗? 最佳答案 “dplyr”包中的se
我正处于 Cassandra 应用程序数据建模的初始阶段。此应用程序具有现有的关系持久层,必须用 Cassandra 替换。 应用程序为用户使用一个名为login_log 的表,它提供所有应用程序中任
我是一名优秀的程序员,十分优秀!