2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 * Custom stream to handle Tar files (compressed and uncompressed)
8 * LICENSE: This source file is subject to version 3.0 of the PHP license
9 * that is available through the world-wide-web at the following URI:
10 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
11 * the PHP License and are unable to obtain it through the web, please
12 * send a note to license@php.net so we can mail you a copy immediately.
14 * @package PHP_Beautifier
15 * @subpackage StreamWrapper
16 * @author Claudio Bustos <cdx@users.sourceforge.com>
17 * @copyright 2004-2006 Claudio Bustos
18 * @link http://pear.php.net/package/PHP_Beautifier
19 * @link http://beautifyphp.sourceforge.net
20 * @license http://www.php.net/license/3_0.txt PHP License 3.0
26 require_once 'Archive/Tar.php';
28 * Custom stream to handle Tar files (compressed and uncompressed)
29 * Use URL tarz://myfile.tgz#myfile.php
32 * @package PHP_Beautifier
33 * @subpackage StreamWrapper
34 * @author Claudio Bustos <cdx@users.sourceforge.com>
35 * @copyright 2004-2006 Claudio Bustos
36 * @link http://pear.php.net/package/PHP_Beautifier
37 * @link http://beautifyphp.sourceforge.net
38 * @license http://www.php.net/license/3_0.txt PHP License 3.0
39 * @version Release: 0.1.14
41 class PHP_Beautifier_StreamWrapper_Tarz
implements PHP_Beautifier_StreamWrapper_Interface
{
48 public $aContents = array();
49 function stream_open($sPath, $sMode, $iOptions, &$sOpenedPath)
51 if ($this->getTar($sPath)) {
53 $aContents = $this->oTar
->listContent();
54 if (array_filter($aContents, array($this, 'tarFileExists'))) {
55 $this->sText
= $this->oTar
->extractInString($this->sPath
);
62 function getTar($sPath)
64 if (preg_match("/tarz:\/\/(.*?(\.tgz|\.tar\.gz|\.tar\.bz2|\.tar)+)(?:#\/?(.*))*/", $sPath, $aMatch)) {
65 $this->sTar
= $aMatch[1];
66 if (strpos($aMatch[2], 'gz') !== FALSE) {
68 } elseif (strpos($aMatch[2], 'bz2') !== FALSE) {
70 } elseif (strpos($aMatch[2], 'tar') !== FALSE) {
75 if (isset($aMatch[3])) {
76 $this->sPath
= $aMatch[3];
78 if (file_exists($this->sTar
)) {
79 $this->oTar
= new Archive_Tar($this->sTar
, $sCompress);
86 function stream_close()
88 unset($this->oTar
, $this->sText
, $this->sPath
, $this->iSeek
);
90 function stream_read($iCount)
92 $sRet = substr($this->sText
, $this->iSeek
, $iCount);
93 $this->iSeek+
= strlen($sRet);
96 function stream_write($sData)
101 // BUG in 5.0.0RC1<PHP<5.0.0.0RC4
102 // DON'T USE EOF. Use ... another option :P
103 if (version_compare(PHP_VERSION
, '5.0.0.RC.1', ">=") and version_compare(PHP_VERSION
, '5.0.0.RC.4', "<")) {
104 return !($this->iSeek
>= strlen($this->sText
));
106 return $this->iSeek
>= strlen($this->sText
);
109 function stream_tell()
113 function stream_seek($iOffset, $iWhence)
117 if ($iOffset<strlen($this->sText
) and $iOffset >= 0) {
118 $this->iSeek
= $iOffset;
127 $this->iSeek+
= $iOffset;
135 if (strlen($this->sText
) +
$iOffset >= 0) {
136 $this->iSeek
= strlen($this->sText
) +
$iOffset;
147 function stream_flush()
150 function stream_stat()
153 function unlink($sPath)
156 function dir_opendir($sPath, $iOptions)
158 if ($this->getTar($sPath)) {
159 array_walk($this->oTar
->listContent() , array(
168 function dir_readdir()
170 if ($this->iSeekDir
>= count($this->aContents
)) {
173 return $this->aContents
[$this->iSeekDir++
];
176 function dir_rewinddir()
180 function dir_closedir()
182 //unset($this->oTar, $this->aContents, $this->sPath, $this->iSeekDir);
186 function getFileList($aInput)
188 $this->aContents
[] = $aInput['filename'];
190 function tarFileExists($aInput)
192 return ($aInput['filename'] == $this->sPath
and empty($aInput['typeflag']));
195 stream_wrapper_register("tarz", "PHP_Beautifier_StreamWrapper_Tarz");