ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ZipStream\ZipStream Class Reference

ZipStream. More...

+ Collaboration diagram for ZipStream\ZipStream:

Public Member Functions

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

Static Public Member Functions

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

Data Fields

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. More...
 
const FILE_HEADER_SIGNATURE = 0x04034b50
 The following signatures end with 0x4b50, which in ASCII is PK, the initials of the inventor Phil Katz. More...
 
const CDR_FILE_SIGNATURE = 0x02014b50
 
const CDR_EOF_SIGNATURE = 0x06054b50
 
const DATA_DESCRIPTOR_SIGNATURE = 0x08074b50
 
const ZIP64_CDR_EOF_SIGNATURE = 0x06064b50
 
const ZIP64_CDR_LOCATOR_SIGNATURE = 0x07064b50
 
 $opt
 
 $files = []
 
 $cdr_ofs
 
 $ofs
 

Protected Member Functions

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

Protected Attributes

 $need_headers
 
 $output_name
 

Detailed Description

ZipStream.

Streamed, dynamically generated zip archives.

Usage:

Streaming zip archives is a simple, three-step process:

  1. Create the zip stream:

    $zip = new ZipStream('example.zip');

  2. Add one or more files to the archive:
    • add first file $data = file_get_contents('some_file.gif'); $zip->addFile('some_file.gif', $data);
    • add second file $data = file_get_contents('some_file.gif'); $zip->addFile('another_file.png', $data);
  3. Finish the zip stream:

    $zip->finish();

You can also add an archive comment, add comments to individual files, and adjust the timestamp of files. See the API documentation for each method below for additional information.

Example:

// create a new zip stream object $zip = new ZipStream('some_files.zip');

// list of local files $files = array('foo.txt', 'bar.jpg');

// read and add each file to the archive foreach ($files as $path) $zip->addFile($path, file_get_contents($path));

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

Definition at line 58 of file ZipStream.php.

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.

Definition at line 171 of file ZipStream.php.

References $name.

172  {
173  $this->opt = $opt ?: new ArchiveOptions();
174 
175  $this->output_name = $name;
176  $this->need_headers = $name && $this->opt->isSendHttpHeaders();
177 
178  $this->cdr_ofs = new Bigint();
179  $this->ofs = new Bigint();
180  }

Member Function Documentation

◆ addCdr64Eof()

ZipStream\ZipStream::addCdr64Eof ( )
protected

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

Returns
void

Definition at line 386 of file ZipStream.php.

References $ret, and ZipStream\Option\Version\ZIP64.

386  : void
387  {
388  $num_files = count($this->files);
389  $cdr_length = $this->cdr_ofs;
390  $cdr_offset = $this->ofs;
391 
392  $fields = [
393  ['V', static::ZIP64_CDR_EOF_SIGNATURE], // ZIP64 end of central file header signature
394  ['P', 44], // Length of data below this header (length of block - 12) = 44
395  ['v', static::ZIP_VERSION_MADE_BY], // Made by version
396  ['v', Version::ZIP64], // Extract by version
397  ['V', 0x00], // disk number
398  ['V', 0x00], // no of disks
399  ['P', $num_files], // no of entries on disk
400  ['P', $num_files], // no of entries in cdr
401  ['P', $cdr_length], // CDR size
402  ['P', $cdr_offset], // CDR offset
403  ];
404 
405  $ret = static::packFields($fields);
406  $this->send($ret);
407  }
send(string $str)
Send string, sending HTTP headers if necessary.
Definition: ZipStream.php:455
$ret
Definition: parser.php:6

◆ addCdr64Locator()

ZipStream\ZipStream::addCdr64Locator ( )
protected

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

Returns
void

Definition at line 514 of file ZipStream.php.

References $ret.

514  : void
515  {
516  $cdr_offset = $this->ofs->add($this->cdr_ofs);
517 
518  $fields = [
519  ['V', static::ZIP64_CDR_LOCATOR_SIGNATURE], // ZIP64 end of central file header signature
520  ['V', 0x00], // Disc number containing CDR64EOF
521  ['P', $cdr_offset], // CDR offset
522  ['V', 1], // Total number of disks
523  ];
524 
525  $ret = static::packFields($fields);
526  $this->send($ret);
527  }
send(string $str)
Send string, sending HTTP headers if necessary.
Definition: ZipStream.php:455
$ret
Definition: parser.php:6

◆ addCdrEof()

ZipStream\ZipStream::addCdrEof ( )
protected

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

Returns
void

Definition at line 534 of file ZipStream.php.

References $comment, and $ret.

