Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
|
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 |
ZipStream\ZipStream::__construct | ( | ?string | $name = null, |
?ArchiveOptions | $opt = null ) |
Create a new ZipStream object.
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:
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.
|
protected |
Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.
void |
|
protected |
|
protected |
Send CDR EOF (Central Directory Record End-of-File) record.
void |
ZipStream\ZipStream::addFile | ( | string | $name, |
string | $data, | ||
?FileOptions | $options = null ) |
addFile
Add a file to the archive.
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);
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.
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);
void |
ZipStream\ZipStream::addFileFromPsr7Stream | ( | string | $name, |
StreamInterface | $stream, | ||
?FileOptions | $options = null ) |
addFileFromPsr7Stream
Add an open stream to the archive.
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);
void |
ZipStream\ZipStream::addFileFromStream | ( | string | $name, |
$stream, | |||
?FileOptions | $options = null ) |
addFileFromStream
Add an open stream to the archive.
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);
void |
ZipStream\ZipStream::addToCdr | ( | File | $file | ) |
|
protected |
Clear all internal variables.
Note that the stream object is not usable after this.
void |
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();
void |
OverflowException |
ZipStream\ZipStream::isLargeFile | ( | string | $path | ) |
Is this file larger than large_file_size?
string | $path |
bool |
|
static |
Create a format string and argument list for pack(), then call pack() and return the result.
array | $fields |
string |
ZipStream\ZipStream::send | ( | string | $str | ) |
Send string, sending HTTP headers if necessary.
Flush output after write if configure option is set.
String | $str |
void |
|
protected |
Send HTTP headers for this stream.
void |
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
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