Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中解压文件时手机被挂起_Android_Multithreading_Performance_File_Unzip - Fatal编程技术网

在android中解压文件时手机被挂起

在android中解压文件时手机被挂起,android,multithreading,performance,file,unzip,Android,Multithreading,Performance,File,Unzip,我正在尝试在android手机中解压一个.zip文件。下面的代码运行良好 public static void unzip(File zipFile, File targetDirectory) throws IOException { ZipInputStream zis = new ZipInputStream( new BufferedInputStream(new FileInputStream(zipFile))); try {

我正在尝试在android手机中解压一个.zip文件。下面的代码运行良好

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(
            new BufferedInputStream(new FileInputStream(zipFile)));
    try {
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " +
                        dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }

            /* if time should be restored as well
            long time = ze.getTime();
            if (time > 0)
                file.setLastModified(time);
            */
        }
    } finally {
        zis.close();
    }

}
当我使用参数调用此方法时,它成功地解压缩了文件,但问题是文件大小为55MB,调用此方法之前,应用程序运行良好,但当我调用此方法时,应用程序解压缩文件所需的几秒钟(约8-13秒)应用程序卡住,除了成功解压缩文件后,没有任何工作,该应用程序再次工作良好,所以请帮助我,使该应用程序应在解压缩文件的工作。 我还尝试在中执行该方法

runOnUiThread(new Runnable() {
}))


但是没有成功。

如果应用程序冻结,通常是因为您在主/UI线程上进行了太多计算(请注意,
runOnUiThread()
正是这样做的)。为了避免这种情况,您必须在另一个线程或进程中调用您的方法

快速而肮脏的修复方法是使用普通线程:

new Thread(new Runnable() {
    public void run() {
        unzip(zipFile, targetDirectory);
    }
}).start();
或使用异步任务:

new AsyncTask<File, Void, Void>() {
     protected Void doInBackground(File... files) {
         unzip(files[0], files[1]);
         return null;
     }

     protected void onPostExecute(Void result) {
         // we're finished
     }
}.execute(zipFile, targetDirectory);
newasynctask(){
受保护的Void doInBackground(文件…文件){
解压缩(文件[0],文件[1]);
返回null;
}
受保护的void onPostExecute(void结果){
//我们结束了
}
}.execute(zipFile,targetDirectory);

您的代码遇到中CERT引用的解压问题。除了在下面的第一个答案中使用背景线程之外,还可以考虑验证,以确保您不解压缩代码<>目标目录< /代码>之外的文件,并且更不受拉链炸弹和类似攻击的影响。我的CWAC安全库有一个
ZipUtils
类,它有一个
unzip()
方法来解决这些攻击:非常感谢您的帮助。我曾经使用过“AsyncTask”,它工作得很好,但是一个新的问题出现了,如果我不使用“AsyncTask”运行应用程序,在应用程序退出“System.exit(0)”时,我的应用程序成功关闭,但当我使用“AsyncTask”时,在使用“System.exit(0)”退出应用程序后,应用程序再次重新启动,并使用在“AsyncTask”的“doInBackground”中编写的相同方法。所以,请帮助我,我如何才能解决这个问题,使应用程序完全关闭。