Documentation

ZipRangeReader
in package

Provides a utility to parse the directory records from a Zip file

Tags

Table of Contents

CDFH_SIG  = 0x2014b50
Central Directory File Header signature
EOCD_LENGTH  = 22
EOCD length in bytes
EOCD_SIG  = 0x6054b50
End of Central Directory signature
LFH_SIG  = 0x4034b50
Local File Header signature
MAX_EXTRA  = 65535
Maximum extra bytes at the end of a file after the EOCD
$cdr_offset  : int
The byte offset of the first Central Directory Record from the start of the file.
$cdr_size  : int
The total size of all the Central Directory Records in bytes.
$cdr_total  : int
The number of Central Directory records expected.
$eocd_offset  : int
The byte offset of the EOCD from the end of the file. Expressed as a negative.
$files  : array<string|int, mixed>
The list of files inside the zip.
$seeker  : SeekerInterface
The Seeker to use.
$stream  : resource
The file handle of the Zip object.
__construct()  : mixed
Create a new ZipRangeReader object, attempt to parse the zip file and populate a list with file details.
getStream()  : resource
Return a file pointer to a decompressed byte stream of the file at `$path`.
correctCRC()  : string
Correct the CRC32 values read from the Central Directory Records.
findEOCD()  : string
Find the End of Central Directory record.
populateFiles()  : void
Read the Central Directory Records and populate an array of information for each file in the ZIP.
readLocalHeader()  : array<string|int, mixed>
Decode a Local File Header and return it as an array of data.
retrieveCDRInfo()  : bool
Read information about the Central Directory Records from the EOCD.

Constants

CDFH_SIG

Central Directory File Header signature

public mixed CDFH_SIG = 0x2014b50
Tags

EOCD_LENGTH

EOCD length in bytes

public mixed EOCD_LENGTH = 22
Tags

EOCD_SIG

End of Central Directory signature

public mixed EOCD_SIG = 0x6054b50
Tags

LFH_SIG

Local File Header signature

public mixed LFH_SIG = 0x4034b50
Tags

MAX_EXTRA

Maximum extra bytes at the end of a file after the EOCD

public mixed MAX_EXTRA = 65535
Tags

Properties

$cdr_offset

The byte offset of the first Central Directory Record from the start of the file.

public int $cdr_offset
Tags

$cdr_size

The total size of all the Central Directory Records in bytes.

public int $cdr_size
Tags

$cdr_total

The number of Central Directory records expected.

public int $cdr_total
Tags

$eocd_offset

The byte offset of the EOCD from the end of the file. Expressed as a negative.

public int $eocd_offset
Tags

$files

The list of files inside the zip.

public array<string|int, mixed> $files
Tags

$stream

The file handle of the Zip object.

protected resource $stream
Tags

Methods

__construct()

Create a new ZipRangeReader object, attempt to parse the zip file and populate a list with file details.

public __construct(SeekerInterface $seeker) : mixed
Parameters
$seeker : SeekerInterface

The Seeker instance providing data.

Tags
throws
InvalidZipFile

Thrown if the Zip file is invalid.

throws
NoData

Thrown if the Seeker provides a null response

Return values
mixed

getStream()

Return a file pointer to a decompressed byte stream of the file at `$path`.

public getStream(string $path) : resource
Parameters
$path : string

The file path within the zip.

Tags
throws
FileNotFound

Thrown if the requested file path does not exist in the zip.

Return values
resource

A file pointer to the decompressed byte stream.

correctCRC()

Correct the CRC32 values read from the Central Directory Records.

private correctCRC(string $crc) : string

Due to the Zip format's little-endianness, and the nature of the parsing using unpack, we end up with the hexadecimal byte values in reverse (as the H unpack parameter only ensures each individual byte is correctly read as little-endian). This function simply splits the resultant CRC strings into byte pairs, reverses the order and rejoins them, additionally uppercasing the result for convention's sake.

Parameters
$crc : string

The CRC read from the CDR.

Tags
Return values
string

The corrected CRC value.

findEOCD()

Find the End of Central Directory record.

private findEOCD() : string

This should be the last data structure in the file, with an optional comment afterwards. Therefore we seek back from the end of the file, starting at the default size of an EOCD, and look for the magic 4 byte signature of the EOCD record. If not found, walk back 1 byte at a time up to the maximum permitted offset until either the EOCD is found, the max offset is reached, or we run out of data from the Seeker.

Tags
throws
NoData

Thrown if the Seeker returns a null response

throws
InvalidZipFile

Thrown if the Zip file does not have an EOCD Record

Return values
string

The EOCD record

populateFiles()

Read the Central Directory Records and populate an array of information for each file in the ZIP.

private populateFiles() : void
Tags
throws
NoData

Thrown if the Seeker provides a null response

Return values
void

readLocalHeader()

Decode a Local File Header and return it as an array of data.

private readLocalHeader(string $header) : array<string|int, mixed>
Parameters
$header : string

The header data.

Tags
Return values
array<string|int, mixed>

An array of the information decoded from the header.

retrieveCDRInfo()

Read information about the Central Directory Records from the EOCD.

private retrieveCDRInfo(string $eocd) : bool
Parameters
$eocd : string

The EOCD record.

Tags
Return values
bool

true if the CDR information was parsed successfully, false otherwise.


        

Search results