Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
ZipStream\ZipStream Class Reference

Public Member Functions

 __construct (?string $name=null, ?ArchiveOptions $opt=null)
 Create a new ZipStream object.
 
 addFile (string $name, string $data, ?FileOptions $options=null)
 addFile
 
 addFileFromPath (string $name, string $path, ?FileOptions $options=null)
 addFileFromPath
 
 addFileFromPsr7Stream (string $name, StreamInterface $stream, ?FileOptions $options=null)
 addFileFromPsr7Stream
 
 addFileFromStream (string $name, $stream, ?FileOptions $options=null)
 addFileFromStream
 
 addToCdr (File $file)
 Save file attributes for trailing CDR record.
 
 finish ()
 finish
 
 isLargeFile (string $path)
 Is this file larger than large_file_size?
 
 send (string $str)
 Send string, sending HTTP headers if necessary.
 

Static Public Member Functions

static packFields (array $fields)
 Create a format string and argument list for pack(), then call pack() and return the result.
 

Public Attributes

Bigint $cdr_ofs
 
array $files = []
 
Bigint $ofs
 
ArchiveOptions $opt
 Global Options.
 
const CDR_EOF_SIGNATURE = 0x06054b50
 
const CDR_FILE_SIGNATURE = 0x02014b50
 
const DATA_DESCRIPTOR_SIGNATURE = 0x08074b50
 
const FILE_HEADER_SIGNATURE = 0x04034b50
 The following signatures end with 0x4b50, which in ASCII is PK, the initials of the inventor Phil Katz.
 
const ZIP64_CDR_EOF_SIGNATURE = 0x06064b50
 
const ZIP64_CDR_LOCATOR_SIGNATURE = 0x07064b50
 
const ZIP_VERSION_MADE_BY = 0x603
 This number corresponds to the ZIP version/OS used (2 bytes) From: https://www.iana.org/assignments/media-types/application/zip The upper byte (leftmost one) indicates the host system (OS) for the file.
 

Protected Member Functions

 addCdr64Eof ()
 Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.
 
 addCdr64Locator ()
 Send ZIP64 CDR Locator (Central Directory Record Locator) record.
 
 addCdrEof ()
 Send CDR EOF (Central Directory Record End-of-File) record.
 
 clear ()
 Clear all internal variables.
 
 sendHttpHeaders ()
 Send HTTP headers for this stream.
 

Protected Attributes

bool $need_headers
 
null String $output_name
 

Constructor & Destructor Documentation

◆ __construct()

ZipStream\ZipStream::__construct ( ?string $name = null,
?ArchiveOptions $opt = null )

Create a new ZipStream object.

Parameters:

Parameters
String$name- Name of output file (optional).
ArchiveOptions$opt- Archive Options

Large File Support:

By default, the method addFileFromPath() will send send files larger than 20 megabytes along raw rather than attempting to compress them. You can change both the maximum size and the compression behavior using the largeFile* options above, with the following caveats:

  • For "small" files (e.g. files smaller than largeFileSize), the memory use can be up to twice that of the actual file. In other words, adding a 10 megabyte file to the archive could potentially occupy 20 megabytes of memory.
  • Enabling compression on large files (e.g. files larger than large_file_size) is extremely slow, because ZipStream has to pass over the large file once to calculate header information, and then again to compress and send the actual data.

Examples:

// create a new zip file named 'foo.zip' $zip = new ZipStream('foo.zip');

// create a new zip file named 'bar.zip' with a comment $opt->setComment = 'this is a comment for the zip file.'; $zip = new ZipStream('bar.zip', $opt);

Notes:

In order to let this library send HTTP headers, a filename must be given and the option sendHttpHeaders must be true. This behavior is to allow software to send its own headers (including the filename), and still use this library.

Member Function Documentation

◆ addCdr64Eof()

ZipStream\ZipStream::addCdr64Eof ( )
protected

Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.

Return values
void

◆ addCdr64Locator()

ZipStream\ZipStream::addCdr64Locator ( )
protected

Send ZIP64 CDR Locator (Central Directory Record Locator) record.

Return values
void

◆ addCdrEof()

ZipStream\ZipStream::addCdrEof ( )
protected

Send CDR EOF (Central Directory Record End-of-File) record.

Return values
void

◆ addFile()

ZipStream\ZipStream::addFile ( string $name,
string $data,
?FileOptions $options = null )

addFile

Add a file to the archive.

Parameters
String$name- path of file in archive (including directory).
String$data- contents of file
FileOptions$options

File Options: time - Last-modified timestamp (seconds since the epoch) of this file. Defaults to the current time. comment - Comment related to this file. method - Storage method for file ("store" or "deflate")

Examples:

// add a file named 'foo.txt' $data = file_get_contents('foo.txt'); $zip->addFile('foo.txt', $data);

// add a file named 'bar.jpg' with a comment and a last-modified // time of two hours ago $data = file_get_contents('bar.jpg'); $opt->setTime = time() - 2 * 3600; $opt->setComment = 'this is a comment about bar.jpg'; $zip->addFile('bar.jpg', $data, $opt);

◆ addFileFromPath()

ZipStream\ZipStream::addFileFromPath ( string $name,
string $path,
?FileOptions $options = null )

