如何通过ProgressDialog从外部存储中的资产中提取zip文件

时间:2019-07-12 20:18:57

标签: java android

  

这是我的代码,我认为doInBackground方法未运行,此代码使targetfolder变为空,我想在targetfolder中提取myzipfile.zip

在MainActivity中获取zip文件的路径

String zipFilename = this.getAssets().openFd("raw/"+"myzipfile.zip")";
String unzipLocation = Environment.getExternalStorageDirectory() + "/targetfolder";
final Decompress d = new Decompress(zipFilename, unzipLocation);
d.execute();

制作Decompree类以在后台解压缩

private class Decompress extends AsyncTask<Void, Integer, Integer> {

private String _zipFile;
private String _location;
private int per = 0;
@Override
protected Integer doInBackground(Void... params) {

    try {
        ZipFile zip = new ZipFile(_zipFile);
        mProgressDialog.setMax(zip.size());
        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 {

                per++;
                publishProgress(per);

                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);
    }

    return null;
}}}

0 个答案:

没有答案