我生成一個包含文本和圖像文件組合的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