PHP Class ZipStream\ZipStream

Streamed, dynamically generated zip archives.
Author: Paul Duncan ([email protected])
Author: Jonatan Männchen ([email protected])
Author: Jesse Donat ([email protected])
Show file Open project: maennchen/zipstream-php Class Usage Examples

Public Properties

Property Type Description
$cdr_ofs integer
$files array
$ofs integer
$opt array Global Options

Protected Properties

Property Type Description
$need_headers boolean
$output_name null | String

Public Methods

Method Description
__construct ( String $name = null, array $opt = [] ) Create a new ZipStream object.
addFile ( String $name, String $data, array $opt = [] ) addFile
addFileFromPath ( String $name, String $path, array $opt = [] ) : void addFileFromPath
addFileFromStream ( String $name, Resource $stream, array $opt = [] ) : void addFile_from_stream
finish ( ) : void finish

Protected Methods

Method Description
addCdr ( array $opt = null ) : void Add CDR (Central Directory Record) footer.
addCdrEof ( array $opt = null ) : void Send CDR EOF (Central Directory Record End-of-File) record.
addCdrFile ( array $args ) : void Send CDR record for specified file.
addDataDescriptorHeader ( integer $len, integer $zlen, String $crc ) : integer
addFileHeader ( String $name, Array &$opt, integer $meth, string $crc, integer $zlen, integer $len, Hex $genb ) : integer Create and send zip header for this file.
addLargeFile ( String $name, String $path, array $opt = [] ) : void Add a large file from the given path.
clear ( ) : void Clear all internal variables. Note that the stream object is not usable after this.
dostime ( integer $when ) : integer Convert a UNIX timestamp to a DOS timestamp.
isLargeFile ( string $path ) : boolean Is this file larger than large_file_size?
packFields ( array $fields ) : string Create a format string and argument list for pack(), then call pack() and return the result.
send ( String $str ) : void Send string, sending HTTP headers if necessary.
sendHttpHeaders ( ) : void Send HTTP headers for this stream.

Private Methods

Method Description
addToCdr ( String $name, Array $opt, integer $meth, string $crc, integer $zlen, integer $len, integer $rec_len, Hex $genb ) : integer Save file attributes for trailing CDR record.

Method Details

__construct() public method

Parameters:
public __construct ( String $name = null, array $opt = [] )
$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.

addCdr() protected method

Add CDR (Central Directory Record) footer.
protected addCdr ( array $opt = null ) : void
$opt array
return void

addCdrEof() protected method

Send CDR EOF (Central Directory Record End-of-File) record.
protected addCdrEof ( array $opt = null ) : void
$opt array
return void

addCdrFile() protected method

Send CDR record for specified file.
protected addCdrFile ( array $args ) : void
$args array
return void

addDataDescriptorHeader() protected method

protected addDataDescriptorHeader ( integer $len, integer $zlen, String $crc ) : integer
$len integer
$zlen integer
$crc String
return integer $num_bytes_written. Num bytes written to zip stream output.

addFile() public method

add a file to the archive
public addFile ( String $name, String $data, array $opt = [] )
$name String - path of file in archive (including directory). @param String $data - contents of file @param array $opt - 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', ));
$data String
$opt array

addFileFromPath() public method

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.
public addFileFromPath ( String $name, String $path, array $opt = [] ) : void
$name String - name of file in archive (including directory path). @param String $path - path to file on disk (note: paths should be encoded using UNIX-style forward slashes -- e.g '/path/to/some/file'). @param array $opt - 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', ));
$path String
$opt array
return void

addFileFromStream() public method

dds an open stream to the archive uncompressed
public addFileFromStream ( String $name, Resource $stream, array $opt = [] ) : void
$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);
return void

addFileHeader() protected method

Create and send zip header for this file.
protected addFileHeader ( String $name, Array &$opt, integer $meth, string $crc, integer $zlen, integer $len, Hex $genb ) : integer
$name String
$opt Array
$meth integer
$crc string
$zlen integer
$len integer
$genb Hex
return integer $num_bytes_written

addLargeFile() protected method

Add a large file from the given path.
protected addLargeFile ( String $name, String $path, array $opt = [] ) : void
$name String
$path String
$opt array
return void

clear() protected method

Clear all internal variables. Note that the stream object is not usable after this.
protected clear ( ) : void
return void

dostime() final protected method

Convert a UNIX timestamp to a DOS timestamp.
final protected dostime ( integer $when ) : integer
$when integer
return integer DOS Timestamp

finish() public method

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();
public finish ( ) : void
return void

isLargeFile() protected method

Is this file larger than large_file_size?
protected isLargeFile ( string $path ) : boolean
$path string
return boolean

packFields() protected method

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

send() protected method

Send string, sending HTTP headers if necessary.
protected send ( String $str ) : void
$str String
return void

sendHttpHeaders() protected method

Send HTTP headers for this stream.
protected sendHttpHeaders ( ) : void
return void

Property Details

$cdr_ofs public property

public int $cdr_ofs
return integer

$files public property

public array $files
return array

$need_headers protected property

protected bool $need_headers
return boolean

$ofs public property

public int $ofs
return integer

$opt public property

Global Options
public array $opt
return array

$output_name protected property

protected null|String $output_name
return null | String