Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
ZipStream\ZipStream Class Reference

Public Member Functions

 __construct (private OperationMode $operationMode=OperationMode::NORMAL, private readonly string $comment='', $outputStream=null, private readonly CompressionMethod $defaultCompressionMethod=CompressionMethod::DEFLATE, private readonly int $defaultDeflateLevel=6, private readonly bool $enableZip64=true, private readonly bool $defaultEnableZeroHeader=true, private bool $sendHttpHeaders=true, ?Closure $httpHeaderCallback=null, private readonly ?string $outputName=null, private readonly string $contentDisposition='attachment', private readonly string $contentType='application/x-zip', private bool $flushOutput=false,)
 Create a new ZipStream object.
 
 addDirectory (string $fileName, string $comment='', ?DateTimeInterface $lastModificationDateTime=null,)
 Add a directory to the archive.
 
 addFile (string $fileName, string $data, string $comment='', ?CompressionMethod $compressionMethod=null, ?int $deflateLevel=null, ?DateTimeInterface $lastModificationDateTime=null, ?int $maxSize=null, ?int $exactSize=null, ?bool $enableZeroHeader=null,)
 Add a file to the archive.
 
 addFileFromCallback (string $fileName, Closure $callback, string $comment='', ?CompressionMethod $compressionMethod=null, ?int $deflateLevel=null, ?DateTimeInterface $lastModificationDateTime=null, ?int $maxSize=null, ?int $exactSize=null, ?bool $enableZeroHeader=null,)
 Add a file based on a callback.
 
 addFileFromPath (string $fileName, string $path, string $comment='', ?CompressionMethod $compressionMethod=null, ?int $deflateLevel=null, ?DateTimeInterface $lastModificationDateTime=null, ?int $maxSize=null, ?int $exactSize=null, ?bool $enableZeroHeader=null,)
 Add a file at path to the archive.
 
 addFileFromPsr7Stream (string $fileName, StreamInterface $stream, string $comment='', ?CompressionMethod $compressionMethod=null, ?int $deflateLevel=null, ?DateTimeInterface $lastModificationDateTime=null, ?int $maxSize=null, ?int $exactSize=null, ?bool $enableZeroHeader=null,)
 Add an open stream to the archive.
 
 addFileFromStream (string $fileName, $stream, string $comment='', ?CompressionMethod $compressionMethod=null, ?int $deflateLevel=null, ?DateTimeInterface $lastModificationDateTime=null, ?int $maxSize=null, ?int $exactSize=null, ?bool $enableZeroHeader=null,)
 Add an open stream (resource) to the archive.
 
 executeSimulation ()
 Executes a previously calculated simulation.
 
 finish ()
 Write zip footer to stream.
 

Public Attributes

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.
 

Constructor & Destructor Documentation

◆ __construct()

ZipStream\ZipStream::__construct ( private OperationMode $operationMode = OperationMode::NORMAL,
private readonly string $comment = '',
$outputStream = null,
private readonly CompressionMethod $defaultCompressionMethod = CompressionMethod::DEFLATE,
private readonly int $defaultDeflateLevel = 6,
private readonly bool $enableZip64 = true,
private readonly bool $defaultEnableZeroHeader = true,
private bool $sendHttpHeaders = true,
?Closure $httpHeaderCallback = null,
private readonly ?string $outputName = null,
private readonly string $contentDisposition = 'attachment',
private readonly string $contentType = 'application/x-zip',
private bool $flushOutput = false )

Create a new ZipStream object.

Examples
// create a new zip file named 'foo.zip'
$zip = new ZipStream(outputName: 'foo.zip');
// create a new zip file named 'bar.zip' with a comment
$zip = new ZipStream(
outputName: 'bar.zip',
comment: 'this is a comment for the zip file.',
);
Comment is helper class to add/delete comments anywhere in moodle.
Definition lib.php:34
 
Definition CentralDirectoryFileHeader.php:5
Parameters
OperationMode$operationModeThe mode can be used to switch between NORMAL and SIMULATION_* modes. For details see the OperationMode documentation.

Default to NORMAL.

Parameters
string$commentArchive Level Comment
StreamInterface | resource | null$outputStreamOverride the output of the archive to a different target.

By default the archive is sent to STDOUT.

