当jar文件从URL打开为InputStream时,JarEntry.getSize()返回-1 [英] JarEntry.getSize() is returning -1 when the jar files is opened as InputStream from URL
本文介绍了当jar文件从URL打开为InputStream时,JarEntry.getSize()返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从JarInputStream读取文件,文件大小返回-1。我从URL访问Jar文件作为inputStream。
I am trying to read file from the JarInputStream and the size of file is returning -1. I am accessing Jar file from the URL as a inputStream.
URLConnection con = url.openConnection();
JarInputStream jis = new JarInputStream(con.getInputStream());
JarEntry je = null;
while ((je = jis.getNextJarEntry()) != null) {
htSizes.put(je.getName(), new Integer((int) je.getSize()));
if (je.isDirectory()) {
continue;
}
int size = (int) je.getSize();
// -1 means unknown size.
if (size == -1) {
size = ((Integer) htSizes.get(je.getName())).intValue();
}
byte[] b = new byte[(int) size];
int rb = 0;
int chunk = 0;
while (((int) size - rb) > 0) {
chunk = jis.read(b, rb, (int) size - rb);
if (chunk == -1) {
break;
}
rb += chunk;
}
// add to internal resource hashtable
htJarContents.put(je.getName(), baos.toByteArray());
}
推荐答案
byte[] classbytes = null;
JarInputStream jis = new JarInputStream(is);
JarEntry je = null;
String jename = null;
while((je = jis.getNextJarEntry()) != null){
jename = je.getName();
if(je.getSize() != -1){
classbytes = new byte[(int)je.getSize()];
int len = (int) je.getSize();
int offset = 0;
while (offset != len)
offset += jis.read(classbytes, offset, len - offset);
classes.put(jename, classbytes);
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(true){
int qwe = jis.read();
if(qwe == -1) break;
baos.write(qwe);
}
classbytes = baos.toByteArray();
classes.put(jename, classbytes);
}
}
这篇关于当jar文件从URL打开为InputStream时,JarEntry.getSize()返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文