ZipStream
Streamed, dynamically generated zip archives.
- Full name:
\ZipStream\ZipStream
Constant | Visibility | Type | Value |
---|---|---|---|
VERSION |
public | '0.3.0' | |
METHOD_STORE |
public | 'store' | |
METHOD_DEFLATE |
public | 'deflate' | |
COMPRESS |
public | 0x8 | |
NOCOMPRESS |
public | 0x0 | |
OPTION_LARGE_FILE_SIZE |
public | 'large_file_size' | |
OPTION_LARGE_FILE_METHOD |
public | 'large_file_method' | |
OPTION_SEND_HTTP_HEADERS |
public | 'send_http_headers' | |
OPTION_HTTP_HEADER_CALLBACK |
public | 'http_header_callback' | |
OPTION_OUTPUT_STREAM |
public | 'output_stream' | |
OPTION_CONTENT_TYPE |
public | 'content_type' | |
OPTION_CONTENT_DISPOSITION |
public | 'content_disposition' |
Global Options
public array $opt
public array $files
public int $cdr_ofs
public int $ofs
protected bool $need_headers
protected null|string $output_name
Create a new ZipStream object.
public __construct(string $name = null, array $opt = array()): mixed
Parameters:
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string | - Name of output file (optional). |
$opt |
array | - Hash of archive options (optional, see "Archive Options" below). Archive Options: comment - Comment for this archive. content_type - HTTP Content-Type. Defaults to 'application/x-zip'. content_disposition - HTTP Content-Disposition. Defaults to 'attachment; filename="FILENAME"', where FILENAME is the specified filename. large_file_size - Size, in bytes, of the largest file to try and load into memory (used by addFileFromPath()). Large files may also be compressed differently; see the 'large_file_method' option. large_file_method - How to handle large files. Legal values are 'store' (the default), or 'deflate'. Store sends the file raw and is significantly faster, while 'deflate' compresses the file and is much, much slower. Note that deflate must compress the file twice and extremely slow. sendHttpHeaders - Boolean indicating whether or not to send the HTTP headers for this file. Note that content_type and content_disposition do nothing if you are not sending HTTP headers. 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 large_file_* options above, with the following caveats: * For "small" files (e.g. files smaller than large_file_size), 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 occupty 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 $zip = new ZipStream('bar.zip', array( 'comment' => 'this is a comment for the zip file.', )); Notes: If you do not set a filename, then this library DOES NOT send HTTP headers by default. This behavior is to allow software to send its own headers (including the filename), and still use this library. |
addFileFromPath
public addFileFromPath(string $name, string $path, array $opt = array(), string $storage_method = "deflate"): void
add a file at path to the archive.
Note that large files may be compresed differently than smaller files; see the "Large File Support" section above for more information.
Parameters:
Parameter | Type | Description |
---|---|---|
$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'). |
$opt |
array | - Hash of options for file (optional, see "File Options" below). 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: // 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'; $zip->addFileFromPath('bigfile.rar', $path, array( 'time' => time() - 2 * 3600, 'comment' => 'this is a comment about bar.jpg', )); |
$storage_method |
string | - storage method for the file: either 'deflate' or 'store' |
Is this file larger than large_file_size?
protected isLargeFile(string $path): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$path |
string |
Add a large file from the given path.
protected addLargeFile(string $name, string $path, array $opt = array()): void
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string | |
$path |
string | |
$opt |
array |
Create and send zip header for this file.
protected addFileHeader(string $name, array& $opt, int $meth, string $crc, int $zlen, int $len, \ZipStream\Hex $genb = 0x0): int
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string | |
$opt |
array | |
$meth |
int | |
$crc |
string | |
$zlen |
int | |
$len |
int | |
$genb |
\ZipStream\Hex |
Return Value:
$num_bytes_written
Convert a UNIX timestamp to a DOS timestamp.
final protected dostime(int $when): int
- This method is final.
Parameters:
Parameter | Type | Description |
---|---|---|
$when |
int |
Return Value:
DOS Timestamp
Create a format string and argument list for pack(), then call pack() and return the result.
protected packFields(array $fields): string
Parameters:
Parameter | Type | Description |
---|---|---|
$fields |
array |
Send string, sending HTTP headers if necessary.
protected send(string $str): void
Parameters:
Parameter | Type | Description |
---|---|---|
$str |
string |
Send HTTP headers for this stream.
protected sendHttpHeaders(): void
Save file attributes for trailing CDR record.
private addToCdr(string $name, array $opt, int $meth, string $crc, int $zlen, int $len, int $rec_len, \ZipStream\Hex $genb = 0x0): int
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string | |
$opt |
array | |
$meth |
int | |
$crc |
string | |
$zlen |
int | |
$len |
int | |
$rec_len |
int | |
$genb |
\ZipStream\Hex |
Return Value:
$num_bytes_written
addFile
public addFile(string $name, string $data, array $opt = array(), string $storage_method = 'deflate'): mixed
add a file to the archive
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string | - path of file in archive (including directory). |
$data |
string | - contents of file |
$opt |
array | - Hash of options for file (optional, see "File Options" below). 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: // 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'); $zip->addFile('bar.jpg', $data, array( 'time' => time() - 2 * 3600, 'comment' => 'this is a comment about bar.jpg', )); |
$storage_method |
string | - storage method for file, could be "store" or "deflate" |
protected getStorageConstant(mixed $storage_method): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$storage_method |
mixed |
addFile_from_stream
public addFileFromStream(string $name, resource $stream, array $opt = array(), mixed $storage_method = self::METHOD_DEFLATE): void
dds an open stream to the archive uncompressed
Parameters:
Parameter | Type | Description |
---|---|---|
$name |
string | - path of file in archive (including directory). |
$stream |
resource | - contents of file as a stream resource |
$opt |
array | - Hash of options for file (optional, see "File Options" below). 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->addFile_from_stream('streamfile.txt', $fp); |
$storage_method |
mixed |
protected addDataDescriptorHeader(int $len, int $zlen, string $crc): int
Parameters:
Parameter | Type | Description |
---|---|---|
$len |
int | |
$zlen |
int | |
$crc |
string |
Return Value:
$num_bytes_written. Num bytes written to zip stream output.
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();
Add CDR (Central Directory Record) footer.
protected addCdr(array $opt = null): void
Parameters:
Parameter | Type | Description |
---|---|---|
$opt |
array |
Send CDR record for specified file.
protected addCdrFile(array $args): void
Parameters:
Parameter | Type | Description |
---|---|---|
$args |
array |
Send CDR EOF (Central Directory Record End-of-File) record.
protected addCdrEof(array $opt = null): void
Parameters:
Parameter | Type | Description |
---|---|---|
$opt |
array |
Clear all internal variables. Note that the stream object is not usable after this.
protected clear(): void