此错误存在于最新的 JDK 1.7 和 1.8 版本(7u72、8u25)中。必需:杰克逊数据绑定 2.5.0。在 Linux x86_64(准确地说是 Ubuntu 14.10)上测试。
代码:
public static void main(final String... args)
throws IOException
{
final Map<String, String> map
= Collections.singletonMap("create", "true");
final Path zipfile = Paths.get("/tmp/foo.zip");
Files.deleteIfExists(zipfile);
final URI uri = URI.create("jar:" + zipfile.toUri());
final ObjectMapper mapper = new ObjectMapper();
try (
final FileSystem zipfs = FileSystems.newFileSystem(uri, map);
final OutputStream out
= Files.newOutputStream(zipfs.getPath("/t.json"));
) {
mapper.writeValue(out, "hello");
}
}
这会产生一个无效的 zip 文件:
$ unzip /tmp/foo.zip
Archive: /tmp/foo.zip
replace t.json? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: t.json
error: invalid compressed data to inflate
我最初在杰克逊的问题跟踪器上打开了这个错误,即使它真的不是这里的罪魁祸首,并且找到了解决它的解决方案:JsonGenerator.Feature.AUTO_CLOSE_SOURCE
在ObjectMapper
. 默认情况下启用的此选项告诉映射器关闭流。
虽然我想向 Oracle 打开错误,但我首先希望能够编写一个 SSCCE,但我不能。我试图关闭流两次(因为它在示例中关闭了两次),而不是使用 try-with-resources 语句等......无济于事。
你能想出一个SSCCE来解决这个问题吗?