Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Faster way to read/write a ZipInputStream?

I am currently writing an application that reads a zip file in my assets folder which contains a bunch of images. I am using the ZipInputStream API to read the contents and then writing each file to my: Environment.getExternalStorageDirectory() directory. I have everything working but the first time the application is run writing the images to the storage directory is INCREDIBLY slow. It takes about about 5 minutes to write my images to disc. My code looks like this:

ZipEntry ze = null;
ZipInputStream zin = new ZipInputStream(getAssets().open("myFile.zip"));
String location = getExternalStorageDirectory().getAbsolutePath() + "/test/images/";

    //Loop through the zip file
    while ((ze = zin.getNextEntry()) != null) {
         File f = new File(location + ze.getName());
          //Doesn't exist? Create to avoid FileNotFoundException
          if(f.exists()) {
             f.createNewFile();
          }
          FileOutputStream fout = new FileOutputStream(f);
          //Read contents and then write to file
          for (c = zin.read(); c != -1; c = zin.read()) {
             fout.write(c);
          }             
    }
    fout.close();
    zin.close();

The process of reading the contents of the particular entry and then writing to it is VERY slow. I am assuming it is more to do with reading than writing. I've read that you can use a byte[] array buffer to speed up the process but this does not seem to work! I tried this but it only read part of the file...

    FileOutputStream fout = new FileOutputStream(f);
    byte[] buffer = new byte[(int)ze.getSize()];
     //Read contents and then write to file
      for (c = zin.read(buffer); c != -1; c = zin.read(buffer)) {
            fout.write(c);
      }             
  }

When I do that I only get about 600-800 bytes written. Is there a way to speed this up?? Have I implemented the buffer array incorrectly??

like image 680
Jack Avatar asked Jul 27 '11 22:07

Jack


1 Answers

I found a much better solution which implements the BuffererdOutputStream API. My solution looks like this:

   byte[] buffer = new byte[2048];

   FileOutputStream fout = new FileOutputStream(f);
   BufferedOutputStream bos = new BufferedOutputStream(fout, buffer.length);

   int size;

    while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        //Close up shop..
        bos.flush();
        bos.close();

        fout.flush();
        fout.close();
        zin.closeEntry();

I managed to increase my load time from anywhere from an average of about 5 minutes to about 5 (depending on how many images are in the package). Hope this helps!

like image 91
Jack Avatar answered Nov 11 '22 10:11

Jack