Parameters
CompressionMethod$defaultCompressionMethodHow to handle file compression. Legal values are CompressionMethod\DEFLATE (the default), or CompressionMethod\STORE. STORE sends the file raw and is significantly faster, while DEFLATE compresses the file and is much, much slower.
int$defaultDeflateLevelDefault deflation level. Only relevant if compressionMethod is DEFLATE.

See details of deflate_init

Parameters
bool$enableZip64Enable Zip64 extension, supporting very large archives (any size > 4 GB or file count > 64k)
bool$defaultEnableZeroHeaderEnable streaming files with single read.

When the zero header is set, the file is streamed into the output and the size & checksum are added at the end of the file. This is the fastest method and uses the least memory. Unfortunately not all ZIP clients fully support this and can lead to clients reporting the generated ZIP files as corrupted in combination with other circumstances. (Zip64 enabled, using UTF8 in comments / names etc.)

When the zero header is not set, the length & checksum need to be defined before the file is actually added. To prevent loading all the data into memory, the data has to be read twice. If the data which is added is not seekable, this call will fail.

Parameters
bool$sendHttpHeadersBoolean indicating whether or not to send the HTTP headers for this file.
?Closure$httpHeaderCallbackThe method called to send HTTP headers
string | null$outputNameThe name of the created archive.

Only relevant if $sendHttpHeaders = true.

Parameters
string$contentDispositionHTTP Content-Disposition

Only relevant if sendHttpHeaders = true.

Parameters
string$contentTypeHTTP Content Type

Only relevant if sendHttpHeaders = true.

Parameters
bool$flushOutputEnable flush after every write to output stream.
Return values
self

Member Function Documentation

◆ addDirectory()

ZipStream\ZipStream::addDirectory ( string $fileName,
string $comment = '',
?DateTimeInterface $lastModificationDateTime = null )

Add a directory to the archive.

File Options

See {

See also
addFileFromPsr7Stream()}
Examples
// add a directory named 'world/'
$zip->addFile(fileName: 'world/');

◆ addFile()

ZipStream\ZipStream::addFile ( string $fileName,
string $data,
string $comment = '',
?CompressionMethod $compressionMethod = null,
?int $deflateLevel = null,
?DateTimeInterface $lastModificationDateTime = null,
?int $maxSize = null,
?int $exactSize = null,
?bool $enableZeroHeader = null )

Add a file to the archive.

File Options

See {

See also
addFileFromPsr7Stream()}
Examples
// add a file named 'world.txt'
$zip->addFile(fileName: 'world.txt', data: 'Hello World!');
// add a file named 'bar.jpg' with a comment and a last-modified
// time of two hours ago
$zip->addFile(
fileName: 'bar.jpg',
data: $data,
comment: 'this is a comment about bar.jpg',
lastModificationDateTime: new DateTime('2 hours ago'),
);
Parameters
string$data

contents of file

◆ addFileFromCallback()

ZipStream\ZipStream::addFileFromCallback ( string $fileName,
Closure $callback,
string $comment = '',
?CompressionMethod $compressionMethod = null,
?int $deflateLevel = null,
?DateTimeInterface $lastModificationDateTime = null,
?int $maxSize = null,
?int $exactSize = null,
?bool $enableZeroHeader = null )

Add a file based on a callback.

This is useful when you want to simulate a lot of files without keeping all of the file handles open at the same time.

Examples
foreach($files as $name => $size) {
$archive->addFileFromPsr7Stream(
fileName: 'streamfile.txt',
exactSize: $size,
callback: function() use($name): Psr::Http::Message::StreamInterface {
$response = download($name);
return $response->getBody();
}
);
}
Definition StreamInterface.php:13
Parameters
string$fileNamepath of file in archive (including directory)
Closure$callback@psalm-param Closure(): (resource|StreamInterface|string) $callback A callback to get the file contents in the shape of a PHP stream, a Psr StreamInterface implementation, or a string.
string$commentZIP comment for this file
?CompressionMethod$compressionMethodOverride defaultCompressionMethod

See {

See also
__construct()}
Parameters
?int$deflateLevelOverride defaultDeflateLevel

See {

See also
__construct()}
Parameters
?DateTimeInterface$lastModificationDateTimeSet last modification time of file.

Default: now

Parameters
?int$maxSizeOnly read maxSize bytes from file.

The file is considered done when either reaching EOF or the maxSize.