534  : void
535  {
536  $num_files = count($this->files);
537  $cdr_length = $this->cdr_ofs;
538  $cdr_offset = $this->ofs;
539 
540  // grab comment (if specified)
541  $comment = $this->opt->getComment();
542 
543  $fields = [
544  ['V', static::CDR_EOF_SIGNATURE], // end of central file header signature
545  ['v', 0x00], // disk number
546  ['v', 0x00], // no of disks
547  ['v', min($num_files, 0xFFFF)], // no of entries on disk
548  ['v', min($num_files, 0xFFFF)], // no of entries in cdr
549  ['V', $cdr_length->getLowFF()], // CDR size
550  ['V', $cdr_offset->getLowFF()], // CDR offset
551  ['v', strlen($comment)], // Zip Comment size
552  ];
553 
554  $ret = static::packFields($fields) . $comment;
555  $this->send($ret);
556  }
send(string $str)
Send string, sending HTTP headers if necessary.
Definition: ZipStream.php:455
$comment
Definition: buildRTE.php:83
$ret
Definition: parser.php:6

◆ 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$optionsFile 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);

Definition at line 210 of file ZipStream.php.

References PHPMailer\PHPMailer\$options.

210  : void
211  {
212  $options = $options ?: new FileOptions();
213  $options->defaultTo($this->opt);
214 
215  $file = new File($this, $name, $options);
216  $file->processData($data);
217  }
$data
Definition: bench.php:6

◆ 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$optionsFile 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);

Returns
void
Exceptions

Definition at line 256 of file ZipStream.php.

References PHPMailer\PHPMailer\$options.

256  : void
257  {
258  $options = $options ?: new FileOptions();
259  $options->defaultTo($this->opt);
260 
261  $file = new File($this, $name, $options);
262  $file->processPath($path);
263  }
$path
Definition: aliased.php:25

◆ 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$optionsFile 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->addFileFromPsr7Stream('streamfile.txt', $fp);

Returns
void

Definition at line 324 of file ZipStream.php.

References PHPMailer\PHPMailer\$options.

328  : void {
329  $options = $options ?: new FileOptions();
330  $options->defaultTo($this->opt);
331 
332  $file = new File($this, $name, $options);
333  $file->processStream($stream);
334  }
$stream
PHP stream implementation.

◆ 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$optionsFile 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);

Returns
void

Definition at line 290 of file ZipStream.php.

References PHPMailer\PHPMailer\$options, and GuzzleHttp\Psr7\$stream.

290  : void
291  {
292  $options = $options ?: new FileOptions();
293  $options->defaultTo($this->opt);
294 
295  $file = new File($this, $name, $options);
296  $file->processStream(new DeflateStream($stream));
297  }
$stream
PHP stream implementation.

◆ addToCdr()

ZipStream\ZipStream::addToCdr ( File  $file)

Save file attributes for trailing CDR record.

Parameters
File$file
Returns
void

Definition at line 593 of file ZipStream.php.

References ZipStream\File\getCdrFile(), and ZipStream\File\getTotalLength().

593  : void
594  {
595  $file->ofs = $this->ofs;
596  $this->ofs = $this->ofs->add($file->getTotalLength());
597  $this->files[] = $file->getCdrFile();
598  }
+ Here is the call graph for this function:

◆ clear()

ZipStream\ZipStream::clear ( )
protected

Clear all internal variables.

Note that the stream object is not usable after this.

Returns
void

Definition at line 564 of file ZipStream.php.

564  : void
565  {
566  $this->files = [];
567  $this->ofs = new Bigint();
568  $this->cdr_ofs = new Bigint();
569  $this->opt = new ArchiveOptions();
570  }

◆ 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();

Returns
void
Exceptions
OverflowException

Definition at line 354 of file ZipStream.php.

References ZipStream\Bigint\init().

354  : void
355  {
356  // add trailing cdr file records
357  foreach ($this->files as $cdrFile) {
358  $this->send($cdrFile);
359  $this->cdr_ofs = $this->cdr_ofs->add(Bigint::init(strlen($cdrFile)));
360  }
361 
362  // Add 64bit headers (if applicable)
363  if (count($this->files) >= 0xFFFF ||
364  $this->cdr_ofs->isOver32() ||
365  $this->ofs->isOver32()) {
366  if (!$this->opt->isEnableZip64()) {
367  throw new OverflowException();
368  }
369 
370  $this->addCdr64Eof();
371  $this->addCdr64Locator();
372  }
373 
374  // add trailing cdr eof record
375  $this->addCdrEof();
376 
377  // The End
378  $this->clear();
379  }
static init(int $value=0)
Get an instance.
Definition: Bigint.php:47
send(string $str)
Send string, sending HTTP headers if necessary.
Definition: ZipStream.php:455
addCdrEof()
Send CDR EOF (Central Directory Record End-of-File) record.
Definition: ZipStream.php:534
clear()
Clear all internal variables.
Definition: ZipStream.php:564
addCdr64Locator()
Send ZIP64 CDR Locator (Central Directory Record Locator) record.
Definition: ZipStream.php:514
addCdr64Eof()
Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.
Definition: ZipStream.php:386
+ Here is the call graph for this function:

◆ isLargeFile()

ZipStream\ZipStream::isLargeFile ( string  $path)

Is this file larger than large_file_size?

Parameters
string$path
Returns
bool

Definition at line 578 of file ZipStream.php.

