javaprogress-barmonitoringzip4j

Monitoring Zip4J extractAll() method progress monitor


enter image description hereI am using Zip4J for extracting zip file and I am able to do it. However, I want to use progress monitor provided in Zip4J but not able to use it successfully. The documentation only says that it should have run in thread mode true. I did it and my console stuck on this on command line. Any working example of extractAll() with progress monitor.

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;
    }

Solution

  • Don't know, works fine if you add enough files, that there actually is a progress to see. I added some really fat ones for the purpose.

    @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);
    }