archive_tar.php

来自「国外免费开源的内容管理系统」· PHP 代码 · 共 1,665 行 · 第 1/4 页

PHP
1,665
字号
	* @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);
	}
	// }}}

	// {{{ _openWrite()
	function _openWrite()
	{
		if ($this->_compress_type == 'gz')
			$this->_file = @gzopen($this->_tarname, "wb");
		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.')');

			$this->_file = 0;
		}

		// ----- Look if a local copy need to be erase
		// Note that it might be interesting to keep the url for a time : ToDo
		if ($this->_temp_tarname != '') {
			@unlink($this->_temp_tarname);
			$this->_temp_tarname = '';
		}

		return true;
	}
	// }}}

	// {{{ _cleanFile()
	function _cleanFile()
	{
		$this->_close();

		// ----- Look for a local copy
		if ($this->_temp_tarname != '') {
			// ----- Remove the local copy but not the remote tarname
			@unlink($this->_temp_tarname);
			$this->_temp_tarname = '';
		} else {
			// ----- Remove the local tarname file
			@unlink($this->_tarname);
		}
		$this->_tarname = '';

		return true;
	}
	// }}}

	// {{{ _writeBlock()
	function _writeBlock($p_binary_data, $p_len=null)
	{
	  if (is_resource($this->_file)) {
		  if ($p_len === null) {
			  if ($this->_compress_type == 'gz')
				  @gzputs($this->_file, $p_binary_data);
			  else if ($this->_compress_type == 'bz2')
				  @bzwrite($this->_file, $p_binary_data);
			  else if ($this->_compress_type == 'none')
				  @fputs($this->_file, $p_binary_data);
			  else
				  $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');
		  } else {
			  if ($this->_compress_type == 'gz')
				  @gzputs($this->_file, $p_binary_data, $p_len);
			  else if ($this->_compress_type == 'bz2')
				  @bzwrite($this->_file, $p_binary_data, $p_len);
			  else if ($this->_compress_type == 'none')
				  @fputs($this->_file, $p_binary_data, $p_len);
			  else
				  $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');

		  }
	  }
	  return true;
	}
	// }}}

	// {{{ _readBlock()
	function _readBlock($p_len=null)
	{
	  $v_block = null;
	  if (is_resource($this->_file)) {
		  if ($p_len === null)
			  $p_len = 512;

		  if ($this->_compress_type == 'gz')
			  $v_block = @gzread($this->_file, 512);
		  else if ($this->_compress_type == 'bz2')
			  $v_block = @bzread($this->_file, 512);
		  else if ($this->_compress_type == 'none')
			  $v_block = @fread($this->_file, 512);
		  else
			  $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');

	  }
	  return $v_block;
	}
	// }}}

	// {{{ _jumpBlock()
	function _jumpBlock($p_len=null)
	{
	  if (is_resource($this->_file)) {
		  if ($p_len === null)
			  $p_len = 1;

		  if ($this->_compress_type == 'gz')
			  @gzseek($this->_file, @gztell($this->_file)+($p_len*512));
		  else if ($this->_compress_type == 'bz2') {
			  // ----- Replace missing bztell() and bzseek()
			  for ($i=0; $i<$p_len; $i++)
				  $this->_readBlock();
		  } else if ($this->_compress_type == 'none')
			  @fseek($this->_file, @ftell($this->_file)+($p_len*512));
		  else
			  $this->_error('Unknown or missing compression type ('.$this->_compress_type.')');

	  }
	  return true;
	}
	// }}}

	// {{{ _writeFooter()
	function _writeFooter()
	{
	  if (is_resource($this->_file)) {
		  // ----- Write the last 0 filled block for end of archive
		  $v_binary_data = pack("a512", '');
		  $this->_writeBlock($v_binary_data);
	  }
	  return true;
	}
	// }}}

	// {{{ _addList()
	function _addList($p_list, $p_add_dir, $p_remove_dir)
	{
	  $v_result=true;
	  $v_header = array();

	  // ----- Remove potential windows directory separator
	  $p_add_dir = $this->_translateWinPath($p_add_dir);
	  $p_remove_dir = $this->_translateWinPath($p_remove_dir, false);

	  if (!$this->_file) {
		  $this->_error('Invalid file descriptor');
		  return false;
	  }

	  if (sizeof($p_list) == 0)
		  return true;

	  for ($j=0; ($j<count($p_list)) && ($v_result); $j++) {
		$v_filename = $p_list[$j];

		// ----- Skip the current tar name
		if ($v_filename == $this->_tarname)
			continue;

		if ($v_filename == '')
			continue;

		if (!file_exists($v_filename)) {
			$this->_warning("File '$v_filename' does not exist");
			continue;
		}

		// ----- Add the file or directory header
		if (!$this->_addFile($v_filename, $v_header, $p_add_dir, $p_remove_dir))
			return false;

		if (@is_dir($v_filename)) {
			if (!($p_hdir = opendir($v_filename))) {
				$this->_warning("Directory '$v_filename' can not be read");
				continue;
			}
			$p_hitem = readdir($p_hdir); // '.' directory
			$p_hitem = readdir($p_hdir); // '..' directory

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?