Some of android sinkhole about zip4j

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Coder_Hcy/article/details/82349761

1. There is a recent project requirements, that is, from the oos download and extract the compressed package, to decrypt the swf demand.

2. Unzip the process, to show the progress of decompression. After successfully decrypt the swf file decompression.

3. sinkhole on display unzip progress: there are eight of ten online method:

/**
     * @param zipFile     需要解压的文件
     * @param filePath    解压后的文件目录
     * @param isDeleteZip 是否删除解压包
     * @throws ZipException
     */
    private void unZipFileWithProgress(File zipFile, String filePath, boolean isDeleteZip) throws ZipException {
        String usbOrTfPath = zipFile.getAbsolutePath();
        ZipFile zFile = new ZipFile(zipFile);
        zFile.setFileNameCharset("GBK");
        if (!zFile.isValidZipFile()) {
            if (zipFileImp != null) {
                flag_unZip = false;
                zipFileImp.copyAndUnPackFailed("文件不合法或不存在" + zipFile.getAbsolutePath());
            }
            Log.e("======", "文件不合法或不存在" + zipFile.getAbsolutePath());
            return;
        }
        File destDir = new File(filePath); // ��ѹĿ¼
        if (destDir.isDirectory() && !destDir.exists()) {
            destDir.mkdir();
        }
        if (zFile.isEncrypted()) {
            // zFile.setPassword(password); // 设置解压密码
        }


        // if (totalWork != 0) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    flag_unZip = true;
                    ProgressMonitor progressMonitor = zFile.getProgressMonitor();
                    int precentDone = 1;
                    boolean isFileeixst = false;
                    while (flag_unZip) {
                        Log.i("TTFFGG", "RRRR");
                        File nomorlFile = new File(usbOrTfPath);
                        if (nomorlFile != null && nomorlFile.exists()) {
                            isFileeixst = true;

                        } else {
                            isFileeixst = false;
                            if (zipFileImp != null) {
                                flag_unZip = false;
                                zipFileImp.usbHasTakeOut("发现U盘拔出");
                            }
                        }

                        // 每隔50ms,发送一个解压进度出去
                        // if (progressMonitor.getTotalWork() > 0) {
                        precentDone = progressMonitor.getPercentDone();
                        //
                        if (zipFileImp != null) {
                            if (precentDone != 0 && lastProgress == 0) {
                                //开始
                                Logger.i("解压SSS1::" + progressMonitor.getTotalWork() + ":::" + progressMonitor.getWorkCompleted());
                                zipFileImp.copyAndUnPackProgress(precentDone * 80 / 100);
                                lastProgress = precentDone;
                            } else if (precentDone != 0 && lastProgress != 0) {
                                Logger.i("解压SSS2::" + progressMonitor.getTotalWork() + ":::" + progressMonitor.getWorkCompleted());
                                zipFileImp.copyAndUnPackProgress(precentDone * 80 / 100);
                                lastProgress = precentDone;
                            } else if (precentDone == 0 && lastProgress != 0) {
                                Logger.i("解压SSS3::" + progressMonitor.getTotalWork() + ":::" + progressMonitor.getWorkCompleted());
                                //if (progressMonitor.getTotalWork() != 0 && progressMonitor.getWorkCompleted() != 0) {
                                if (progressMonitor.getTotalWork() == progressMonitor.getWorkCompleted()) {
                                    // flag_unZip = false;

                                    if (zipFileImp != null) {
                                        Thread.sleep(2500);
                                        File nomorlFile2 = new File(usbOrTfPath);
                                        if (nomorlFile2 != null && nomorlFile2.exists() && isFileeixst) {
                                            Log.i("copy materials==", "解压成功" + zipFile.getAbsolutePath());
                                            flag_unZip = false;
                                            zipFileImp.copyAndUnPackSuccess();
                                        } else {
                                            if (zipFileImp != null) {
                                                flag_unZip = false;
                                                zipFileImp.usbHasTakeOut("发现U盘拔出");
                                            }
                                            Log.i("copy materials==", "发现U盘拔出" + usbOrTfPath);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    Log.i("copy materials==", "解压失败" + e.getMessage());
                    if (zipFileImp != null) {
                        flag_unZip = false;
                        zipFileImp.copyAndUnPackFailed(e.getMessage().toString());
                    }
                } finally {
                    if (isDeleteZip) {
                        flag_unZip = false;
                        zipFile.delete();//将原压缩文件删除
                    }
                }
            }
        });
        thread.start();
        zFile.setRunInThread(true); //true 在子线程中进行解压 , false主线程中解压
        zFile.extractAll(filePath); //将压缩文件解压到filePath中...
    }

1. This method has a big problem is that progress is not complete, unzip the successful determination has big loophole (when extracting files in the U disk, unplug the U disk decompression process will actually go decompression judging success !!! )

progressMonitor.getTotalWork() == progressMonitor.getWorkCompleted()

2. no good solution yet

3. Only take the second decompression method but there are limitations (must be a multiple archive files)

 /**
     * 课件整包,解压方法
     *
     * @param archive
     * @param unicode
     * @param decompressDir
     * @param uiHandler
     * @param allCount
     */
    public static void unZipFileByZip4j(final String archive, final String unicode, final String decompressDir, final Handler uiHandler, final int allCount, UnZipCallBack unZipCallBack) {
        stopUnzip = false;
        try {
            int currentProcess = 0;
            int oldProcess = 0;
            long startTime = System.currentTimeMillis();
            ZipFile zipFile2 = new ZipFile(archive);
            FileHeader fileHeader = null;
            //设置编码格式
            zipFile2.setFileNameCharset(unicode);
            if (!zipFile2.isValidZipFile()) {
                Log.e("======", "文件不合法或不存在");
                return;
            }
            List<FileHeader> fileHeaderList = zipFile2.getFileHeaders();
            for (int i = 0; i < fileHeaderList.size(); i++) {
                if(!stopUnzip){
                    fileHeader = fileHeaderList.get(i);
                    zipFile2.extractFile(fileHeader, decompressDir);
                    ProgressMonitor progressMonitor = zipFile2.getProgressMonitor();
                    currentProcess = (i + 1) * allCount / fileHeaderList.size();
                    if (currentProcess != oldProcess && currentProcess < 101) {
                        oldProcess = currentProcess;
                        Message message = new Message();
                        Bundle bundle = new Bundle();
                        message.what = Constants.unzipCourse;
                        bundle.putInt("currentTotal", currentProcess);
                        message.obj = bundle;
                        uiHandler.sendMessage(message);
                    }
                }
            }
            if(!stopUnzip){
                Log.e("======", "解压成功!");
                unZipCallBack.onSuccess(archive);
                long endTime = System.currentTimeMillis();
                Log.e("======", "耗时:" + (endTime - startTime) + "ms");
            }
        } catch (Exception e) {
            if(!stopUnzip){
                Log.e("======", e.toString());
                unZipCallBack.onFail(e.toString(), archive);
            }
        }
    }

 

Guess you like

Origin blog.csdn.net/Coder_Hcy/article/details/82349761