我生成一个包含文本和图像文件组合的zip文件,这在一台计算机上正常工作,但在另一台计算机上,我的图像文件已损坏(相同的Java版本和操作系统);由此产生的文件大小是相同的,但图像不会在图像编辑器/查看器中打开,文本文件很好。为什么图像在我的zip文件中被损坏?
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos);
zos.setMethod(ZipOutputStream.DEFLATED);
addZipEntry(zos, "/forms/images/calendar.gif", "images/calendar.gif");
addZipEntry(zos, "/forms/templ/header.php", "templ/header.php");
zos.close();
private void addZipEntry(ZipOutputStream zos, String resourcePath, String entryName) throws IOException {
ClassLoader cl = getClass().getClassLoader();
InputStream is = cl.getResourceAsStream(resourcePath);
zos.putNextEntry(new ZipEntry(entryName));
zos.write(IOUtils.toByteArray(is));
zos.closeEntry();
}
任何想法为什么图像被损坏?
Here's a visual binary comparison between a corrupt image and the original.
我建议你创建一个更小的例子来说明问题(排除对ClassLoaders和东西的引用),也许还会上传造成麻烦的图像。 – aioobe
在图像上运行diff工具以确切了解数据的变化情况。 – Stijn
@Stijn我已经添加了一个视觉比较的链接,它并没有真正告诉我很多个人,但也许有人更开明可以看到发生了什么。 – Phill