Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to correctly add empty folder entries with ZipOutputStream #410

Closed
daghendrik opened this issue Mar 9, 2022 · 2 comments
Closed

How to correctly add empty folder entries with ZipOutputStream #410

daghendrik opened this issue Mar 9, 2022 · 2 comments
Assignees
Labels
bug Something isn't working resolved

Comments

@daghendrik
Copy link

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

@srikanth-lingala
Copy link
Owner

Thanks for the detailed report of the issue and the sample zip file. It helps a lot to have such detailed information.

I fixed the issue, and will include the fix in the next release.

@srikanth-lingala
Copy link
Owner

Fixed in v2.10.0 released today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working resolved
Projects
None yet
Development

No branches or pull requests

2 participants