Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


extracting files by name

  • Hi Boris, I am trying to extract a file from an archive by name.
    Unfortunately, the code is not really working for me (http://pastebin.com/9VncUUPA). It also extracts some contents of a directory from the archive.
    The test archive is below

    Can you give me some hints about what I'm doing wrong?

    The output:
    Hash | Size | Filename
    found index 2 for blaManifest.json
    0 | 0 | test\helloclt.aim
    7E4F76FF | 870 | blaManifest.json

    Last edit: Horia Constantin 2013-09-01
  • attached the test archive

  • Boris Brodski
    Boris Brodski

    Hello Constantin,

    you are write, the snippet of mine isn't perfect. According to 7-Zip API the getStream() and setOperationResult() callbacks get called for all files. the getStream() callback get the ExtractAskMode parameter, that tells you, if a file is supposed to be extracted or skipped. In the snippet you see

    if (extractAskMode != ExtractAskMode.EXTRACT) {
        return null;

    that actually skips the file extraction. In the setOperationResult() you get extractOperationResult == ExtractOperationResult.OK also for skipped files. This is, why you see

    0 | 0 | test\helloclt.aim

    with the zeros. The simplest solution is to save extractAskMode in getStream() to a field and read it in setOperationResult() to determine, whether the files was skipped or not.

    I hope, it helps.


  • Hi Boris, thanks for pointing out that. Maybe I should have read the 7z manual first :).
    Anyway, I have my code fixed. I am actually using your example from here: https://gist.github.com/borisbrodski/6120309 (combined with a callback to extract specific files)

    And there is a bug in the example, at lines 64-67. In my case, I am also skipping files, and currently, if you detect a file should be skipped you create a directory. So now I'm creating the directory in a nested separate if. You can add this code to the project code samples.


    Cheers and thanks again,

  • Boris Brodski
    Boris Brodski

    Hello Horia,

    thank you for your snippet! I will look into it and upgrade my snippet.


  • Hello Boris, I've discovered a problem with the code that I last submitted to you.

    I was not closing the output stream so the last file that was extracted from the archive was flushed at the jvm shutdown, thus it was not possible to delete it immediately.

    I've corrected that (now calling callback.closeOutputStream() explicitly after inArchive.extract).

    Code is here:

    Last edit: Horia Constantin 2013-10-03