Android ZipInputStream ZipEntry不读取到文件末尾

时间:2016-09-19 10:40:54

标签: java android zipinputstream

我有一个包含json文件和一些base64解码图像的压缩文件,我使用以下代码从中读取数据:

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

//这不是问题所在: 问题是zis.read(buffer, 0, (int) size)没有读取文件中的所有数据,因此我得到JSONException

编辑:

问题不在于从流读取数据中读取所有数据但我认为问题是编码这是JSONException:

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

2959607是String

的长度

1 个答案:

答案 0 :(得分:0)

问题出现在org.JSONArray中它无法解析大字符串,我用GSon替换它现在它工作正常