I am trying to stream files from a zip file using ZipEntry class in android, however I am not getting the files in alphabetical order.
Here is my code:
InputStream is = context.getResources().openRawResource(R.raw.file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
try {
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = zis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
String filename = ze.getName();
byte[] bytes = baos.toByteArray();
int value = progress++;
task.doProgress(value);
Log.e(" -- zip process ---", "Filename: " + filename.toString());
// do something with 'filename' and 'bytes'...
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Database Install", "Error: " + e.toString());
e.printStackTrace();
} finally {
try {
zis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am not su开发者_如何学JAVAre what is wrong with the code.
ZipInputStream
reads entries sequentially so they can't be in alphabetical order in general. If you want them to be in alphabetical order you should read them first using ZipFile.entries()
, sort as you like and access entries using ZipFile.getEntry()
.
精彩评论