Skip to content

Commit

Permalink
getNextEntry should return next zip entry even if current entry is no…
Browse files Browse the repository at this point in the history
…t completely read
  • Loading branch information
srikanth-lingala committed Sep 8, 2019
1 parent b392c9a commit 12d86e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/net/lingala/zip4j/io/inputstream/ZipInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class ZipInputStream extends InputStream {
private LocalFileHeader localFileHeader;
private CRC32 crc32 = new CRC32();
private boolean extraDataRecordReadForThisEntry = false;
private byte[] endOfEntryBuffer;

public ZipInputStream(InputStream inputStream) {
this(inputStream, null);
Expand All @@ -64,6 +65,10 @@ public LocalFileHeader getNextEntry() throws IOException {
}

public LocalFileHeader getNextEntry(FileHeader fileHeader) throws IOException {
if (localFileHeader != null) {
readUntilEndOfEntry();
}

localFileHeader = headerReader.readLocalFileHeader(inputStream);

if (localFileHeader == null) {
Expand Down Expand Up @@ -300,6 +305,13 @@ private int getEncryptionHeaderSize(LocalFileHeader localFileHeader) {
}
}

private void readUntilEndOfEntry() throws IOException {
if (endOfEntryBuffer == null) {
endOfEntryBuffer = new byte[512];
}
while (read(endOfEntryBuffer) != -1);
}

private boolean isEncryptionMethodZipStandard(LocalFileHeader localFileHeader) {
return localFileHeader.isEncrypted() && EncryptionMethod.ZIP_STANDARD.equals(localFileHeader.getEncryptionMethod());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ public void testExtractFilesForZipFileWithInvalidExtraDataRecordIgnoresIt() thro
zipInputStream.close();
}

@Test
public void testGetNextEntryReturnsNextEntryEvenIfEntryNotCompletelyRead() throws IOException {
File createZipFile = createZipFile(CompressionMethod.DEFLATE);
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(createZipFile));
int numberOfEntries = 0;
while (zipInputStream.getNextEntry() != null) {
numberOfEntries++;
}
assertThat(numberOfEntries).isEqualTo(FILES_TO_ADD.size());
}

private void extractZipFileWithInputStreams(File zipFile, char[] password) throws IOException {
extractZipFileWithInputStreams(zipFile, password, 4096, AesVersion.TWO);
}
Expand Down

0 comments on commit 12d86e3

Please sign in to comment.