Dee*_*hry 5 java zip applet jar signed-applet
我在使用ZipInputStream读取并从Applet遍历ZipEntry的zip文件中有一堆图像文件。
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);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我将这些图像放入签名的jar中时,“ ze.getSize()”以-1的形式出现,并且图像文件被错误地读取。
有人可以在这方面帮助我。
是的,-1 意味着大小未知 - 目前尚不清楚为什么要将其放入地图中,然后再次将其取出。
基本上,如果大小未知,您应该继续读取缓冲区,直到read返回 -1。一种简单的方法是创建一个ByteArrayOutputStream,并不断从 复制到ZipEntry该文件 - 然后一旦您完成阅读,只需从 中获取字节数组即可ByteArrayOutputStream。它将处理任何必要的大小调整。
| 归档时间: |
|
| 查看次数: |
6210 次 |
| 最近记录: |