添加进度条以解压缩进程

时间:2015-06-22 11:10:17

标签: android

我有一个从教程网站上使用的解压缩类,工作正常,但我想在解压缩过程中添加一个进度条,我搜索了一些源代码,但我不知道在课堂上我在哪里应该说。这是decompress.java类:

    import android.util.Log; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipInputStream; 

/** 
 * 
 * @author jon 
 */ 
public class Decompress { 
  private String _zipFile; 
  private String _location; 

  public Decompress(String zipFile, String location) { 
    _zipFile = zipFile; 
    _location = location; 

    _dirChecker(""); 
  } 

  public void unzip() { 
    try  { 
      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 { 
      FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
      for (int c = zin.read(); c != -1; c = zin.read()) { 
        fout.write(c); 
      } 

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

      } 
      zin.close(); 
    } catch(Exception e) { 
      Log.e("Decompress", "unzip", e); 
    } 

  } 

  private void _dirChecker(String dir) { 
    File f = new File(_location + dir); 

    if(!f.isDirectory()) { 
      f.mkdirs(); 
    } 
  } 
} 

我在我的活动中这样说:

Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip()

1 个答案:

答案 0 :(得分:0)

按照以下步骤操作:

1。

Decompress d = new Decompress(CurrentActivity.this,zipFile, unzipLocation);
d.unzip();

<强> 2

Activity _activity;
        private String _zipFile; 
        private String _location; 
        ProgressDialog progressDialog =null;
        public Decompress(Activity activity,String zipFile, String location) { 
            _zipFile = zipFile; 
            _location = location; 
            this._activity = activity;
            _dirChecker(""); 
            progressDialog = new ProgressDialog(activity,
                    android.R.style.Theme_Panel);
            progressDialog.setMessage("Please wait...");
            progressDialog.setCancelable(false);
        } 








public void unzip() { 
        try  { 
            progressDialog.show(); // Showing Progress Dialog 
            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 { 
                    FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
                    for (int c = zin.read(); c != -1; c = zin.read()) { 
                        fout.write(c); 
                    } 

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

            } 
            progressDialog.cancel(); // cancelling Dialog.
            zin.close(); 
        } catch(Exception e) { 
            progressDialog.cancel();
            Log.e("Decompress", "unzip", e); 
        } 

    } 

    private void _dirChecker(String dir) { 
        File f = new File(_location + dir); 

        if(!f.isDirectory()) { 
            f.mkdirs(); 
        } 
    } 
}