Skip to content

Nullpointer exception with empty file name in ZipParameters #388

@pdenooijer

Description

@pdenooijer

Since this change from version 2.9.0 tot 2.9.1 the file name could still be null leading to a null pointer exception.

The validation is done in the FileHeaderFactory, but this is called from the initializeAndWriteFileHeader. As this is now after the directory check, this will never trigger:

  private String validateAndGetFileName(String fileNameInZip) throws ZipException {
    if (!Zip4jUtil.isStringNotNullAndNotEmpty(fileNameInZip)) {
      throw new ZipException("fileNameInZip is null or empty");
    } else {
      return fileNameInZip;
    }
  }

Problematic code, 2e line is the problem when file name is null:

ZipParameters clonedZipParameters = new ZipParameters(zipParameters);
if (isZipEntryDirectory(zipParameters.getFileNameInZip())) {
  clonedZipParameters.setWriteExtendedLocalFileHeader(false);
  clonedZipParameters.setCompressionMethod(CompressionMethod.STORE);
  clonedZipParameters.setEncryptFiles(false);
}
initializeAndWriteFileHeader(clonedZipParameters);

There are multiple solutions to solve this. Moving the validation to an earlier point or making sure there is no null pointer exception by checking if it's not null.

Metadata

Metadata

Labels

bugSomething isn't workingresolved

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions