Search code examples
javaandroidzipinputstream

Android ZipInputStream ZipEntry does not read to end of file


I have a zipped flie containing a json file and some base64 decoded images, I use the following code to read the data from it:

    JSONArray model = new JSONArray();
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(inputStream));
    ZipEntry zipEntry;
    while ((zipEntry = zis.getNextEntry()) != null) {
        if (zipEntry.getName().equals("model.json")) {
            long size = (int) zipEntry.getSize();
            if (size < 0) {
                size = 0xffffffffl + size;
            }
            byte[] buffer = new byte[(int) size];
            int read;
            while ((read = zis.read(buffer, 0, (int) size)) > 0) {
                Log.i("model", new String(buffer, "UTF-8"));
                model = new JSONArray(new String(buffer, "UTF-8"));
            }
        } else {
            long size = (int) zipEntry.getSize();
            if (size < 0) {
                size = 0xffffffffl + size;
            }
            byte[] buffer = new byte[(int) size];
            int read;
            while ((read = zis.read(buffer, 0, (int) size)) > 0) {
                Log.i("bitmap", new String(buffer, "UTF-8"));
                byte[] decodedMap = Base64.decode(new String(buffer, "UTF-8"), Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(decodedMap, 0, decodedMap.length);
                Map map = new Map(zipEntry.getName().substring(0, zipEntry.getName().indexOf(".")), bitmap, id, level);
                SharedData.maps.add(map);
            }
        }
        zis.closeEntry();
    }
    zis.close();

// This is not the problem: the problem is that the zis.read(buffer, 0, (int) size) does not read all data from the file so I get a JSONException

Edit:

The problem is not in the reading data from stream it reads all data but I think the problem is encoding this is the JSONException:

09-19 14:30:23.216: W/System.err(7524): org.json.JSONException: Unterminated array at character 2959607 of 

the 2959607 is the length of the String


Solution

  • the problem was in org.JSONArray it could not parse large string, I replaced it with GSon now it works fine