JarEntry.getSize() is returning -1 when the jar files is opened as InputStream from URL

Posted by Reshma Donthireddy on Stack Overflow See other posts from Stack Overflow or by Reshma Donthireddy
Published on 2012-01-18T11:59:52Z Indexed on 2012/06/24 9:16 UTC
Read the original article Hit count: 199

Filed under:
|
|

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());
        }

© Stack Overflow or respective owner

Related posts about java

Related posts about jar