578  : bool
579  {
580  if (!$this->opt->isStatFiles()) {
581  return false;
582  }
583  $stat = stat($path);
584  return $stat['size'] > $this->opt->getLargeFileSize();
585  }
$path
Definition: aliased.php:25

◆ 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
Returns
string

Definition at line 416 of file ZipStream.php.

References $format.

416  : string
417  {
418  $fmt = '';
419  $args = [];
420 
421  // populate format string and argument list
422  foreach ($fields as [$format, $value]) {
423  if ($format === 'P') {
424  $fmt .= 'VV';
425  if ($value instanceof Bigint) {
426  $args[] = $value->getLow32();
427  $args[] = $value->getHigh32();
428  } else {
429  $args[] = $value;
430  $args[] = 0;
431  }
432  } else {
433  if ($value instanceof Bigint) {
434  $value = $value->getLow32();
435  }
436  $fmt .= $format;
437  $args[] = $value;
438  }
439  }
440 
441  // prepend format string to argument list
442  array_unshift($args, $fmt);
443 
444  // build output string from header and compressed data
445  return pack(...$args);
446  }
$format
Definition: metadata.php:141

◆ 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
Returns
void

Definition at line 455 of file ZipStream.php.

455  : void
456  {
457  if ($this->need_headers) {
458  $this->sendHttpHeaders();
459  }
460  $this->need_headers = false;
461 
462  fwrite($this->opt->getOutputStream(), $str);
463 
464  if ($this->opt->isFlushOutput()) {
465  // flush output buffer if it is on and flushable
466  $status = ob_get_status();
467  if (isset($status['flags']) && ($status['flags'] & PHP_OUTPUT_HANDLER_FLUSHABLE)) {
468  ob_flush();
469  }
470 
471  // Flush system buffers after flushing userspace output buffer
472  flush();
473  }
474  }
sendHttpHeaders()
Send HTTP headers for this stream.
Definition: ZipStream.php:481

◆ sendHttpHeaders()

ZipStream\ZipStream::sendHttpHeaders ( )
protected

Send HTTP headers for this stream.

Returns
void

Definition at line 481 of file ZipStream.php.

References $key.

481  : void
482  {
483  // grab content disposition
484  $disposition = $this->opt->getContentDisposition();
485 
486  if ($this->output_name) {
487  // Various different browsers dislike various characters here. Strip them all for safety.
488  $safe_output = trim(str_replace(['"', "'", '\\', ';', "\n", "\r"], '', $this->output_name));
489 
490  // Check if we need to UTF-8 encode the filename
491  $urlencoded = rawurlencode($safe_output);
492  $disposition .= "; filename*=UTF-8''{$urlencoded}";
493  }
494 
495  $headers = array(
496  'Content-Type' => $this->opt->getContentType(),
497  'Content-Disposition' => $disposition,
498  'Pragma' => 'public',
499  'Cache-Control' => 'public, must-revalidate',
500  'Content-Transfer-Encoding' => 'binary'
501  );
502 
503  $call = $this->opt->getHttpHeaderCallback();
504  foreach ($headers as $key => $val) {
505  $call("$key: $val");
506  }
507  }
$key
Definition: croninfo.php:18

Field Documentation

◆ $cdr_ofs

ZipStream\ZipStream::$cdr_ofs

Definition at line 112 of file ZipStream.php.

◆ $files

ZipStream\ZipStream::$files = []

Definition at line 107 of file ZipStream.php.

◆ $need_headers

ZipStream\ZipStream::$need_headers
protected

Definition at line 122 of file ZipStream.php.

◆ $ofs

ZipStream\ZipStream::$ofs

Definition at line 117 of file ZipStream.php.

◆ $opt

ZipStream\ZipStream::$opt

Definition at line 102 of file ZipStream.php.

◆ $output_name

ZipStream\ZipStream::$output_name
protected

Definition at line 127 of file ZipStream.php.

◆ CDR_EOF_SIGNATURE

const ZipStream\ZipStream::CDR_EOF_SIGNATURE = 0x06054b50

Definition at line 92 of file ZipStream.php.

◆ CDR_FILE_SIGNATURE

const ZipStream\ZipStream::CDR_FILE_SIGNATURE = 0x02014b50

Definition at line 91 of file ZipStream.php.

◆ DATA_DESCRIPTOR_SIGNATURE

const ZipStream\ZipStream::DATA_DESCRIPTOR_SIGNATURE = 0x08074b50

Definition at line 93 of file ZipStream.php.

◆ 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

Definition at line 90 of file ZipStream.php.

◆ ZIP64_CDR_EOF_SIGNATURE

const ZipStream\ZipStream::ZIP64_CDR_EOF_SIGNATURE = 0x06064b50

Definition at line 94 of file ZipStream.php.

◆ ZIP64_CDR_LOCATOR_SIGNATURE

const ZipStream\ZipStream::ZIP64_CDR_LOCATOR_SIGNATURE = 0x07064b50

Definition at line 95 of file ZipStream.php.

◆ 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

Definition at line 83 of file ZipStream.php.


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