note 75984 added to ref.zip

From: Date: Mon, 25 Jun 2007 17:59:09 +0000
Subject: note 75984 added to ref.zip
Groups: php.notes 
Request: Send a blank email to php-notes+get-127962@lists.php.net to get a copy of this message
I made a zip stream handler in case your distribution does not have the built in one using the new ZipArchive system. This one also features the ability to grab entries by index as well as by name. It is similar in capabilities to the builtin gzip/bzip2 compression stream handlers (http://us2.php.net/manual/en/wrappers.compression.php) except it does not support writing. To use: fopen('zip://absolute/path/to/file.zip?entryname', $mode) or fopen('zip://absolute/path/to/file.zip#entryindex', $mode) or fopen('zip://absolute/path/to/file.zip', $mode) $mode can only be 'r' or 'rb'. In the last case the first entry in the zip file is used. <?php class ZipStream { public $zip; //the zip file public $entry; //the opened zip entry public $length; //the uncompressed size of the zip entry public $position; //the current position in the zip entry read //Opens the zip file then retrieves and opens the entry to stream public function stream_open($path, $mode, $options, &$opened_path) { if ($mode != 'r' && $mode != 'rb') //only accept r and rb modes, no writing! return false; $path = 'file:///'.substr($path, 6); //switch out file:/// for zip:// so we can use url_parse $url = parse_url($path); //open the zip file $filename = $url['path']; $this->zip = zip_open($filename); if (!is_resource($this->zip)) return false; //if entry name is given, find that entry if (array_key_exists('query', $url) && $url['query']) { $path = $url['query']; do { $this->entry = zip_read($this->zip); if (!is_resource($this->entry)) return false; } while (zip_entry_name($this->entry) != $path); } else { //otherwise get it by index (default to 0) $id = 0; if (array_key_exists('fragment', $url) && is_int($url['fragment'])) $id = $url['fragment']*1; for ($i = 0; $i <= $id; $i++) { $this->entry = zip_read($this->zip); if (!is_resource($this->entry)) return false; } } //setup length and open the entry for reading $this->length = zip_entry_filesize($this->entry); $this->position = 0; zip_entry_open($this->zip, $this->entry, $mode); return true; } //Closes the zip entry and file public function stream_close() { @zip_entry_close($this->entry); @zip_close($this->zip); } //Returns how many bytes have been read from the zip entry public function stream_tell() { return $this->position; } //Returns true if the end of the zip entry has been reached public function stream_eof() { return $this->position >= $this->length; } //Returns the stat array, only 'size' is filled in with the uncompressed zip entry size public function url_stat() { return array('dev'=>0, 'ino'=>0, 'mode'=>0, 'nlink'=>0, 'uid'=>0, 'gid'=>0, 'rdev'=>0, 'size'=>$this->length, 'atime'=>0, 'mtime'=>0, 'ctime'=>0, 'blksize'=>0, 'blocks'=>0); } //Reads the next $count bytes or until the end of the zip entry. Returns the data or false if no data was read. public function stream_read($count) { $this->position += $count; if ($this->position > $this->length) $this->position = $this->length; return zip_entry_read($this->entry, $count); } } //Register the zip stream handler stream_wrapper_register('zip', 'ZipStream'); //if this fails there is already a zip stream handler and we will just use that one ?> ---- Server IP: 208.69.120.35 Probable Submitter: 128.113.8.72 ---- Manual Page -- http://www.php.net/manual/en/ref.zip.php Edit -- https://master.php.net/note/edit/75984 Del: integrated -- https://master.php.net/note/delete/75984/integrated Del: useless -- https://master.php.net/note/delete/75984/useless Del: bad code -- https://master.php.net/note/delete/75984/bad+code Del: spam -- https://master.php.net/note/delete/75984/spam Del: non-english -- https://master.php.net/note/delete/75984/non-english Del: in docs -- https://master.php.net/note/delete/75984/in+docs Del: other reasons-- https://master.php.net/note/delete/75984 Reject -- https://master.php.net/note/reject/75984 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#127962) next »