💾 ZipStream-PHP

ZipStream
in package

Streamed, dynamically generated zip archives.

Usage

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

  1. Create the zip stream:
$zip = new ZipStream(outputName: 'example.zip');
  1. Add one or more files to the archive:
// add first file
$zip->addFile(fileName: 'world.txt', data: 'Hello World');

// add second file
$zip->addFile(fileName: 'moon.txt', data: 'Hello Moon');
  1. 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(outputName: '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->addFileFormPath(fileName: $path, $path);

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

Table of Contents

__construct()  : self
Create a new ZipStream object.
addDirectory()  : void
Add a directory to the archive.
addFile()  : void
Add a file to the archive.
addFileFromCallback()  : void
Add a file based on a callback.
addFileFromPath()  : void
Add a file at path to the archive.
addFileFromPsr7Stream()  : void
Add an open stream to the archive.
addFileFromStream()  : void
Add an open stream (resource) to the archive.
executeSimulation()  : void
Executes a previously calculated simulation.
finish()  : int
Write zip footer to stream.

Methods

__construct()

Create a new ZipStream object.

public __construct([OperationMode $operationMode = OperationMode::NORMAL ][, string $comment = '' ][, StreamInterface|resource|null $outputStream = null ][, CompressionMethod $defaultCompressionMethod = CompressionMethod::DEFLATE ][, int $defaultDeflateLevel = 6 ][, bool $enableZip64 = true ][, bool $defaultEnableZeroHeader = true ][, bool $sendHttpHeaders = true ][, Closure|null $httpHeaderCallback = null ][, string|null $outputName = null ][, string $contentDisposition = 'attachment' ][, string $contentType = 'application/x-zip' ][, bool $flushOutput = false ]) : self
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.',
);
Parameters
$operationMode : OperationMode = OperationMode::NORMAL

The mode can be used to switch between NORMAL and SIMULATION_* modes. For details see the OperationMode documentation.

Default to NORMAL.

$comment : string = ''

Archive Level Comment

$outputStream : StreamInterface|resource|null = null

Override the output of the archive to a different target.

By default the archive is sent to STDOUT.

$defaultCompressionMethod : CompressionMethod = CompressionMethod::DEFLATE

How 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.

$defaultDeflateLevel : int = 6

Default deflation level. Only relevant if compressionMethod is DEFLATE.

See details of deflate_init

$enableZip64 : bool = true

Enable Zip64 extension, supporting very large archives (any size > 4 GB or file count > 64k)

$defaultEnableZeroHeader : bool = true

Enable 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.

$sendHttpHeaders : bool = true

Boolean indicating whether or not to send the HTTP headers for this file.

$httpHeaderCallback : Closure|null = null

The method called to send HTTP headers

$outputName : string|null = null

The name of the created archive.

Only relevant if $sendHttpHeaders = true.

$contentDisposition : string = 'attachment'

HTTP Content-Disposition

Only relevant if sendHttpHeaders = true.

$contentType : string = 'application/x-zip'

HTTP Content Type

Only relevant if sendHttpHeaders = true.

$flushOutput : bool = false

Enable flush after every write to output stream.

Return values
self

addDirectory()

Add a directory to the archive.

public addDirectory(string $fileName[, string $comment = '' ][, DateTimeInterface|null $lastModificationDateTime = null ]) : void
File Options

See addFileFromPsr7Stream()

Examples
// add a directory named 'world/'
$zip->addFile(fileName: 'world/');
Parameters
$fileName : string
$comment : string = ''
$lastModificationDateTime : DateTimeInterface|null = null
Return values
void

addFile()

Add a file to the archive.

public addFile(string $fileName, string $data[, string $comment = '' ][, CompressionMethod|null $compressionMethod = null ][, int|null $deflateLevel = null ][, DateTimeInterface|null $lastModificationDateTime = null ][, int|null $maxSize = null ][, int|null $exactSize = null ][, bool|null $enableZeroHeader = null ]) : void
File Options

See 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
$fileName : string
$data : string

contents of file

$comment : string = ''
$compressionMethod : CompressionMethod|null = null
$deflateLevel : int|null = null
$lastModificationDateTime : DateTimeInterface|null = null
$maxSize : int|null = null
$exactSize : int|null = null
$enableZeroHeader : bool|null = null
Return values
void

