?? tar.php
字號:
{
if (!$this->_readHeader($v_binary_data, $v_header))
return NULL;
if ($v_header['filename'] == '')
continue;
// ----- Look for long filename
if ($v_header['typeflag'] == 'L') {
if (!$this->_readLongHeader($v_header))
return NULL;
}
if ($v_header['filename'] == $p_filename) {
if ($v_header['typeflag'] == "5") {
$this->_error('Unable to extract in string a directory entry {'.$v_header['filename'].'}');
return NULL;
} else {
$n = floor($v_header['size']/512);
for ($i=0; $i<$n; $i++) {
$v_result_str .= $this->_readBlock();
}
if (($v_header['size'] % 512) != 0) {
$v_content = $this->_readBlock();
$v_result_str .= substr($v_content, 0, ($v_header['size'] % 512));
}
return $v_result_str;
}
} else {
$this->_jumpBlock(ceil(($v_header['size']/512)));
}
}
return NULL;
}
// }}}
// {{{ _extractList()
function _extractList($p_path, &$p_list_detail, $p_mode, $p_file_list, $p_remove_path)
{
$v_result=true;
$v_nb = 0;
$v_extract_all = true;
$v_listing = false;
$p_path = $this->_translateWinPath($p_path, false);
if ($p_path == '' || (substr($p_path, 0, 1) != '/' && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':'))) {
$p_path = "./".$p_path;
}
$p_remove_path = $this->_translateWinPath($p_remove_path);
// ----- Look for path to remove format (should end by /)
if (($p_remove_path != '') && (substr($p_remove_path, -1) != '/'))
$p_remove_path .= '/';
$p_remove_path_size = strlen($p_remove_path);
switch ($p_mode) {
case "complete" :
$v_extract_all = TRUE;
$v_listing = FALSE;
break;
case "partial" :
$v_extract_all = FALSE;
$v_listing = FALSE;
break;
case "list" :
$v_extract_all = FALSE;
$v_listing = TRUE;
break;
default :
$this->_error('Invalid extract mode ('.$p_mode.')');
return false;
}
clearstatcache();
While (strlen($v_binary_data = $this->_readBlock()) != 0)
{
$v_extract_file = FALSE;
$v_extraction_stopped = 0;
if (!$this->_readHeader($v_binary_data, $v_header))
return false;
if ($v_header['filename'] == '')
continue;
// ----- Look for long filename
if ($v_header['typeflag'] == 'L') {
if (!$this->_readLongHeader($v_header))
return false;
}
if ((!$v_extract_all) && (is_array($p_file_list))) {
// ----- By default no unzip if the file is not found
$v_extract_file = false;
for ($i=0; $i<sizeof($p_file_list); $i++) {
// ----- Look if it is a directory
if (substr($p_file_list[$i], -1) == '/') {
// ----- Look if the directory is in the filename path
if ((strlen($v_header['filename']) > strlen($p_file_list[$i])) && (substr($v_header['filename'], 0, strlen($p_file_list[$i])) == $p_file_list[$i])) {
$v_extract_file = TRUE;
break;
}
}
// ----- It is a file, so compare the file names
elseif ($p_file_list[$i] == $v_header['filename']) {
$v_extract_file = TRUE;
break;
}
}
} else {
$v_extract_file = TRUE;
}
// ----- Look if this file need to be extracted
if (($v_extract_file) && (!$v_listing))
{
if (($p_remove_path != '')
&& (substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path))
$v_header['filename'] = substr($v_header['filename'], $p_remove_path_size);
if (($p_path != './') && ($p_path != '/')) {
while (substr($p_path, -1) == '/')
$p_path = substr($p_path, 0, strlen($p_path)-1);
if (substr($v_header['filename'], 0, 1) == '/')
$v_header['filename'] = $p_path.$v_header['filename'];
else
$v_header['filename'] = $p_path.'/'.$v_header['filename'];
}
if (file_exists($v_header['filename'])) {
if ((@is_dir($v_header['filename'])) && ($v_header['typeflag'] == '')) {
$this->_error('File '.$v_header['filename'].' already exists as a directory');
return false;
}
if ((is_file($v_header['filename'])) && ($v_header['typeflag'] == "5")) {
$this->_error('Directory '.$v_header['filename'].' already exists as a file');
return false;
}
if (!is_writeable($v_header['filename'])) {
$this->_error('File '.$v_header['filename'].' already exists and is write protected');
return false;
}
if (filemtime($v_header['filename']) > $v_header['mtime']) {
// To be completed : An error or silent no replace ?
}
}
// ----- Check the directory availability and create it if necessary
elseif (($v_result = $this->_dirCheck(($v_header['typeflag'] == "5"?$v_header['filename']:dirname($v_header['filename'])))) != 1) {
$this->_error('Unable to create path for '.$v_header['filename']);
return false;
}
if ($v_extract_file) {
if ($v_header['typeflag'] == "5") {
if (!@file_exists($v_header['filename'])) {
if (!@mkdir($v_header['filename'], 0777)) {
$this->_error('Unable to create directory {'.$v_header['filename'].'}');
return false;
}
}
} else {
if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
$this->_error('Error while opening {'.$v_header['filename'].'} in write binary mode');
return false;
} else {
$n = floor($v_header['size']/512);
for ($i=0; $i<$n; $i++) {
$v_content = $this->_readBlock();
fwrite($v_dest_file, $v_content, 512);
}
if (($v_header['size'] % 512) != 0) {
$v_content = $this->_readBlock();
fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
}
@fclose($v_dest_file);
// ----- Change the file mode, mtime
@touch($v_header['filename'], $v_header['mtime']);
// To be completed
//chmod($v_header[filename], DecOct($v_header[mode]));
}
// ----- Check the file size
clearstatcache();
if (filesize($v_header['filename']) != $v_header['size']) {
$this->_error('Extracted file '.$v_header['filename'].' does not have the correct file size \''.filesize($v_filename).'\' ('.$v_header['size'].' expected). Archive may be corrupted.');
return false;
}
}
} else {
$this->_jumpBlock(ceil(($v_header['size']/512)));
}
} else {
$this->_jumpBlock(ceil(($v_header['size']/512)));
}
/* TBC : Seems to be unused ...
if ($this->_compress)
$v_end_of_file = @gzeof($this->_file);
else
$v_end_of_file = @feof($this->_file);
*/
if ($v_listing || $v_extract_file || $v_extraction_stopped) {
// ----- Log extracted files
if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename'])
$v_file_dir = '';
if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == ''))
$v_file_dir = '/';
$p_list_detail[$v_nb++] = $v_header;
}
}
return true;
}
// }}}
// {{{ _openAppend()
function _openAppend()
{
if (filesize($this->_tarname) == 0)
return $this->_openWrite();
if ($this->_compress) {
$this->_close();
if (!@rename($this->_tarname, $this->_tarname.".tmp")) {
$this->_error('Error while renaming \''.$this->_tarname.'\' to temporary file \''.$this->_tarname.'.tmp\'');
return false;
}
if ($this->_compress_type == 'gz')
$v_temp_tar = @gzopen($this->_tarname.".tmp", "rb");
elseif ($this->_compress_type == 'bz2')
$v_temp_tar = @bzopen($this->_tarname.".tmp", "rb");
if ($v_temp_tar == 0) {
$this->_error('Unable to open file \''.$this->_tarname.'.tmp\' in binary read mode');
@rename($this->_tarname.".tmp", $this->_tarname);
return false;
}
if (!$this->_openWrite()) {
@rename($this->_tarname.".tmp", $this->_tarname);
return false;
}
if ($this->_compress_type == 'gz') {
$v_buffer = @gzread($v_temp_tar, 512);
// ----- Read the following blocks but not the last one
if (!@gzeof($v_temp_tar)) {
do{
$v_binary_data = pack("a512", $v_buffer);
$this->_writeBlock($v_binary_data);
$v_buffer = @gzread($v_temp_tar, 512);
} while (!@gzeof($v_temp_tar));
}
@gzclose($v_temp_tar);
}
elseif ($this->_compress_type == 'bz2') {
$v_buffered_lines = array();
$v_buffered_lines[] = @bzread($v_temp_tar, 512);
// ----- Read the following blocks but not the last one
while (strlen($v_buffered_lines[] = @bzread($v_temp_tar, 512)) > 0) {
$v_binary_data = pack("a512", array_shift($v_buffered_lines));
$this->_writeBlock($v_binary_data);
}
@bzclose($v_temp_tar);
}
if (!@unlink($this->_tarname.".tmp")) {
$this->_error('Error while deleting temporary file \''.$this->_tarname.'.tmp\'');
}
} else {
// ----- For not compressed tar, just add files before the last 512 bytes block
if (!$this->_openReadWrite())
return false;
clearstatcache();
$v_size = filesize($this->_tarname);
fseek($this->_file, $v_size-512);
}
return true;
}
// }}}
// {{{ _append()
function _append($p_filelist, $p_add_dir='', $p_remove_dir='')
{
if (!$this->_openAppend())
return false;
if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir))
$this->_writeFooter();
$this->_close();
return true;
}
// }}}
// {{{ _dirCheck()
/**
* Check if a directory exists and create it (including parent
* dirs) if not.
*
* @param string $p_dir directory to check
*
* @return bool TRUE if the directory exists or was created
*/
function _dirCheck($p_dir)
{
if ((@is_dir($p_dir)) || ($p_dir == ''))
return true;
$p_parent_dir = dirname($p_dir);
if (($p_parent_dir != $p_dir) &&
($p_parent_dir != '') &&
(!$this->_dirCheck($p_parent_dir)))
return false;
if (!@mkdir($p_dir, 0777)) {
$this->_error("Unable to create directory '$p_dir'");
return false;
}
return true;
}
// }}}
// {{{ _pathReduction()
/**
* Compress path by changing for example "/dir/foo/../bar" to "/dir/bar", and
* remove double slashes.
*
* @param string $p_dir path to reduce
*
* @return string reduced path
*
* @access private
*
*/
function _pathReduction($p_dir)
{
$v_result = '';
// ----- Look for not empty path
if ($p_dir != '') {
// ----- Explode path by directory names
$v_list = explode('/', $p_dir);
// ----- Study directories from last to first
for ($i=sizeof($v_list)-1; $i>=0; $i--) {
// ----- Look for current path
if ($v_list[$i] == ".") {
// ----- Ignore this directory
// Should be the first $i=0, but no check is done
}
else if ($v_list[$i] == "..") {
// ----- Ignore it and ignore the $i-1
$i--;
}
else if (($v_list[$i] == '') && ($i!=(sizeof($v_list)-1)) && ($i!=0)) {
// ----- Ignore only the double '//' in path,
// but not the first and last /
} else {
$v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?'/'.$v_result:'');
}
}
}
$v_result = strtr($v_result, '\\', '/');
return $v_result;
}
// }}}
// {{{ _translateWinPath()
function _translateWinPath($p_path, $p_remove_disk_letter=true)
{
if (OS_WINDOWS) {
// ----- Look for potential disk letter
if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
$p_path = substr($p_path, $v_position+1);
}
// ----- Change potential windows directory separator
if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
$p_path = strtr($p_path, '\\', '/');
}
}
return $p_path;
}
// }}}
}
?>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -