📄 tar.php
字号:
$v_result = $this->_append($v_list, $p_add_dir, $p_remove_dir);
}
return $v_result;
}
// }}}
// {{{ addString()
/**
* This method add a single string as a file at the
* end of the existing archive. If the archive does not yet exists it
* is created.
*
* @param string $p_filename A string which contains the full
* filename path that will be associated
* with the string.
* @param string $p_string The content of the file added in
* the archive.
* @return true on success, false on error.
* @access public
*/
function addString($p_filename, $p_string)
{
$v_result = true;
if (!$this->_isArchive()) {
if (!$this->_openWrite()) {
return false;
}
$this->_close();
}
if (!$this->_openAppend())
return false;
// Need to check the get back to the temporary file ? ....
$v_result = $this->_addString($p_filename, $p_string);
$this->_writeFooter();
$this->_close();
return $v_result;
}
// }}}
// {{{ extractModify()
/**
* This method extract all the content of the archive in the directory
* indicated by $p_path. When relevant the memorized path of the
* files/dir can be modified by removing the $p_remove_path path at the
* beginning of the file/dir path.
* While extracting a file, if the directory path does not exists it is
* created.
* While extracting a file, if the file already exists it is replaced
* without looking for last modification date.
* While extracting a file, if the file already exists and is write
* protected, the extraction is aborted.
* While extracting a file, if a directory with the same name already
* exists, the extraction is aborted.
* While extracting a directory, if a file with the same name already
* exists, the extraction is aborted.
* While extracting a file/directory if the destination directory exist
* and is write protected, or does not exist but can not be created,
* the extraction is aborted.
* If after extraction an extracted file does not show the correct
* stored file size, the extraction is aborted.
* When the extraction is aborted, a PEAR error text is set and false
* is returned. However the result can be a partial extraction that may
* need to be manually cleaned.
*
* @param string $p_path The path of the directory where the
* files/dir need to by extracted.
* @param string $p_remove_path Part of the memorized path that can be
* removed if present at the beginning of
* the file/dir path.
* @return boolean true on success, false on error.
* @access public
* @see extractList()
*/
function extractModify($p_path, $p_remove_path)
{
$v_result = true;
$v_list_detail = array();
if ($v_result = $this->_openRead()) {
$v_result = $this->_extractList($p_path, $v_list_detail,
"complete", 0, $p_remove_path);
$this->_close();
}
return $v_result;
}
// }}}
// {{{ extractInString()
/**
* This method extract from the archive one file identified by $p_filename.
* The return value is a string with the file content, or NULL on error.
* @param string $p_filename The path of the file to extract in a string.
* @return a string with the file content or NULL.
* @access public
*/
function extractInString($p_filename)
{
if ($this->_openRead()) {
$v_result = $this->_extractInString($p_filename);
$this->_close();
} else {
$v_result = NULL;
}
return $v_result;
}
// }}}
// {{{ extractList()
/**
* This method extract from the archive only the files indicated in the
* $p_filelist. These files are extracted in the current directory or
* in the directory indicated by the optional $p_path parameter.
* If indicated the $p_remove_path can be used in the same way as it is
* used in extractModify() method.
* @param array $p_filelist An array of filenames and directory names,
* or a single string with names separated
* by a single blank space.
* @param string $p_path The path of the directory where the
* files/dir need to by extracted.
* @param string $p_remove_path Part of the memorized path that can be
* removed if present at the beginning of
* the file/dir path.
* @return true on success, false on error.
* @access public
* @see extractModify()
*/
function extractList($p_filelist, $p_path='', $p_remove_path='')
{
$v_result = true;
$v_list_detail = array();
if (is_array($p_filelist))
$v_list = $p_filelist;
elseif (is_string($p_filelist))
$v_list = explode($this->_separator, $p_filelist);
else {
$this->_error('Invalid string list');
return false;
}
if ($v_result = $this->_openRead()) {
$v_result = $this->_extractList($p_path, $v_list_detail, "partial",
$v_list, $p_remove_path);
$this->_close();
}
return $v_result;
}
// }}}
// {{{ setAttribute()
/**
* This method set specific attributes of the archive. It uses a variable
* list of parameters, in the format attribute code + attribute values :
* $arch->setAttribute(ARCHIVE_TAR_ATT_SEPARATOR, ',');
* @param mixed $argv variable list of attributes and values
* @return true on success, false on error.
* @access public
*/
function setAttribute()
{
$v_result = true;
// ----- Get the number of variable list of arguments
if (($v_size = func_num_args()) == 0) {
return true;
}
// ----- Get the arguments
$v_att_list = &func_get_args();
// ----- Read the attributes
$i=0;
while ($i<$v_size) {
// ----- Look for next option
switch ($v_att_list[$i]) {
// ----- Look for options that request a string value
case ARCHIVE_TAR_ATT_SEPARATOR :
// ----- Check the number of parameters
if (($i+1) >= $v_size) {
$this->_error('Invalid number of parameters for '
.'attribute ARCHIVE_TAR_ATT_SEPARATOR');
return false;
}
// ----- Get the value
$this->_separator = $v_att_list[$i+1];
$i++;
break;
default :
$this->_error('Unknow attribute code '.$v_att_list[$i].'');
return false;
}
// ----- Next attribute
$i++;
}
return $v_result;
}
// }}}
// {{{ _error()
function _error($p_message)
{
// ----- To be completed
$this->raiseError($p_message);
}
// }}}
// {{{ _warning()
function _warning($p_message)
{
// ----- To be completed
$this->raiseError($p_message);
}
// }}}
// {{{ _isArchive()
function _isArchive($p_filename=NULL)
{
if ($p_filename == NULL) {
$p_filename = $this->_tarname;
}
clearstatcache();
return @is_file($p_filename);
}
// }}}
// {{{ _openWrite()
function _openWrite()
{
if ($this->_compress_type == 'gz')
$this->_file = @gzopen($this->_tarname, "wb9");
else if ($this->_compress_type == 'bz2')
$this->_file = @bzopen($this->_tarname, "wb");
else if ($this->_compress_type == 'none')
$this->_file = @fopen($this->_tarname, "wb");
else
$this->_error('Unknown or missing compression type ('
.$this->_compress_type.')');
if ($this->_file == 0) {
$this->_error('Unable to open in write mode \''
.$this->_tarname.'\'');
return false;
}
return true;
}
// }}}
// {{{ _openRead()
function _openRead()
{
if (strtolower(substr($this->_tarname, 0, 7)) == 'http://') {
// ----- Look if a local copy need to be done
if ($this->_temp_tarname == '') {
$this->_temp_tarname = uniqid('tar').'.tmp';
if (!$v_file_from = @fopen($this->_tarname, 'rb')) {
$this->_error('Unable to open in read mode \''
.$this->_tarname.'\'');
$this->_temp_tarname = '';
return false;
}
if (!$v_file_to = @fopen($this->_temp_tarname, 'wb')) {
$this->_error('Unable to open in write mode \''
.$this->_temp_tarname.'\'');
$this->_temp_tarname = '';
return false;
}
while ($v_data = @fread($v_file_from, 1024))
@fwrite($v_file_to, $v_data);
@fclose($v_file_from);
@fclose($v_file_to);
}
// ----- File to open if the local copy
$v_filename = $this->_temp_tarname;
} else
// ----- File to open if the normal Tar file
$v_filename = $this->_tarname;
if ($this->_compress_type == 'gz')
$this->_file = @gzopen($v_filename, "rb");
else if ($this->_compress_type == 'bz2')
$this->_file = @bzopen($v_filename, "rb");
else if ($this->_compress_type == 'none')
$this->_file = @fopen($v_filename, "rb");
else
$this->_error('Unknown or missing compression type ('
.$this->_compress_type.')');
if ($this->_file == 0) {
$this->_error('Unable to open in read mode \''.$v_filename.'\'');
return false;
}
return true;
}
// }}}
// {{{ _openReadWrite()
function _openReadWrite()
{
if ($this->_compress_type == 'gz')
$this->_file = @gzopen($this->_tarname, "r+b");
else if ($this->_compress_type == 'bz2')
$this->_file = @bzopen($this->_tarname, "r+b");
else if ($this->_compress_type == 'none')
$this->_file = @fopen($this->_tarname, "r+b");
else
$this->_error('Unknown or missing compression type ('
.$this->_compress_type.')');
if ($this->_file == 0) {
$this->_error('Unable to open in read/write mode \''
.$this->_tarname.'\'');
return false;
}
return true;
}
// }}}
// {{{ _close()
function _close()
{
//if (isset($this->_file)) {
if (is_resource($this->_file)) {
if ($this->_compress_type == 'gz')
@gzclose($this->_file);
else if ($this->_compress_type == 'bz2')
@bzclose($this->_file);
else if ($this->_compress_type == 'none')
@fclose($this->_file);
else
$this->_error('Unknown or missing compression type ('
.$this->_compress_type.')');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -