将图像文件添加到现有zip文件时出现Java异常

时间:2015-07-16 07:15:15

标签: java filesystems

我在使用FileSytem方法将图像文件添加到zip文件时遇到问题。例外情况如下:

Exception in thread "main" 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(Unknown Source)
    at java.nio.file.FileSystems.newFileSystem(Unknown Source)
    at Tests.main(Tests.java:71)

我用来做的代码是:

Map<String, String> zipproperties = new HashMap<>();
zipproperties.put("create", "false");
URI urizip = URI.create("jar:"+zipfile.toURI());
try(FileSystem zipfs = FileSystems.newFileSystem(urizip, zipproperties))
{
    Path entry = Paths.get(filetoadd.getAbsolutePath());
    Path Pathinzipfile = zipfs.getPath(zippath+filetoadd.getName());
    Files.move(Pathinzipfile, entry);
}

我用来创建zip文件的方法是:

ZipOutputStream dest = new ZipOutputStream(new FileOutputStream(zippath));

提前感谢您的帮助!

0 个答案:

没有答案