0

I am using a service to download files and extract them if they are archived.

The extraction method is wrapped as a asynctask to improve performance of the extraction process.

My problem is that when I run the app on the virtual device, all is fine and the extraction process is really fast but as soon as I test it on a real device (Nexus 9 tablet, Android 6x) the extraction process is really slow and takes minutes to complete.

Is there anything I can do, to speed up the extraction process?

I execute the asynctask with: new UnRarTask(targetAppName).execute();

Below the piece of code which is relevant:

public class DownloadTask implements Runnable {

private DownloadService service;
private DownloadManager downloadManager;
protected void init(DownloadService service, Intent intent) {

this.service = service;
downloadManager = (DownloadManager) MyApp_.getInstance().
                getSystemService(Activity.DOWNLOAD_SERVICE);
DownloadRequest downloadRequest = intent.getParcelableExtra(DownloadService
                .DOWNLOAD_REQUEST);
}


private class UnRarTask extends AsyncTask<Void, Integer, String> {

         String rarPath = null;
         int countRar = 0;
         long copiedbytes = 0, totalbytes = 0;

         Archive archive = null;
         FileHeader fileHeader = null;
         File archiveFile;
         List<FileHeader> headers;

         UnRarTask(String one) {

             this.archiveFile = new File(one);
         }

         @Override
         protected String doInBackground(Void... params) {

             try {

                 archive = new Archive(new FileVolumeManager(archiveFile));

             } catch (RarException | IOException e) {

                 e.printStackTrace();
             }

             String fileName = archiveFile.getName();
             String absolutePath = archiveFile.getAbsolutePath();
             String archiveDirectoryFileName = absolutePath.substring(0, absolutePath.indexOf(fileName));

             if (archive != null) {

                 fileHeader = archive.nextFileHeader();
                 headers = archive.getFileHeaders();

                 for (FileHeader fh : headers) {

                     totalbytes = totalbytes + fh.getFullUnpackSize();
                 }
             }

             while (fileHeader != null) {

                 BufferedInputStream inputStream;

                 try {

                     inputStream = new BufferedInputStream(archive.getInputStream(fileHeader));

                     String extractedFileName = fileHeader.getFileNameString().trim();
                     String fullExtractedFileName = archiveDirectoryFileName + extractedFileName;

                     File extractedFile = new File(fullExtractedFileName);
                     FileOutputStream fileOutputStream = new FileOutputStream(extractedFile);
                     BufferedOutputStream flout = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);

                     if (extractedFile.getName().toLowerCase().endsWith(".mp3")
                             || extractedFile.getName().toLowerCase().endsWith(".epub")
                             || extractedFile.getName().toLowerCase().endsWith(".pdf")
                             || extractedFile.getName().toLowerCase().endsWith(".mobi")
                             || extractedFile.getName().toLowerCase().endsWith(".azw3")
                             || extractedFile.getName().toLowerCase().endsWith(".m4b")
                             || extractedFile.getName().toLowerCase().endsWith(".apk")) {

                         rarPath = extractedFile.getPath();
                         countRar++;
                     }

                     int len;
                     byte buf[] = new byte[BUFFER_SIZE];
                     while ((len = inputStream.read(buf)) > 0) {

                         //fileOutputStream.write(buf, 0, len);
                         copiedbytes = copiedbytes + len;

                         int progress = (int) ((copiedbytes / (float) totalbytes) * 100);

                         if (progress > lastProgress) {
                             lastProgress = progress;

                             service.showUpdateProgressNotification(downloadId, appName, progress,
                                     "Extracting rar archive: " + lastProgress + " % completed", downloadStart);
                         }
                     }

                     archive.extractFile(fileHeader, flout);

                     flout.flush();
                     flout.close();

                     fileOutputStream.flush();
                     fileOutputStream.close();

                     inputStream.close();

                     fileHeader = archive.nextFileHeader();

                 } catch (RarException | IOException e) {

                     e.printStackTrace();
                 }
             }

             if (countRar == 0) {

                 filePath = "Error";
                 broadcastFailed();
             }

             if (copiedbytes == totalbytes) {

             if (archive != null)
                 archive.close();
             }

             return null;
         }
     }

}
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Simon
  • 1,691
  • 2
  • 13
  • 28
  • Hi Aj, question is about the extraction method too slow, the internet connection is fine and the download goes fast. Extracting the archive is too slow. – Simon Mar 06 '18 at 06:37
  • Have you looked at [this answer](https://stackoverflow.com/a/21159437/3145960)? – Reaz Murshed Mar 06 '18 at 06:40
  • Hi Reaz, if I make a normal void method, the extraction is even slower. – Simon Mar 06 '18 at 06:42

0 Answers0