1

I am seeking for most efficient way (in terms of speed) to retrieve some file out of the middle of a ZIP file.

e.g. I have ZIP file, which includes 700 folders (tagged 1 to 700). Each folder equals picture and mp3 file. There is special folder called Info, which contains XML file. Problem is, I need to iterate through this ZIP file to find XML file and then I am displaying images from desired folders. I am using ZipFile approach (thus I am iterating through whole ZIP file, even if I want folder 666, I need to go through 665 items in ZIP file) -> selecting from ZIP file is extremely slow.

I would like to ask you, If you have faced similar issue, how have you solved this? Is there any approach in Java, which turns my ZIP file into virtual folder to browse it much more quicker? Is there any external library, which is the most efficient in terms of time?

Source Code snippet:

try {
  FileInputStream fin = new FileInputStream(
      "sdcard/external_sd/mtp_data/poi_data/data.zip");
  ZipInputStream zin = new ZipInputStream(fin);
  ZipEntry ze = null;
  while ((ze = zin.getNextEntry()) != null) {
    // Log.d("ZE", ze.getName());
    if (ze.getName().startsWith("body/665/")) {
      // Log.d("FILE F", "soubor: "+ze.getName());
      if (ze.getName().endsWith(".jpg")
          || ze.getName().endsWith(".JPG")) {
        Log.d("OBR", "picture: " + ze.getName());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int count;

        while ((count = zin.read(buffer)) != -1) {
          baos.write(buffer, 0, count);
        }
        byte[] bytes = baos.toByteArray();

        bmp = BitmapFactory.decodeByteArray(bytes, 0,
            bytes.length);
        photoField.add(bmp);
        i++;
      }
    }
  }
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
Waypoint
  • 17,283
  • 39
  • 116
  • 170

1 Answers1

7

The ZipFile.getEntry() and ZipFile.getInputStream() methods can be used to access a specific file in a ZIP archive. For example:

ZipFile file = ...
ZipEntry entry = file.getEntry("folder1/picture.jpg");
InputStream in = file.getInputStream(entry);
Michael
  • 34,873
  • 17
  • 75
  • 109
  • Thanks, I have a problem now, is there any way how to store more them 65535 files inside one archive? – Waypoint Jun 03 '12 at 07:32
  • @Waypoint That appears to be a limitation in the ZIP file format. This SO question might help you: http://stackoverflow.com/questions/6738773/zip-files-with-java-is-there-a-limit – Michael Jun 03 '12 at 13:55