Skip to content

Commit

Permalink
#171 Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Apr 10, 2020
1 parent f273759 commit a5db7b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
13 changes: 6 additions & 7 deletions src/main/java/net/lingala/zip4j/headers/HeaderReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,24 @@ public ZipModel readAllHeaders(RandomAccessFile zip4jRaf, Charset charset) throw
private EndOfCentralDirectoryRecord readEndOfCentralDirectoryRecord(RandomAccessFile zip4jRaf, RawIO rawIO, Charset charset)
throws IOException {

seekInCurrentPart(zip4jRaf, zip4jRaf.length() - ENDHDR);
long offsetEndOfCentralDirectory = zip4jRaf.length() - ENDHDR;
seekInCurrentPart(zip4jRaf, offsetEndOfCentralDirectory);
int headerSignature = rawIO.readIntLittleEndian(zip4jRaf);

EndOfCentralDirectoryRecord endOfCentralDirectoryRecord = new EndOfCentralDirectoryRecord();

if (headerSignature != HeaderSignature.END_OF_CENTRAL_DIRECTORY.getValue()) {
long offsetEndOfCentralDirectory = determineOffsetOfEndOfCentralDirectory(zip4jRaf);
offsetEndOfCentralDirectory = determineOffsetOfEndOfCentralDirectory(zip4jRaf);
zip4jRaf.seek(offsetEndOfCentralDirectory + 4); // 4 to ignore reading signature again
endOfCentralDirectoryRecord.setOffsetOfEndOfCentralDirectory(offsetEndOfCentralDirectory);
}

EndOfCentralDirectoryRecord endOfCentralDirectoryRecord = new EndOfCentralDirectoryRecord();
endOfCentralDirectoryRecord.setSignature(HeaderSignature.END_OF_CENTRAL_DIRECTORY);
endOfCentralDirectoryRecord.setNumberOfThisDisk(rawIO.readShortLittleEndian(zip4jRaf));
endOfCentralDirectoryRecord.setNumberOfThisDiskStartOfCentralDir(rawIO.readShortLittleEndian(zip4jRaf));
endOfCentralDirectoryRecord.setTotalNumberOfEntriesInCentralDirectoryOnThisDisk(
rawIO.readShortLittleEndian(zip4jRaf));
endOfCentralDirectoryRecord.setTotalNumberOfEntriesInCentralDirectory(rawIO.readShortLittleEndian(zip4jRaf));
endOfCentralDirectoryRecord.setSizeOfCentralDirectory(rawIO.readIntLittleEndian(zip4jRaf));
endOfCentralDirectoryRecord.setOffsetOfEndOfCentralDirectory(offsetEndOfCentralDirectory);

zip4jRaf.readFully(intBuff);
endOfCentralDirectoryRecord.setOffsetOfStartOfCentralDirectory(rawIO.readLongLittleEndian(intBuff, 0));
Expand Down Expand Up @@ -520,13 +520,12 @@ private void setFilePointerToReadZip64EndCentralDirLoc(RandomAccessFile zip4jRaf
long offsetEndOfCentralDirectoryRecord) throws IOException {
// Now the file pointer is at the end of signature of Central Dir Rec
// Seek back with the following values
// 4 -> end of central dir signature
// 4 -> total number of disks
// 8 -> relative offset of the zip64 end of central directory record
// 4 -> number of the disk with the start of the zip64 end of central directory
// 4 -> zip64 end of central dir locator signature
// Refer to Appnote for more information
seekInCurrentPart(zip4jRaf, offsetEndOfCentralDirectoryRecord - 4 - 4 - 8 - 4 - 4);
seekInCurrentPart(zip4jRaf, offsetEndOfCentralDirectoryRecord - 4 - 8 - 4 - 4);
}

public LocalFileHeader readLocalFileHeader(InputStream inputStream, Charset charset) throws IOException {
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/net/lingala/zip4j/ExtractZipFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,12 @@ public void testExtractZipFileWithCommentLengthGreaterThanZipFileLength() throws

@Test
public void testExtractZipFileWithEndOfCentralDirectoryNotAtExpectedPosition() throws IOException {
verifyZipFileByExtractingAllFiles(getTestArchiveFromResources("end_of_cen_dir_not_at_expected_position.zip"),
null, outputFolder, 24, false);
ZipFile zipFile = new ZipFile(getTestArchiveFromResources("end_of_cen_dir_not_at_expected_position.zip"));
zipFile.extractAll(outputFolder.getPath());
List<File> outputFiles = FileUtils.getFilesInDirectoryRecursive(outputFolder, true, true);

assertThat(outputFiles).hasSize(24);
assertThat(zipFile.getFileHeaders()).hasSize(19);
}

private void addFileToZip(ZipFile zipFile, String fileName, EncryptionMethod encryptionMethod, String password) throws ZipException {
Expand Down

0 comments on commit a5db7b4

Please sign in to comment.