首页 > 解决方案 > 如何通过 ProgressDialog 从外部存储中的资产中提取 zip 文件

问题描述

这是我的代码,我认为 doInBackground 方法没有运行,此代码生成目标文件夹但它是空的,我想在目标文件夹中提取 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;
}}}

标签: javaandroid

解决方案


推荐阅读