为什么我的 zip 文件中的图像已损坏?
我正在用java生成一个包含文本和图像文件混合的zip文件,这在一台计算机上工作正常,但在另一台计算机上我的图像文件已损坏(相同的java版本和操作系统);生成的文件大小相同,但图像不会在图像编辑器/查看器中打开,文本文件没问题。
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();
}
知道图像损坏的原因吗?
I'm generating a zip file in java containing a mix of text and image files, this works fine on one computer but on another my image files are corrupt (same java version and OS); the resulting file sizes are the same but the image will not open in an image editor/viewer, text files are fine.
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();
}
Any ideas why the images are getting corrupted?
Here's a visual binary comparison between a corrupt image and the original.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您用来提取 ZIP 文件的工具似乎将您的图像视为 ASCII 文本,将任何大于或等于 0x80 的值替换为未知字符,并将其替换为问号 (0x3F)。
It seems the tool you use to extract your ZIP file treats your image as ASCII text, replacing any value higher than or equal to 0x80 as an unknown character, replacing it with a questionmark (0x3F).
这取决于 IOUtils.toByteArray(is) 的作用。
That would depend on what
IOUtils.toByteArray(is)
does.