Java NIO ZipFileSystem:“未找到zip END标头"在创建文件系统时 [英] Java NIO ZipFileSystem: "zip END header not found" while creating file system

查看:714
本文介绍了Java NIO ZipFileSystem:“未找到zip END标头"在创建文件系统时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问这个问题是因为谷歌搜索此错误仅使我在编写zip文件时遇到了麻烦,而我只是试图读取它.
我有一个单元测试,正在尝试测试以下生产代码:

I'm asking this here because googling this error only gives me hits on writing a zip file, while I'm only trying to read it.
I have a unit test where I'm trying to test the following production code:

Map<String, String> zipProps = new HashMap<>();
URI zipUri = URI.create("jar:file:" + itemZipPath.toString());
try (FileSystem zipfiles = FileSystems.newFileSystem(zipUri, zipProps)) {
   // do stuff...
} catch (IOException e) {
   // log an error
}

但是,在包含try的行上,此操作失败:

However this fails on the line containing the try:

java.util.zip.ZipError: zip END header not found
at com.sun.nio.zipfs.ZipFileSystem.zerror(ZipFileSystem.java:1605)
at com.sun.nio.zipfs.ZipFileSystem.findEND(ZipFileSystem.java:1021)
at com.sun.nio.zipfs.ZipFileSystem.initCEN(ZipFileSystem.java:1030)
at com.sun.nio.zipfs.ZipFileSystem.<init>(ZipFileSystem.java:130)
at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:117)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:326)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)
at com.company.PageCommandHandler$ProvisioningSteps.getItemModel(PageCommandHandler.java:105)

我尝试使用OSX的zip实用工具和jar cvf创建zipfile,但是两者均失败(但是file <filename>的输出略有不同). 我能找到的所有关于此错误的信息都与使用Java NIO创建zipfile有关,但是正如您所看到的,我仅在进行读取(目前验证ZIP中是否存在某个文件). 对这里出什么问题有任何想法吗?

I've tried creating the zipfile using both OSX's zip utility and using jar cvf but both fail (the output of file <filename> differs slightly however). All the information about this error I can find relates to creating a zipfile using Java NIO, but as you can see I'm only doing a read (verifying the presence of a certain file inside the ZIP for now). Any thoughts on what is going wrong here?

推荐答案

我遇到了完全相同的错误. Java8.原因是我错误地创建了一个空文件,而不仅仅是对象:

I've met exactly the same error. Java 8. The reason was, by mistake I created an empty file, not only object:

File zipFile = new File(path);
zipFile.createNewFile();

然后将此路径传递到

URI uri = URI.create("jar:file:" + zipFile.getAbsolutePath());

要解决此问题,我没有创建文件本身,只是创建了一个File对象:

To fix it, I did not create file itself, only created a File object:

File zipFile = new File(path);
URI uri = URI.create("jar:file:" + zipFile.getAbsolutePath());

为了使其更可靠,如果存在该文件,我会先删除它:

To make it more reliable I would offer to delete file first if it exists:

File zipFile = new File(path);
//Caused by: java.util.zip.ZipError: zip END header not found
if (zipFile.exists()){
    try {
        Files.delete(Paths.get(zipFile.getAbsolutePath()));
    } catch (IOException e) {
        throw new IllegalStateException(
                "Could not delete file.", e);
    }
}
...
URI uri = URI.create("jar:file:" + zipFile.getAbsolutePath());

在某些情况下,删除文件的解决方案可能是不可接受的.例如,如果您将多个条目添加到同一个zip文件中.但是在我的用例中就可以了.

Probably in some case the solution with deletion of the file is not acceptable. For instance, if you add to the same zip file several entries. But in my use case it is OK.

这篇关于Java NIO ZipFileSystem:“未找到zip END标头"在创建文件系统时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