Java – jarentry. Java when the jar file is opened from URL to InputStream Getsize() returns – 1
I tried to read the file from jarinputstream, and the size of the file returned - 1 I am accessing the jar file from the URL as 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());
}
Solution
This is a recorded act Javadoc says getsize()
Obviously, this is the case where the actual size of the compressed data is unknown Your application only needs to deal with this problem
follow-up
You commented on another answer:
If you do not have this size, you can still read the file and use ByteArrayOutputStream to buffer it, then call toByteArray () to extract the buffer byte to byte []. In fact, since (according to Tom hawtin's comments), the size of the report may be incorrect, you should do so anyway. Just treat the size of the report as a hint... And use it as the initial capacity of bytearrayoutputstream
(by the way, the code in your question may seem to work in this way... Judging from line 2 to the last line. The problem is that the rest of the code doesn't use a Bao object.)
