ZipStream
in package
ZipStream
Streamed, dynamically generated zip archives.
Usage:
Streaming zip archives is a simple, three-step process:
-
Create the zip stream:
$zip = new ZipStream('example.zip');
-
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);
-
-
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();
Table of Contents
Constants
- CDR_EOF_SIGNATURE = 0x6054b50
- CDR_FILE_SIGNATURE = 0x2014b50
- DATA_DESCRIPTOR_SIGNATURE = 0x8074b50
- FILE_HEADER_SIGNATURE = 0x4034b50
- The following signatures end with 0x4b50, which in ASCII is PK, the initials of the inventor Phil Katz.
- ZIP64_CDR_EOF_SIGNATURE = 0x6064b50
- ZIP64_CDR_LOCATOR_SIGNATURE = 0x7064b50
- 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:
Properties
- $cdr_ofs : Bigint
- $files : array<string|int, mixed>
- $ofs : Bigint
- $opt : Archive
- Global Options
- $need_headers : bool
- $output_name : null|string
Methods
- __construct() : mixed
- Create a new ZipStream object.
- addFile() : void
- addFile
- addFileFromPath() : void
- addFileFromPath
- addFileFromPsr7Stream() : void
- addFileFromPsr7Stream
- addFileFromStream() : void
- addFileFromStream
- addToCdr() : void
- Save file attributes for trailing CDR record.
- finish() : void
- finish
- isLargeFile() : bool
- Is this file larger than large_file_size?
- packFields() : string
- Create a format string and argument list for pack(), then call pack() and return the result.
- send() : void
- Send string, sending HTTP headers if necessary.
- addCdr64Eof() : void
- Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.
- addCdr64Locator() : void
- Send ZIP64 CDR Locator (Central Directory Record Locator) record.
- addCdrEof() : void
- Send CDR EOF (Central Directory Record End-of-File) record.
- clear() : void
- Clear all internal variables. Note that the stream object is not usable after this.
- sendHttpHeaders() : void
- Send HTTP headers for this stream.
Constants
CDR_EOF_SIGNATURE
public
mixed
CDR_EOF_SIGNATURE
= 0x6054b50
CDR_FILE_SIGNATURE
public
mixed
CDR_FILE_SIGNATURE
= 0x2014b50
DATA_DESCRIPTOR_SIGNATURE
public
mixed
DATA_DESCRIPTOR_SIGNATURE
= 0x8074b50
FILE_HEADER_SIGNATURE
The following signatures end with 0x4b50, which in ASCII is PK, the initials of the inventor Phil Katz.
public
mixed
FILE_HEADER_SIGNATURE
= 0x4034b50
See https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers
ZIP64_CDR_EOF_SIGNATURE
public
mixed
ZIP64_CDR_EOF_SIGNATURE
= 0x6064b50
ZIP64_CDR_LOCATOR_SIGNATURE
public
mixed
ZIP64_CDR_LOCATOR_SIGNATURE
= 0x7064b50
ZIP_VERSION_MADE_BY
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:
public
mixed
ZIP_VERSION_MADE_BY
= 0x603
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
Properties
$cdr_ofs
public
Bigint
$cdr_ofs
$files
public
array<string|int, mixed>
$files
= []
$ofs
public
Bigint
$ofs
$opt
Global Options
public
Archive
$opt
$need_headers
protected
bool
$need_headers
$output_name
protected
null|string
$output_name
Methods
__construct()
Create a new ZipStream object.
public
__construct([string $name = null ][, Archive $opt = null ]) : mixed
Parameters:
Parameters
- $name : string = null
-
- Name of output file (optional).
- $opt : Archive = null
-
- 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 betrue
. This behavior is to allow software to send its own headers (including the filename), and still use this library.
addFile()
addFile
public
addFile(string $name, string $data[, File $options = null ]) : void
Add a file to the archive.
Parameters
- $name : string
-
- path of file in archive (including directory).
- $data : string
-
- contents of file
- $options : File = null
-
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()
addFileFromPath
public
addFileFromPath(string $name, string $path[, File $options = null ]) : void
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
- $name : string
-
- name of file in archive (including directory path).
- $path : string
-
- path to file on disk (note: paths should be encoded using UNIX-style forward slashes -- e.g '/path/to/some/file').
- $options : File = null
-
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);
Tags
addFileFromPsr7Stream()
addFileFromPsr7Stream
public
addFileFromPsr7Stream(string $name, StreamInterface $stream[, File $options = null ]) : void
Add an open stream to the archive.
Parameters
- $name : string
-
- path of file in archive (including directory).
- $stream : StreamInterface
-
- contents of file as a stream resource
- $options : File = null
-
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);
addFileFromStream()
addFileFromStream
public
addFileFromStream(string $name, resource $stream[, File $options = null ]) : void
Add an open stream to the archive.
Parameters
- $name : string
-
- path of file in archive (including directory).
- $stream : resource
-
- contents of file as a stream resource
- $options : File = null
-
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);
addToCdr()
Save file attributes for trailing CDR record.
public
addToCdr(File $file) : void
Parameters
- $file : File
finish()
finish
public
finish() : void
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();
Tags
isLargeFile()
Is this file larger than large_file_size?
public
isLargeFile(string $path) : bool
Parameters
- $path : string
Return values
boolpackFields()
Create a format string and argument list for pack(), then call pack() and return the result.
public
static packFields(array<string|int, mixed> $fields) : string
Parameters
- $fields : array<string|int, mixed>
Return values
stringsend()
Send string, sending HTTP headers if necessary.
public
send(string $str) : void
Flush output after write if configure option is set.
Parameters
- $str : string
addCdr64Eof()
Send ZIP64 CDR EOF (Central Directory Record End-of-File) record.
protected
addCdr64Eof() : void
addCdr64Locator()
Send ZIP64 CDR Locator (Central Directory Record Locator) record.
protected
addCdr64Locator() : void
addCdrEof()
Send CDR EOF (Central Directory Record End-of-File) record.
protected
addCdrEof() : void
clear()
Clear all internal variables. Note that the stream object is not usable after this.
protected
clear() : void
sendHttpHeaders()
Send HTTP headers for this stream.
protected
sendHttpHeaders() : void