Search code examples
javaandroidzipinputstream

The size of zipInputStream is always 512 bytes?


    private byte[] loadClassData(String className) {
    ZipInputStream in = null;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(jarPath);
        in = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            if (entry.getName().contains(".class")) {
                String outFileName = entry.getName()
                        .substring(0, entry.getName().lastIndexOf('.'))
                        .replace('/', '.');
                if (outFileName.equals(className)) {
                    if (entry.getSize() == -1) {
                        Log.e("loadClassData", "can't read the file!");
                        return null;
                    }
                    byte[] classData = new byte[(int) entry.getSize()];
                    in.read(classData);
                    return classData;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fis.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

In debug , I always get the size of "in" is 512 bytes so that I can't get the rest of my file,and I don't know why.Does ZipInputStream have size limit? Thank you!


Solution

  •  if (entry.getSize() == -1) {
    

    ZipEntry.getSize() Returns the uncompressed size of the entry data, or -1 if not known.. You need to remove this check.

    I always get the size of "in" is 512 bytes

    How are you checking this? ZipInputStream has no size properties. Whatever you are checking is unrelated.

    This seems to be a good canonical example of ZipInputStream usage.