Skip to content

Commit

Permalink
Merge pull request #16 from digitaldogsbody/issue-12
Browse files Browse the repository at this point in the history
Minimise extra data retrieved when requesting a file stream
  • Loading branch information
digitaldogsbody authored Oct 5, 2022
2 parents f63c409 + 25913a4 commit a385c6e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ZipRangeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ private function populateFiles()
$this->files[$file_name] = array(
"file_name" => $file_name,
"offset" => $unpacked["offset"],
"compression" => $unpacked["compression"],
"compressed_size" => $unpacked["csize"],
"uncompressed_size" => $unpacked["usize"],
"CRC32" => $this->correctCRC($unpacked["crc"]),
Expand Down Expand Up @@ -266,15 +267,14 @@ public function getStream(string $path) {
throw new FileNotFound("File " . $path . " not found in the zip file.");
}

// Retrieve the Local File Header
$header = $this->seeker->retrieveStart(30, $file["offset"]);
$file["local_header"] = $this->readLocalHeader($header);
// Retrieve the length of the filename and extra field from the Local File Header
$lfh_field_lengths = unpack("v2", $this->seeker->retrieveStart(30, $file["offset"]+26));

// Calculate the offset of the compressed data (File offset + LFH length + file name length + extra field length)
$data_offset = $file["offset"] + 30 + $file["local_header"]["fnlength"] + $file["local_header"]["eflength"];
$data_offset = $file["offset"] + 30 + $lfh_field_lengths[1] + $lfh_field_lengths[2];

// Return the file pointer to the user
return $this->seeker->getStream($file["compressed_size"], $data_offset, $file["local_header"]["compression"]);
return $this->seeker->getStream($file["compressed_size"], $data_offset, $file["compression"]);
}

/**
Expand Down

0 comments on commit a385c6e

Please sign in to comment.