ZipInputStream 和 JarInputStream 的 ZipEntry 大小不一致
我在 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,并且图像文件读取不正确。
有人可以在这方面帮助我吗?
I have bunch of image files in a zip file which I am reading using ZipInputStream and Iterating over ZipEntry from an Applet.
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);
}
However when I put these images into a signed jar "ze.getSize()
" is coming as -1, and image file is getting read incorrectly.
Can someone help me in this regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,-1 意味着大小未知 - 目前还不清楚为什么要将其放入地图中,然后再次将其取出。
基本上,如果大小未知,您应该继续读取缓冲区,直到读取代码> 返回-1。一种简单的方法是创建一个 ByteArrayOutputStream ,并不断从 ZipEntry 复制到该流 - 然后一旦读完,只需从 ZipEntry 中获取字节数组即可ByteArrayOutputStream。它将处理任何必要的大小调整。
Yes, -1 means the size is unknown - it's not clear why you're putting it into a map and then fetching it out again though..
Basically, if the size is unknown you should keep reading buffers until
read
returns -1. One simply way is to create aByteArrayOutputStream
, and keep copying from theZipEntry
to that - then once you've finished reading, just get the byte array from theByteArrayOutputStream
. It will handle any resizing necessary.这个问题类似于 当 jar 文件从 URL 作为 InputStream 打开时,JarEntry.getSize() 返回 -1 并且答案是相同的。
javadoc 特别指出,如果无法确定大小,该方法将返回
-1
。而且(根据 Tom Hawtin 的说法)返回的大小也有可能是非负的……而且是不正确的。最重要的是,您需要将报告的大小仅视为提示,并将整个流读入可扩展的字节缓冲区(例如 ByteArrayOutputStream)。
This question is similar to JarEntry.getSize() is returning -1 when the jar files is opened as InputStream from URL and the answer is the same.
The javadoc specifically states that that method returns
-1
if the size cannot be determined. And (according to Tom Hawtin) it is also possible that the size returned is non-negative ... and incorrect.The bottom line is that you need to treat the reported size as a hint only, and read the entire stream into and expandable byte buffer (e.g. a ByteArrayOutputStream).
getSize() 返回条目数据的未压缩大小,如果未知,则返回 -1。
因此,如果返回的大小为负数,请将其添加到 0xffffffffl 以获得正确的值。
示例:
参考:
ZipEntry.getSize() 返回负值
getSize() returns the uncompressed size of the entry’s data, or -1 when not known.
So if the size returned is negative, add it to 0xffffffffl to get the correct value.
Example:
Reference:
Negative value returned for ZipEntry.getSize()