diff --git a/src/main/java/net/lingala/zip4j/io/inputstream/ZipEntryInputStream.java b/src/main/java/net/lingala/zip4j/io/inputstream/ZipEntryInputStream.java index 9d925932..68d032a1 100644 --- a/src/main/java/net/lingala/zip4j/io/inputstream/ZipEntryInputStream.java +++ b/src/main/java/net/lingala/zip4j/io/inputstream/ZipEntryInputStream.java @@ -58,6 +58,10 @@ public int readRawFully(byte[] b) throws IOException { int readLen = inputStream.read(b); + if (readLen == -1) { + throw new IOException("Unexpected EOF reached when trying to read stream"); + } + if (readLen != b.length) { readLen = readUntilBufferIsFull(b, readLen); diff --git a/src/test/java/net/lingala/zip4j/io/inputstream/ZipInputStreamIT.java b/src/test/java/net/lingala/zip4j/io/inputstream/ZipInputStreamIT.java index 7b2ed9f1..87de6862 100644 --- a/src/test/java/net/lingala/zip4j/io/inputstream/ZipInputStreamIT.java +++ b/src/test/java/net/lingala/zip4j/io/inputstream/ZipInputStreamIT.java @@ -328,6 +328,15 @@ public void testExtractZipFileWithInvalidAesExtraDataRecordThrowsException() thr null, InternalZipConstants.BUFF_SIZE, false, 1); } + @Test + public void testExtractZipFileWhenUnexpectedEofReachedThrowsException() throws IOException { + expectedException.expect(IOException.class); + expectedException.expectMessage("Unexpected EOF reached when trying to read stream"); + + extractZipFileWithInputStreams(TestUtils.getTestArchiveFromResources("unexpected-eof-when-reading-stream"), + null, InternalZipConstants.BUFF_SIZE, false, 1); + } + private void extractZipFileWithInputStreams(File zipFile, char[] password) throws IOException { extractZipFileWithInputStreams(zipFile, password, InternalZipConstants.BUFF_SIZE); } diff --git a/src/test/resources/test-archives/unexpected-eof-when-reading-stream b/src/test/resources/test-archives/unexpected-eof-when-reading-stream new file mode 100644 index 00000000..7b53ec50 Binary files /dev/null and b/src/test/resources/test-archives/unexpected-eof-when-reading-stream differ