Как сделать распаковку быстрее? - PullRequest
0 голосов
/ 26 декабря 2018

Я хочу разархивировать файлы, и мой код работает, но он очень медленный.Пример: если я хочу разархивировать файл размером 4 МБ, его завершение занимает около 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;
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...