0

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;
    }
Kumar
  • 955
  • 5
  • 20
  • 50

2 Answers2

1

testAddFilesWithProgressMonitor.java in the the project's test cases shows how to use ProgressMonitor.

Curiosa Globunznik
  • 3,129
  • 1
  • 16
  • 24
  • I am not able to get any example that I use it for extractAll() and that show progress of extraction. Could you provide any such example or snippet code. I tried to follow but unable as of now. – Kumar Sep 30 '19 at 09:43
1

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);
}
Curiosa Globunznik
  • 3,129
  • 1
  • 16
  • 24
  • Thanks for your reply. Yes, this works for me. However, this does not come out from Thread by default after completion of the task. When I run over console in eclipse, the console shows, program is still running. How to abort after completion of the task from thread? – Kumar Oct 01 '19 at 03:00
  • I'd propose you debug the thing. I'd have to do it too, see https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php – Curiosa Globunznik Oct 01 '19 at 03:12
  • I added picture. When I run through java -jar by making a runnable jar, I got heap exception on console. So does it mean, Thread is running internally and keeping the system busy? How to stop the thread after completion of the job once it is done as shown picture. – Kumar Oct 01 '19 at 03:32
  • sorry, I don't know. Could be anything, caused by your code or hidden in the component. Also my sketch wasn't meant to be production save. – Curiosa Globunznik Oct 01 '19 at 03:37