【问题标题】:Monitoring Zip4J extractAll() method progress monitor监控 Zip4J extractAll() 方法进度监控
【发布时间】:2019-09-30 05:35:38
【问题描述】:

我正在使用 Zip4J 来提取 zip 文件,我能够做到。但是,我想使用 Zip4J 中提供的进度监视器,但无法成功使用它。 文档只说它应该在线程模式下运行。我做到了,我的控制台卡在命令行上。任何带有进度监视器的 extractAll() 工作示例。

public String unzipFile(String sourceFilePath, String extractionPath) {
        String extractionDirectory = "";
        FileHeader fileHeader = null;
        if (FileUtility.isPathExist(sourceFilePath) && FileUtility.isPathExist(extractionPath)) {
            try {
                ZipFile zipFile = new ZipFile(sourceFilePath);
                LOG.info("File Extraction started");
                List<FileHeader> fileHeaderList = zipFile.getFileHeaders();
                if (fileHeaderList.size() > 0)
                    fileHeader = (FileHeader) fileHeaderList.get(0);
                if (fileHeader != null)
                    extractionDirectory = splitFileName(fileHeader.getFileName());
                long totalPercentage = 235;
                long startTime = System.currentTimeMillis();
                zipFile.extractAll(extractionPath);
                LOG.info("File Extraction completed.");
                System.out.println();
            } catch (ZipException e) {
                LOG.error("Extraction Exception ->\n" + e.getMessage());
            }
        } else {
            LOG.error("Either source path or extraction path is not exist.");
        }
        return extractionDirectory;
    }

【问题讨论】:

    标签: java progress-bar monitoring zip4j


    【解决方案1】:

    不知道,如果你添加足够多的文件,工作正常,实际上有一个进展可以看到。为此,我添加了一些非常胖的。

    @Test
    public void testExtractAllDeflateAndNoEncryptionExtractsSuccessfully() throws IOException {
        ZipFile zipFile = new ZipFile(generatedZipFile);
    
         List<File> toAdd = Arrays.asList(
                getTestFileFromResources("sample_text1.txt"),
                getTestFileFromResources("sample_text_large.txt"),
                getTestFileFromResources("OrccTutorial.pdf"),
                 getTestFileFromResources("introduction-to-automata-theory.pdf"),
                 getTestFileFromResources("thomas.pdf")
        );
        zipFile.addFiles(toAdd);
    
        zipFile.setRunInThread(true);
    
        zipFile.extractAll(outputFolder.getPath());
    
        ProgressMonitor mon = zipFile.getProgressMonitor();
        while (mon.getState() == BUSY) {
            System.out.println(zipFile.getProgressMonitor().getPercentDone());
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        ZipFileVerifier.verifyFolderContentsSameAsSourceFiles(outputFolder);
        verifyNumberOfFilesInOutputFolder(outputFolder, 5);
    }
    

    【讨论】:

    • 感谢您的回复。是的,这对我有用。但是,这在任务完成后默认不会从 Thread 中出来。当我在 Eclipse 中运行控制台时,控制台显示,程序仍在运行。从线程完成任务后如何中止?
    • 我建议你调试一下。我也必须这样做,请参阅eclipse.org/community/eclipse_newsletter/2017/june/article1.php
    • 我添加了图片。当我通过制作一个可运行的 jar 来运行 java -jar 时,我在控制台上遇到了堆异常。那么这是否意味着 Thread 正在内部运行并使系统保持忙碌状态?如图所示,完成作业后如何停止线程。
    • 对不起,我不知道。可能是任何东西,由您的代码引起或隐藏在组件中。此外,我的草图也不是为了生产保存。
    【解决方案2】:

    testAddFilesWithProgressMonitor.java在项目的测试用例中展示了如何使用ProgressMonitor。

    【讨论】:

    • 我无法获得任何将其用于 extractAll() 并显示提取进度的示例。您能否提供任何此类示例或 sn-p 代码。我试图跟随,但目前无法跟随。