Skip to content

Commit

Permalink
#290 Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed May 24, 2021
1 parent da55c58 commit 4e2f5b2
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions src/main/java/net/lingala/zip4j/headers/HeaderReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
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.util.InternalZipConstants;
import net.lingala.zip4j.util.RawIO;

import java.io.IOException;
Expand Down Expand Up @@ -74,11 +73,11 @@ public ZipModel readAllHeaders(RandomAccessFile zip4jRaf, Zip4jConfig zip4jConfi
zipModel = new ZipModel();

try {
zipModel.setEndOfCentralDirectoryRecord(
readEndOfCentralDirectoryRecord(zip4jRaf, rawIO, zip4jConfig));
zipModel.setEndOfCentralDirectoryRecord(readEndOfCentralDirectoryRecord(zip4jRaf, rawIO, zip4jConfig));
} catch (ZipException e) {
throw e;
} catch (IOException e) {
e.printStackTrace();
throw new ZipException("Zip headers not found. Probably not a zip file or a corrupted zip file", e);
}

Expand Down Expand Up @@ -108,10 +107,8 @@ public ZipModel readAllHeaders(RandomAccessFile zip4jRaf, Zip4jConfig zip4jConfi
private EndOfCentralDirectoryRecord readEndOfCentralDirectoryRecord(RandomAccessFile zip4jRaf, RawIO rawIO,
Zip4jConfig zip4jConfig) throws IOException {

long offsetEndOfCentralDirectory = determineOffsetOfEndOfCentralDirectory(zip4jRaf);
if (zip4jRaf.getFilePointer() != offsetEndOfCentralDirectory) {
zip4jRaf.seek(offsetEndOfCentralDirectory + 4); // 4 to ignore reading signature again
}
long offsetEndOfCentralDirectory = locateOffsetOfEndOfCentralDirectory(zip4jRaf);
seekInCurrentPart(zip4jRaf, offsetEndOfCentralDirectory + 4);

EndOfCentralDirectoryRecord endOfCentralDirectoryRecord = new EndOfCentralDirectoryRecord();
endOfCentralDirectoryRecord.setSignature(HeaderSignature.END_OF_CENTRAL_DIRECTORY);
Expand Down Expand Up @@ -696,35 +693,32 @@ private long getNumberOfEntriesInCentralDirectory(ZipModel zipModel) {
return zipModel.getEndOfCentralDirectoryRecord().getTotalNumberOfEntriesInCentralDirectory();
}

private long determineOffsetOfEndOfCentralDirectory(RandomAccessFile randomAccessFile) throws IOException {
private long locateOffsetOfEndOfCentralDirectory(RandomAccessFile randomAccessFile) throws IOException {
long zipFileSize = randomAccessFile.length();
if (zipFileSize < ENDHDR) {
throw new ZipException("Zip file size less than size of zip headers. Probably not a zip file.");
}

long currentFilePointer = zipFileSize - ENDHDR;
randomAccessFile.seek(currentFilePointer);
seekInCurrentPart(randomAccessFile, zipFileSize - ENDHDR);
if (rawIO.readIntLittleEndian(randomAccessFile) == HeaderSignature.END_OF_CENTRAL_DIRECTORY.getValue()) {
return currentFilePointer;
return zipFileSize - ENDHDR;
}

int readLength;
byte[] buff = new byte[InternalZipConstants.BUFF_SIZE];
long numberOfBytesToRead = zipFileSize < MAX_COMMENT_SIZE ? zipFileSize : MAX_COMMENT_SIZE;
return locateOffsetOfEndOfCentralDirectoryByReverseSeek(randomAccessFile);
}

do {
currentFilePointer -= buff.length;
randomAccessFile.seek(currentFilePointer);
readLength = randomAccessFile.read(buff);
private long locateOffsetOfEndOfCentralDirectoryByReverseSeek(RandomAccessFile randomAccessFile) throws IOException {
long currentFilePointer = randomAccessFile.length() - ENDHDR;
// reverse seek for a maximum of MAX_COMMENT_SIZE bytes
long numberOfBytesToRead = randomAccessFile.length() < MAX_COMMENT_SIZE ? randomAccessFile.length() : MAX_COMMENT_SIZE;

for (int i = readLength - 4; i > 0; i--) {
if (rawIO.readIntLittleEndian(buff, i) == HeaderSignature.END_OF_CENTRAL_DIRECTORY.getValue()) {
return currentFilePointer + i;
}
while (numberOfBytesToRead > 0 && currentFilePointer > 0){
seekInCurrentPart(randomAccessFile, --currentFilePointer);
if (rawIO.readIntLittleEndian(randomAccessFile) == HeaderSignature.END_OF_CENTRAL_DIRECTORY.getValue()) {
return currentFilePointer;
}

numberOfBytesToRead -= readLength;
} while (numberOfBytesToRead > 0);
numberOfBytesToRead--;
};

throw new ZipException("Zip headers not found. Probably not a zip file");
}
Expand Down

0 comments on commit 4e2f5b2

Please sign in to comment.