addFileFromPath

Add a file at path to the archive.

Note that large files may be compressed differently than smaller files; see the "Large File Support" section above for more information.

Parameters
String$name- name of file in archive (including directory path).
String$path- path to file on disk (note: paths should be encoded using UNIX-style forward slashes – e.g '/path/to/some/file').
FileOptions$options

File Options: time - Last-modified timestamp (seconds since the epoch) of this file. Defaults to the current time. comment - Comment related to this file. method - Storage method for file ("store" or "deflate")

Examples:

// add a file named 'foo.txt' from the local file '/tmp/foo.txt' $zip->addFileFromPath('foo.txt', '/tmp/foo.txt');

// add a file named 'bigfile.rar' from the local file // '/usr/share/bigfile.rar' with a comment and a last-modified // time of two hours ago $path = '/usr/share/bigfile.rar'; $opt->setTime = time() - 2 * 3600; $opt->setComment = 'this is a comment about bar.jpg'; $zip->addFileFromPath('bigfile.rar', $path, $opt);

Return values
void
Exceptions
ZipStream\Exception\FileNotFoundException
ZipStream\Exception\FileNotReadableException

◆ addFileFromPsr7Stream()

ZipStream\ZipStream::addFileFromPsr7Stream ( string $name,
StreamInterface $stream,
?FileOptions $options = null )

addFileFromPsr7Stream

Add an open stream to the archive.

Parameters
String$name- path of file in archive (including directory).
StreamInterface$stream- contents of file as a stream resource
FileOptions$options

File Options: time - Last-modified timestamp (seconds since the epoch) of this file. Defaults to the current time. comment - Comment related to this file.

Examples:

$stream = $response->getBody(); // add a file named 'streamfile.txt' from the content of the stream $x->addFileFromPsr7Stream('streamfile.txt', $stream);

Return values
void

◆ addFileFromStream()

ZipStream\ZipStream::addFileFromStream ( string $name,
$stream,
?FileOptions $options = null )

addFileFromStream

Add an open stream to the archive.

Parameters
String$name- path of file in archive (including directory).
resource$stream- contents of file as a stream resource
FileOptions$options

File Options: time - Last-modified timestamp (seconds since the epoch) of this file. Defaults to the current time. comment - Comment related to this file.

Examples:

// create a temporary file stream and write text to it $fp = tmpfile(); fwrite($fp, 'The quick brown fox jumped over the lazy dog.');

// add a file named 'streamfile.txt' from the content of the stream $x->addFileFromStream('streamfile.txt', $fp);

Return values
void

◆ addToCdr()

ZipStream\ZipStream::addToCdr ( File $file)

Save file attributes for trailing CDR record.

Parameters
File$file
Return values
void

◆ clear()

ZipStream\ZipStream::clear ( )
protected

Clear all internal variables.

Note that the stream object is not usable after this.

Return values
void

◆ finish()

ZipStream\ZipStream::finish ( )

finish

Write zip footer to stream.

Example:

// add a list of files to the archive $files = array('foo.txt', 'bar.jpg'); foreach ($files as $path) $zip->addFile($path, file_get_contents($path));

// write footer to stream $zip->finish();

Return values
void
Exceptions
OverflowException

◆ isLargeFile()

ZipStream\ZipStream::isLargeFile ( string $path)

Is this file larger than large_file_size?

Parameters
string$path
Return values
bool

◆ packFields()

static ZipStream\ZipStream::packFields ( array $fields)
static

Create a format string and argument list for pack(), then call pack() and return the result.

Parameters
array$fields
Return values
string

◆ send()

ZipStream\ZipStream::send ( string $str)

Send string, sending HTTP headers if necessary.

Flush output after write if configure option is set.

Parameters
String$str
Return values
void

◆ sendHttpHeaders()

ZipStream\ZipStream::sendHttpHeaders ( )
protected

Send HTTP headers for this stream.

Return values
void

Member Data Documentation

◆ FILE_HEADER_SIGNATURE

const ZipStream\ZipStream::FILE_HEADER_SIGNATURE = 0x04034b50

The following signatures end with 0x4b50, which in ASCII is PK, the initials of the inventor Phil Katz.

See https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers

◆ ZIP_VERSION_MADE_BY

const ZipStream\ZipStream::ZIP_VERSION_MADE_BY = 0x603

This number corresponds to the ZIP version/OS used (2 bytes) From: https://www.iana.org/assignments/media-types/application/zip The upper byte (leftmost one) indicates the host system (OS) for the file.

Software can use this information to determine the line record format for text files etc. The current mappings are:

0 - MS-DOS and OS/2 (F.A.T. file systems) 1 - Amiga 2 - VAX/VMS 3 - *nix 4 - VM/CMS 5 - Atari ST 6 - OS/2 H.P.F.S. 7 - Macintosh 8 - Z-System 9 - CP/M 10 thru 255 - unused

The lower byte (rightmost one) indicates the version number of the software used to encode the file. The value/10 indicates the major version number, and the value mod 10 is the minor version number. Here we are using 6 for the OS, indicating OS/2 H.P.F.S. to prevent file permissions issues upon extract (see #84) 0x603 is 00000110 00000011 in binary, so 6 and 3


The documentation for this class was generated from the following file: