首页 > 解决方案 > 如何让解压任务更快?

问题描述

我想解压缩文件,我的代码正在运行,但速度很慢。示例:如果我想解压缩一个大小为 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;
    }

}

标签: javaandroidandroid-studio

解决方案


推荐阅读