Parameters
?int$exactSizeRead exactly exactSize bytes from file. If EOF is reached before reading exactSize bytes, an error will be thrown. The parameter allows for faster size calculations if the stream does not support fstat size or is slow and otherwise known beforehand.
?bool$enableZeroHeaderOverride defaultEnableZeroHeader

See {

See also
__construct()}

◆ addFileFromPath()

ZipStream\ZipStream::addFileFromPath ( string $fileName,
string $path,
string $comment = '',
?CompressionMethod $compressionMethod = null,
?int $deflateLevel = null,
?DateTimeInterface $lastModificationDateTime = null,
?int $maxSize = null,
?int $exactSize = null,
?bool $enableZeroHeader = null )

Add a file at path to the archive.

File Options

See {

See also
addFileFromPsr7Stream()}
Examples
// add a file named 'foo.txt' from the local file '/tmp/foo.txt'
$zip->addFileFromPath(
fileName: 'foo.txt',
path: '/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
$zip->addFile(
fileName: 'bigfile.rar',
path: '/usr/share/bigfile.rar',
comment: 'this is a comment about bigfile.rar',
lastModificationDateTime: new DateTime('2 hours ago'),
);
Exceptions
ZipStream\Exception\FileNotFoundException
ZipStream\Exception\FileNotReadableException
Parameters
$fileNamename of file in archive (including directory path).
$pathpath to file on disk (note: paths should be encoded using UNIX-style forward slashes – e.g '/path/to/some/file').

◆ addFileFromPsr7Stream()

ZipStream\ZipStream::addFileFromPsr7Stream ( string $fileName,
StreamInterface $stream,
string $comment = '',
?CompressionMethod $compressionMethod = null,
?int $deflateLevel = null,
?DateTimeInterface $lastModificationDateTime = null,
?int $maxSize = null,
?int $exactSize = null,
?bool $enableZeroHeader = null )

Add an open stream to the archive.

Examples
$stream = $response->getBody();
// add a file named 'streamfile.txt' from the content of the stream
$archive->addFileFromPsr7Stream(
fileName: 'streamfile.txt',
stream: $stream,
);
Parameters
string$fileNamepath of file in archive (including directory)
StreamInterface$streamcontents of file as a stream resource
string$commentZIP comment for this file
?CompressionMethod$compressionMethodOverride defaultCompressionMethod

See {

See also
__construct()}
Parameters
?int$deflateLevelOverride defaultDeflateLevel

See {

See also
__construct()}
Parameters
?DateTimeInterface$lastModificationDateTimeSet last modification time of file.

Default: now

Parameters
?int$maxSizeOnly read maxSize bytes from file.

The file is considered done when either reaching EOF or the maxSize.

Parameters
?int$exactSizeRead exactly exactSize bytes from file. If EOF is reached before reading exactSize bytes, an error will be thrown. The parameter allows for faster size calculations if the stream does not support fstat size or is slow and otherwise known beforehand.
?bool$enableZeroHeaderOverride defaultEnableZeroHeader

See {

See also
__construct()}

◆ addFileFromStream()

ZipStream\ZipStream::addFileFromStream ( string $fileName,
$stream,
string $comment = '',
?CompressionMethod $compressionMethod = null,
?int $deflateLevel = null,
?DateTimeInterface $lastModificationDateTime = null,
?int $maxSize = null,
?int $exactSize = null,
?bool $enableZeroHeader = null )

Add an open stream (resource) to the archive.

File Options

See {

See also
addFileFromPsr7Stream()}
Examples
// create a temporary file stream and write text to it
$filePointer = tmpfile();
fwrite($filePointer, 'The quick brown fox jumped over the lazy dog.');
// add a file named 'streamfile.txt' from the content of the stream
$archive->addFileFromStream(
fileName: 'streamfile.txt',
stream: $filePointer,
);
Parameters
resource$streamcontents of file as a stream resource

◆ executeSimulation()

ZipStream\ZipStream::executeSimulation ( )

Executes a previously calculated simulation.

Example
$zip = new ZipStream(
outputName: 'foo.zip',
operationMode: OperationMode::SIMULATE_STRICT,
);
$zip->addFile('test.txt', 'Hello World');
$size = $zip->finish();
header('Content-Length: '. $size);
$zip->executeSimulation();
OperationMode
Definition OperationMode.php:11

◆ finish()

ZipStream\ZipStream::finish ( )

Write zip footer to stream.

The clase is left in an unusable state after finish.

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

Member Data Documentation

◆ 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: