Skip to content

Commit

Permalink
#194 Fix for reading jar files
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed May 27, 2020
1 parent 7be1b06 commit d5c5b41
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ public LocalFileHeader getNextEntry(FileHeader fileHeader) throws IOException {

if (fileHeader != null) {
localFileHeader.setCrc(fileHeader.getCrc());
localFileHeader.setCompressedSize(fileHeader.getCompressedSize());

// In case of jar files, directories' compressed size in file header is 2 and local file header is 0.
// The actual value of compressed size in file header should be 0.
// This is a workaround to ignore the comprressed size from file header if directory and is deflate and
// compressed sizes does not match
if (!(fileHeader.isDirectory()
&& fileHeader.getCompressionMethod().equals(CompressionMethod.DEFLATE)
&& fileHeader.getCompressedSize() != localFileHeader.getCompressedSize())) {
localFileHeader.setCompressedSize(fileHeader.getCompressedSize());
}

localFileHeader.setUncompressedSize(fileHeader.getUncompressedSize());
canSkipExtendedLocalFileHeader = true;
} else {
Expand Down Expand Up @@ -300,9 +310,14 @@ private int getEncryptionHeaderSize(LocalFileHeader localFileHeader) {
}

private void readUntilEndOfEntry() throws IOException {
if (localFileHeader.getCompressedSize() == 0) {
return;
}

if (endOfEntryBuffer == null) {
endOfEntryBuffer = new byte[512];
}

while (read(endOfEntryBuffer) != -1);
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/net/lingala/zip4j/ExtractZipFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,12 @@ public void testExtractFileHeaderExtractAllFilesIfFileHeaderIsDirectoryAndRename
ZipFileVerifier.verifyFileContent(TestUtils.getTestFileFromResources("öüäöäö/asöäööl"), outputFile);
}

@Test
public void testExtractJarFile() throws IOException {
ZipFile zipFile = new ZipFile(TestUtils.getTestArchiveFromResources("zip4j-for-testing.jar"));
zipFile.extractAll(outputFolder.getPath());
}

private void addFileToZip(ZipFile zipFile, String fileName, EncryptionMethod encryptionMethod, String password) throws ZipException {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setEncryptFiles(encryptionMethod != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ public void testGetFileNamesWithChineseCharset() throws IOException {
assertThat(filenameSet.contains(expactedFileName)).isTrue();
}

@Test
public void testExtractJarFile() throws IOException {
byte[] b = new byte[4096];
File jarFile = getTestArchiveFromResources("zip4j-for-testing.jar");
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(jarFile))) {
while (zipInputStream.getNextEntry() != null) {
zipInputStream.read(b);
}
}
}

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

0 comments on commit d5c5b41

Please sign in to comment.