Search code examples
javafilezipunzip

Why does getNextZipEntry skip root directory for few files?


Hi when i am trying to unzip my file i am using the following snippet

    public static List<String> unZip(File tarFile) throws IOException {
    List<String> result = new ArrayList<String>();
    InputStream inputStream = new FileInputStream(tarFile);
    ZipArchiveInputStream in = new ZipArchiveInputStream(inputStream);
    ZipArchiveEntry entry = in.getNextZipEntry();
    while (entry != null) {
        System.out.println("Entry name ::: "+entry);
        if (entry.isDirectory()) {
            System.out.println("This entry is a directory ");
            entry = in.getNextZipEntry();
            continue;
        }
        result.add(entry.getName());
        entry = in.getNextZipEntry();
    }
    in.close();
    return result;
}

My above snippet outputs as

Entry name ::: lib/docssdk.jar
Entry name ::: lib/jackson-annotations-2.7.4.jar
Entry name ::: lib/jackson-core-2.7.4.jar
Entry name ::: lib/jackson-databind-2.7.4.jar
Entry name ::: lib/jsonapi-converter-0.3.jar
Entry name ::: lib/okhttp-3.0.1.jar
Entry name ::: lib/okio-1.7.0.jar
Entry name ::: lib/retrofit-2.0.2.jar

I am expecting it to be as

Entry name ::: lib/
Entry name ::: lib/docssdk.jar
Entry name ::: lib/jackson-annotations-2.7.4.jar
Entry name ::: lib/jackson-core-2.7.4.jar
Entry name ::: lib/jackson-databind-2.7.4.jar
Entry name ::: lib/jsonapi-converter-0.3.jar
Entry name ::: lib/okhttp-3.0.1.jar
Entry name ::: lib/okio-1.7.0.jar
Entry name ::: lib/retrofit-2.0.2.jar

The root folder lib/ is missing

I am getting the expected ouput when i am unzipping the file content in my machine and created a zip again on lib folder.

Why i am missing my root folder here while obtaining getNextZipEntry? Please advice me on this.

I have attached the screenshot of the zip structure along with this

Original Zip File to reproduce this issue


Solution

  • You don't see the lib/ entry because it doesn't exist in your zip file:

    $ zipinfo zip_root_folder_issue.zip 
    Archive:  zip_root_folder_issue.zip
    Zip file size: 1878250 bytes, number of entries: 8
    -rw-r--r--  3.0 unx    17596 bx defN 16-Aug-02 13:38 lib/docssdk.jar
    -rw-r--r--  3.0 unx    50897 bx defN 16-Aug-02 13:40 lib/jackson-annotations-2.7.4.jar
    -rw-r--r--  3.0 unx   253001 bx defN 16-Aug-02 13:39 lib/jackson-core-2.7.4.jar
    -rw-r--r--  3.0 unx  1204187 bx defN 16-Aug-02 13:39 lib/jackson-databind-2.7.4.jar
    -rw-r--r--  3.0 unx    36004 bx defN 16-Aug-02 13:39 lib/jsonapi-converter-0.3.jar
    -rw-r--r--  3.0 unx   326467 bx defN 16-Aug-02 13:39 lib/okhttp-3.0.1.jar
    -rw-r--r--  3.0 unx    69868 bx defN 16-Aug-02 13:38 lib/okio-1.7.0.jar
    -rw-r--r--  3.0 unx    86225 bx defN 16-Aug-02 13:38 lib/retrofit-2.0.2.jar
    8 files, 2044245 bytes uncompressed, 1876818 bytes compressed:  8.2%
    

    Archive managers are likely display intermediary "missing" folders but a zip file doesn't need entries for them. Some file format using zip (xlsx for example) even expect to have none of them.

    In your case, I think you will need to do what these managers do: show/create the folder if it doesn't exist.