addFileFromCallback()

Add a file based on a callback.

public addFileFromCallback(string $fileName, Closure $callback[, string $comment = '' ][, CompressionMethod|null $compressionMethod = null ][, int|null $deflateLevel = null ][, DateTimeInterface|null $lastModificationDateTime = null ][, int|null $maxSize = null ][, int|null $exactSize = null ][, bool|null $enableZeroHeader = null ]) : void

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();
    }
  );
}
Parameters
$fileName : string

path of file in archive (including directory)

$callback : Closure
$comment : string = ''

ZIP comment for this file

$compressionMethod : CompressionMethod|null = null

Override defaultCompressionMethod

See __construct()

$deflateLevel : int|null = null

Override defaultDeflateLevel

See __construct()

$lastModificationDateTime : DateTimeInterface|null = null

Set last modification time of file.

Default: now

$maxSize : int|null = null

Only read maxSize bytes from file.

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

$exactSize : int|null = null

Read 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.

$enableZeroHeader : bool|null = null

Override defaultEnableZeroHeader

See __construct()

Tags
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.

Return values
void

addFileFromPath()

Add a file at path to the archive.

public addFileFromPath(string $fileName, string $path[, string $comment = '' ][, CompressionMethod|null $compressionMethod = null ][, int|null $deflateLevel = null ][, DateTimeInterface|null $lastModificationDateTime = null ][, int|null $maxSize = null ][, int|null $exactSize = null ][, bool|null $enableZeroHeader = null ]) : void
File Options

See 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'),
);
Parameters
$fileName : string
$path : string
$comment : string = ''
$compressionMethod : CompressionMethod|null = null
$deflateLevel : int|null = null
$lastModificationDateTime : DateTimeInterface|null = null
$maxSize : int|null = null
$exactSize : int|null = null
$enableZeroHeader : bool|null = null
Tags
throws
FileNotFoundException
throws
FileNotReadableException
Return values
void

addFileFromPsr7Stream()

Add an open stream to the archive.

public addFileFromPsr7Stream(string $fileName, StreamInterface $stream[, string $comment = '' ][, CompressionMethod|null $compressionMethod = null ][, int|null $deflateLevel = null ][, DateTimeInterface|null $lastModificationDateTime = null ][, int|null $maxSize = null ][, int|null $exactSize = null ][, bool|null $enableZeroHeader = null ]) : void
Examples
$stream = $response->getBody();
// add a file named 'streamfile.txt' from the content of the stream
$archive->addFileFromPsr7Stream(
  fileName: 'streamfile.txt',
  stream: $stream,
);
Parameters
$fileName : string

path of file in archive (including directory)

$stream : StreamInterface

contents of file as a stream resource

$comment : string = ''

ZIP comment for this file

$compressionMethod : CompressionMethod|null = null

Override defaultCompressionMethod

See __construct()

$deflateLevel : int|null = null

Override defaultDeflateLevel

See __construct()

$lastModificationDateTime : DateTimeInterface|null = null

Set last modification time of file.

Default: now

$maxSize : int|null = null

Only read maxSize bytes from file.

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

$exactSize : int|null = null

Read 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.

$enableZeroHeader : bool|null = null

Override defaultEnableZeroHeader

See __construct()

Return values
void

addFileFromStream()

Add an open stream (resource) to the archive.

public addFileFromStream(string $fileName, resource $stream[, string $comment = '' ][, CompressionMethod|null $compressionMethod = null ][, int|null $deflateLevel = null ][, DateTimeInterface|null $lastModificationDateTime = null ][, int|null $maxSize = null ][, int|null $exactSize = null ][, bool|null $enableZeroHeader = null ]) : void
File Options

See 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
$fileName : string
$stream : resource

contents of file as a stream resource

$comment : string = ''
$compressionMethod : CompressionMethod|null = null
$deflateLevel : int|null = null
$lastModificationDateTime : DateTimeInterface|null = null
$maxSize : int|null = null
$exactSize : int|null = null
$enableZeroHeader : bool|null = null
Return values
void

executeSimulation()

Executes a previously calculated simulation.

public executeSimulation() : void
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();
Return values
void

finish()

Write zip footer to stream.

public finish() : int

The clase is left in an unusable state after finish.

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

        

Search results