Skip to content

Commit

Permalink
#301 Set password also via a setter on ZipInputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed May 25, 2021
1 parent d7094b9 commit 97679f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 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 @@ -185,6 +185,17 @@ public int available() throws IOException {
return entryEOFReached ? 0 : 1;
}

/**
* Sets the password for the inputstream. This password will be used for any subsequent encrypted entries that will be
* read from this stream. If this method is called when an entry is being read, it has no effect on the read action
* of the current entry, and the password will take effect from any subsequent entry reads.
*
* @param password Password to be used for reading of entries from the zip input stream
*/
public void setPassword(char[] password) {
this.password = password;
}

private void endOfCompressedDataReached() throws IOException {
//With inflater, without knowing the compressed or uncompressed size, we over read necessary data
//In such cases, we have to push back the inputstream to the end of data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.lingala.zip4j.model.enums.AesVersion;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import net.lingala.zip4j.testutils.TestUtils;
import net.lingala.zip4j.util.InternalZipConstants;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -282,6 +283,18 @@ public void testAvailableReturns1WhenEntryEOFNotReachedAnd0AfterEOFReached() thr
}
}

@Test
public void testExtractZipWithDifferentPasswords() throws IOException {
byte[] buffer = new byte[InternalZipConstants.BUFF_SIZE];
File zipFileUnderTest = TestUtils.getTestArchiveFromResources("zip_with_different_passwords.zip");
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFileUnderTest))) {
readAndAssertNextEntry(zipInputStream, "after_deflate_remaining_bytes.bin", "password_1");
readAndAssertNextEntry(zipInputStream, "file_PDF_1MB.pdf", "password_2");
// 3rd entry is not encrypted, but it should not be impacted if a password is set
readAndAssertNextEntry(zipInputStream, "sample.pdf", null);
}
}

private void extractZipFileWithInputStreams(File zipFile, char[] password) throws IOException {
extractZipFileWithInputStreams(zipFile, password, 4096, AesVersion.TWO);
}
Expand Down Expand Up @@ -382,4 +395,16 @@ private File createZipFileWithJdkZip(List<File> filesToAdd, File rootFolder) thr
}
return generatedZipFile;
}

private void readAndAssertNextEntry(ZipInputStream zipInputStream, String fileNameToExpect, String passwordToUse)
throws IOException {
byte[] buffer = new byte[InternalZipConstants.BUFF_SIZE];
if (passwordToUse != null) {
zipInputStream.setPassword(passwordToUse.toCharArray());
}
LocalFileHeader localFileHeader = zipInputStream.getNextEntry();
assertThat(localFileHeader.getFileName()).isEqualTo(fileNameToExpect);
//noinspection StatementWithEmptyBody
while (zipInputStream.read(buffer) != -1);
}
}
Binary file not shown.

0 comments on commit 97679f8

Please sign in to comment.