【发布时间】:2023-03-13 00:27:01
【问题描述】:
我在一个 zip 文件中有一堆图像文件,我正在使用 ZipInputStream 读取这些图像文件,并从 Applet 中迭代 ZipEntry。
ZipInputStream zis = new ZipInputStream(in);
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
if (ze.isDirectory()) {
continue;
}
int size = (int) ze.getSize();
// -1 means unknown size.
if (size == -1) {
size = ((Integer) htSizes.get(ze.getName())).intValue();
}
byte[] b = new byte[(int) size];
int rb = 0;
int chunk = 0;
while (((int) size - rb) > 0) {
chunk = zis.read(b, rb, (int) size - rb);
if (chunk == -1) {
break;
}
rb += chunk;
}
// add to internal resource hashtable
htJarContents.put(ze.getName(), b);
}
但是,当我将这些图像放入已签名的 jar 中时,“ze.getSize()”将变为 -1,并且图像文件读取不正确。
有人可以在这方面帮助我吗?
【问题讨论】:
-
您真的不应该将大小截断为 int。另外,请参阅下面@JonSkeet 的回答。
-
为什么不直接获取压缩图像的输入流并传递给
ImageIO.read(InputStream)?
标签: java jar applet zip signed-applet