0

I have added zip4j library to my Android project. It is very easy to use.

ZipFile zipFile = new ZipFile(downloadedFilePath);
zipFile.extractAll(context.getFilesDir().getPath());

But there is one feature that I am concerned about. Instead of subscribing to get the progress of unzipping, they use while-loop as follows:

while (pm.getState() == ProgressMonitor.STATE_BUSY) {
    // my progress handler is here
}

And if I need to know that unzip is complete, I should use it as follows:

while (pm.getState() == ProgressMonitor.STATE_BUSY) {
    // the loop runs until progress monitor changes its status
}
if (pm.getResult() == ProgressMonitor.RESULT_SUCCESS) {
    // my code to handle completion
}

To me it looks as anti-pattern and poor coding. Should I use in my project, or switch to a different zip-library?

tasomaniac
  • 10,234
  • 6
  • 52
  • 84
Alex Smolov
  • 1,831
  • 4
  • 24
  • 43

2 Answers2

1

while loop will use CPU resources and keep it busy. i would suggest use Thread and sleep it for a while and then again check pm.getState() and update your progress bar

Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
1

It's better to use AsyncTask for this kind of task.

Start showing progress on onPreExecute, extract the files in doInBackground, and dismiss the progress in onPostExecute.

You can also use onProgresUpdate to show the exact progress of the work.

This will also ensure that the extraction work happens in a background thread, and will keep the UI thread free.

k1slay
  • 1,068
  • 1
  • 10
  • 18