diff --git a/src/main/java/net/lingala/zip4j/headers/HeaderReader.java b/src/main/java/net/lingala/zip4j/headers/HeaderReader.java index f15c13e5..b957f691 100755 --- a/src/main/java/net/lingala/zip4j/headers/HeaderReader.java +++ b/src/main/java/net/lingala/zip4j/headers/HeaderReader.java @@ -60,6 +60,12 @@ public class HeaderReader { private byte[] intBuff = new byte[4]; public ZipModel readAllHeaders(RandomAccessFile zip4jRaf, Charset charset) throws IOException { + + 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"); + } + zipModel = new ZipModel(); try { diff --git a/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java b/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java index edc4c84b..041e5f5f 100644 --- a/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java +++ b/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java @@ -366,6 +366,16 @@ public void testExtractNestedZipFileWithAesOnInnerAndZipStandardOuter() throws I testExtractNestedZipFileWithEncrpytion(EncryptionMethod.AES, EncryptionMethod.ZIP_STANDARD); } + @Test + public void testExtractZipFileLessThanMinimumExpectedZipFileSizeThrowsException() throws IOException { + expectedException.expect(ZipException.class); + expectedException.expectMessage("Zip file size less than minimum expected zip file size. " + + "Probably not a zip file or a corrupted zip file"); + + ZipFile zipFile = new ZipFile(getTestArchiveFromResources("invalid_zip_file_size_less_than_22kb.zip")); + zipFile.extractAll(temporaryFolder.toString()); + } + private void testExtractNestedZipFileWithEncrpytion(EncryptionMethod innerZipEncryption, EncryptionMethod outerZipEncryption) throws IOException { File innerZipFile = temporaryFolder.newFile("inner.zip"); diff --git a/src/test/resources/test-archives/invalid_zip_file_size_less_than_22kb.zip b/src/test/resources/test-archives/invalid_zip_file_size_less_than_22kb.zip new file mode 100755 index 00000000..5ef7ea2f --- /dev/null +++ b/src/test/resources/test-archives/invalid_zip_file_size_less_than_22kb.zip @@ -0,0 +1 @@ +version=18.3.0