2

I have placed two media folders into a single zip folder, and the total is 100k media files in the zip folder. I need to play a single file from particular folder of the zip folder. The problem is, first the total content of the Zip folder is completely read and then the necessary file is accessed. So, it takes more than 55 seconds to play a single file. I need a solution to reduce the time consumed to play the audio files.

Below is my code :

long lStartTime = System.currentTimeMillis();
            System.out.println("Time started");
            String filePath = File.separator+"sdcard"+File.separator+"Android"+File.separator+"obb"+File.separator+"com.mobifusion.android.ldoce5"+File.separator+"main.9.com.mobifusion.android.ldoce5.zip";
            FileInputStream fis = new FileInputStream(filePath);
            ZipInputStream zis = new ZipInputStream(fis);
            String zipFileName = "media"+File.separator+"aus"+File.separator+fileName;
            String usMediaPath = "media"+File.separator+"auk"+File.separator;
            ZipEntry entry;
            int UnzipCounter = 0;
            while((entry = zis.getNextEntry())!=null){
            UnzipCounter++;
                System.out.println(UnzipCounter);
                if(entry.getName().endsWith(zipFileName)){
                    File Mytemp = File.createTempFile("TCL", "mp3", getActivity().getCacheDir());
                    Mytemp.deleteOnExit();
                    FileOutputStream fos = new FileOutputStream(Mytemp);
                    for (int c = zis.read(); c!=-1; c= zis.read()){
                        fos.write(c);
                    }
                    if(fos!=null){
                        mediaPlayer = new MediaPlayer();
                    }
                    fos.close();
                    FileInputStream MyFile = new FileInputStream(Mytemp);
                    mediaPlayer.setDataSource(MyFile.getFD());
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    mediaPlayer.setOnCompletionListener(this);
                    long lEndTime = System.currentTimeMillis();
                    long difference = lEndTime - lStartTime;
                    System.out.println("Elapsed milliseconds: " + difference);
                    mediaPlayer.setOnErrorListener(this);
                }
                zis.closeEntry();
            }
            zis.close();

1 Answers1

2

Try to not re-unzip the file because it consume too much time. Instead of re-unzip the file, you can follow the following step:

  1. Unzip file when first time app is launch. Set flag that we have launch the app before, use preferences.
  2. On the next launch, check the flag. If app never launched before, goto first step. If it had launched before find the file and play.

If you really can't use those step, because it's your requirement, you can try using truezip vfs. But be aware that I've never use it before. Here the library: https://truezip.java.net/, https://github.com/jruesga/android_external_libtruezip

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • 1
    FYKI, we are not unzipping the file. It is a zip file, and we access the audio files within the zip file directly. – Mahalakshmi Saravanan Jun 21 '16 at 09:09
  • @MahalakshmiSaravanan I'm really sorry it slipped on my brain.. It could be because you use the wrong way to iterate zip file. This could be a help: [How to iterate zip file records](http://java-performance.info/how-to-iterate-zip-file-records/) – ישו אוהב אותך Jun 22 '16 at 01:40
  • By following your approach it takes time to retrieve the file. I have 3 folders in my Zip file, the file from the first folder can be accessed within a span of time but the second and third folder files are taking more than 53 seconds to retrieve the file. Can you help me in this? – Mahalakshmi Saravanan Jun 29 '16 at 05:55
  • Instead copying byte per byte, try using BufferedOutputStream. Change **for (int c = zis.read(); c!=-1; c= zis.read()){ fos.write(c); }** to something like: **BufferedOutputStream bufout = new BufferedOutputStream(fout); byte[] buffer = new byte[1024]; int read = 0; while ((read = zipStream.read(buffer)) != -1) { bufout.write(buffer, 0, read); } zipStream.closeEntry(); bufout.close();** – ישו אוהב אותך Jun 29 '16 at 13:45