I found an excellent UnZip class with progress bar support for Android. However the progress bar does not update when clicking extract in my app. The Unzipping works fine.
Can someone please tell me what I am doing wrong?
public class UnZip1 extends AsyncTask<Void, Integer, Integer> {
{
}
private String _zipFile;
private String _location;
private ProgressDialog mProgressDialog;
private int per = 0;
private Context _conti;
private ProgressBar bar1;
public UnZip1(Context conti,String zipFile, String location) {
_conti = conti;
_zipFile = zipFile;
_location = location;
bar1 = (ProgressBar)findViewById(R.id.pb1);
_dirChecker("");
}
public void streamCopy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[32 * 1024]; // play with sizes..
int readCount;
while ((readCount = in.read(buffer)) != -1) {
out.write(buffer, 0, readCount);
}
}
protected Integer doInBackground(Void... params) {
try {
ZipFile zip = new ZipFile(_zipFile);
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
// Here I am doing the update of my progress bar
Log.v("Decompress", "more " + ze.getName());
per++;
publishProgress(per);
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
streamCopy(zin, fout);
zin.closeEntry();
fout.close();
} }
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
bar1.setProgress(per);
}
protected void onPostExecute(Integer... result) {
Log.i("UnZip" ,"Completed. Total size: "+result);
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
}
You have to use the ProgressDialog with STYLE_HORIZONTAL if you want to see the actual progress. You can add the initialization in your preExecute():
@Override
protected void onPreExecute()
{
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
public void onCancel(DialogInterface dialog)
{
}
});
mProgressDialog.setCancelable(true);
mProgressDialog.setMessage("Progress");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setProgress(1);
mProgressDialog.show();
}
MainActivity is the name of my activity class.
Then set the progress on the onProgressUpdate function.
protected void onProgressUpdate(Integer... progress)
{
mProgressDialog.setProgress(progress[0]);
}