[英]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: 我有一个包含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();
// 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
//这不是问题:问题是
zis.read(buffer, 0, (int) size)
不读取文件中的所有数据,所以我得到一个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: 问题不在于从流读取数据它读取所有数据但我认为问题是编码这是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 2959607是String的长度
问题是在org.JSONArray中它无法解析大字符串,我用GSon替换它现在它工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.