我在这里问这个问题是因为谷歌搜索这个错误只会让我在编写 zip 文件时得到点击,而我只是想阅读它。
我有一个单元测试,我试图测试以下生产代码:
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 的行上失败:
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 实用程序创建 zip 文件并使用jar cvf
但都失败了(输出file <filename>
但略有不同)。
我能找到的有关此错误的所有信息都与使用 Java NIO 创建 zip 文件有关,但正如您所看到的,我只是进行读取(暂时验证 ZIP 中是否存在某个文件)。
对这里出了什么问题有什么想法吗?
我遇到了完全相同的错误。 Java 8。原因是,我错误地创建了一个空文件,而不仅仅是对象:
File zipFile = new File(path);
zipFile.createNewFile();
然后将这条路径传递给
URI uri = URI.create("jar:file:" + zipFile.getAbsolutePath());
为了解决这个问题,我没有创建文件本身,只创建了一个 File 对象:
File zipFile = new File(path);
URI uri = URI.create("jar:file:" + zipFile.getAbsolutePath());
为了使其更可靠,我建议先删除文件(如果存在):
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 文件添加多个条目。但在我的用例中,这是可以的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)