【问题标题】:Android ZipInputStream ZipEntry does not read to end of fileAndroid ZipInputStream ZipEntry 不读取到文件末尾
【发布时间】:2016-09-19 10:40:54
【问题描述】:

我有一个包含 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 是字符串的长度

【问题讨论】:

    标签: java android zipinputstream


    【解决方案1】:

    问题出在 org.JSONArray 中,它无法解析大字符串,我将其替换为 GSon 现在它可以正常工作了

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2011-09-21
      相关资源
      最近更新 更多