diff --git a/src/main/java/net/lingala/zip4j/headers/HeaderReader.java b/src/main/java/net/lingala/zip4j/headers/HeaderReader.java index e60da7f5..b40293f9 100755 --- a/src/main/java/net/lingala/zip4j/headers/HeaderReader.java +++ b/src/main/java/net/lingala/zip4j/headers/HeaderReader.java @@ -66,6 +66,10 @@ public class HeaderReader { public ZipModel readAllHeaders(RandomAccessFile zip4jRaf, Zip4jConfig zip4jConfig) throws IOException { + if (zip4jRaf.length() == 0) { + return new ZipModel(); + } + if (zip4jRaf.length() < ENDHDR) { throw new ZipException("Zip file size less than minimum expected zip file size. " + "Probably not a zip file or a corrupted zip file"); diff --git a/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java b/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java index fcaa9519..e4c9d10c 100644 --- a/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java +++ b/src/test/java/net/lingala/zip4j/AddFilesToZipIT.java @@ -949,6 +949,22 @@ public void testAddStreamWithStoreCompressionAndAesEncryptionWorksFine() throws extractZipFileWithStream(generatedZipFile, PASSWORD); } + @Test + public void testAddStreamToAnEmptyFileDoesNotThrowException() throws IOException { + if (!generatedZipFile.createNewFile()) { + throw new RuntimeException("Cannot create an empty file to test"); + } + File fileToAdd = TestUtils.getTestFileFromResources("sample.pdf"); + try (ZipFile zipFile = new ZipFile(generatedZipFile); + InputStream inputStream = Files.newInputStream(fileToAdd.toPath())) { + ZipParameters zipParameters = new ZipParameters(); + zipParameters.setFileNameInZip(fileToAdd.getName()); + zipFile.addStream(inputStream, zipParameters); + } + + ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 1); + } + @Test public void testAddFolderWithCustomBufferSize() throws IOException { ZipFile zipFile = new ZipFile(generatedZipFile);