0

I'm trying to use zip4j on password protected zip files received through a ZipInputStream. It's mostly working well, but I have a .zip file with subdirectories and when I'm looping over it I am only seeing the one directory.

Minimal example:

  1. Create a password protected zip with one directory in it with 1 file in that (I chose the password "password" for simplicity). Your zip will look like this:
$ unzip -l test_dir.zip
Archive:  test_dir.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  07-04-2021 21:36   test_dir/
        0  07-04-2021 21:36   test_dir/test_file
---------                     -------
        0                     2 files
  1. Load the zip in with a ZipInputStream
    public void minimalExample() throws IOException {
        InputStream inputStream = getClass().getResourceAsStream("test_dir.zip");

        LocalFileHeader localFileHeader;
        ZipInputStream zipInputStream = new ZipInputStream(inputStream, "password".toCharArray());

        while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
            Log.w("File found:", localFileHeader.getFileName());
        }
    }

The output will be as follows if the .zip is password-protected:

W/File found:: test_dir/

(So, the test_dir/test_file is not found! This works if it is not password-protected)

Anyone have any tips on what I may be doing wrong?

  • 1
    So regular zip unpacking with subdirs works fine? If so, please turn this into a [mcve] with _instructions_ instead of links on how create a zip file that shows off the problem, when read in by your MCVE code. (Because aside from the fact that [links die, which shouldn't invalidate SO questions](/help/how-to-ask), no one in their right mind is going to download a random zip file from an unknown repo, that's just begging for trouble ;) – Mike 'Pomax' Kamermans Jul 04 '21 at 18:13
  • I've reformatted it to use a minimal reproducible example and instructions on how to create such a .zip file. – Sylvia van Os Jul 04 '21 at 19:48
  • Thanks, but please update that java code to be a fully qualified `Test.java` or the like, starting with the bare minimum imports, and having a public static main, relying on a file with that hardcoded name, so that folks can copy it, and run it, without any compile errors. Also, you've left the first question unanswered: does regular zip unpacking (so: without a password) with subdirs work fine? – Mike 'Pomax' Kamermans Jul 04 '21 at 20:25
  • Honestly, no clue how I'd do that. I only hobby Android dev and an Android project would be big as hell because of all the boilerplate, to the point that it'd be less clear than this. Regular Java has been extremely long for me. And no, I DID answer that question: it works. Read again please: "This works if it is not password-protected" – Sylvia van Os Jul 05 '21 at 10:19

1 Answers1

0

So, it turns out I ran into 2 different issues:

In my main code, I forgot to create a new InputStream after feeding it to zip4j once (oops).

However, generating a minimal example, I triggered a completely different bug with Ark-created password-protected zip files https://github.com/srikanth-lingala/zip4j/issues/329.

Remember (and it makes sense!), zip4j reads the InputStream you feed it so if you read it to see if the password is right, you need to give it a new InputStream afterwards or you won't find any files anymore!