如何使减压任务更快?

时间:2018-12-26 19:47:33

标签: java android android-studio

我想解压缩文件,我的代码可以正常工作,但是速度很慢。 示例:如果我想解压缩大小为4 MB的文件,则大约需要40分钟才能完成。有没有办法使任务更快?

我的代码:

public class DecompressManager extends DownloadManager {

    public static final int DECOMPRESS_SUCCESS = 101;
    public static final int DECOMPRESS_FAILED = 102;


    public DecompressManager(DownloadListener downloadListener) {
        super(downloadListener);
    }

        public int DecompressTask(File zipFile, File targetDirectory){
        updateTaskProgress("Starting unzip file",-1,null);
        int result = DECOMPRESS_FAILED;

        //unzip file
        ZipInputStream zis = null;
        try {
            zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));

            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;
                try (FileOutputStream fout = new FileOutputStream(file)) {
                    long total = 0;
                    while ((count = zis.read(buffer)) != -1) {
                        fout.write(buffer, 0, count);
                        total += count;
                        updateTaskProgress("Decompress task",(int) ((total * 100) / ze.getSize()),null);
                    }
                }

            }
            result = DECOMPRESS_SUCCESS;

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert zis != null;
                zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

}

0 个答案:

没有答案