Skip to content

How to correctly add empty folder entries with ZipOutputStream #410

@daghendrik

Description

@daghendrik

I'm getting "Error 79 - Inappropriate file type or format." on macOS Montery if I try to extract a file that's created using ZipOutPutStream and that contains empty folders.

On Windows 11, using the built in Zip utility, no error is shown. When using 7zip on Windows, the files and folders are successfully extracted, but 7zip warns me about "Header errors". On Mac, I was able to successfully extract the empty folders using a tool called "The Unarchiver".

I'm not sure if I'm adding the empty folder zip entries correct. Basically, I'm just looping over all the empty folders that should be added to the zip first, and then, in a secondary loop, I'm adding all the actual files.

A folder is added like this, where the filename is the folder path with a trailing "/":

zipParameters.setFileNameInZip("/20220310T0800 - 75 - test/1 - Presenter - test/"));

Relevant part of the code:

  .....

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  try(ZipOutputStream zos = initializeZipOutputStream(bos, input.isEncryptZip(), password )) {

      // Add all empty folders first
      for (String emptyFolder : emptyFolders){
          zipParameters.setFileNameInZip(emptyFolder);
          zos.putNextEntry(zipParameters);
          zos.closeEntry();
      }

      ..... loop adding actual files here (code removed. Is just like the example in the readme).

      // closing the stream and uploading to AWS S3
      zos.close();
      s3Client.putObject(putObjectRequest, RequestBody.fromBytes(bos.toByteArray()));
  }

  .....

Attached an example Zip generated by Zip4j with one empty folder and one folder with a file.

futureconf2021-20220309T1159AM.zip

Metadata

Metadata

Labels

bugSomething isn't workingresolved

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions