⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcltar.lib.php

📁 Joomla15 - 最新开源CMS
💻 PHP
📖 第 1 页 / 共 5 页
字号:
<?php
/**
* @version $Id: pcltar.lib.php 7890 2007-07-07 12:52:33Z friesengeist $
* @package		Joomla
*/


// --------------------------------------------------------------------------------
// PhpConcept Library - Tar Module 1.3
// --------------------------------------------------------------------------------
// License GNU/GPL - Vincent Blavet - August 2001
// http://www.phpconcept.net
// --------------------------------------------------------------------------------
//
// Presentation :
//	PclTar is a library that allow you to create a GNU TAR + GNU ZIP archive,
//	to add files or directories, to extract all the archive or a part of it.
//	So far tests show that the files generated by PclTar are readable by
//	gzip tools and WinZip application.
//
// Description :
//	See readme.txt (English & Fran锟絘is) and http://www.phpconcept.net
//
// Warning :
//	This library and the associated files are non commercial, non professional
//	work.
//	It should not have unexpected results. However if any damage is caused by
//	this software the author can not be responsible.
//	The use of this software is at the risk of the user.
//
// --------------------------------------------------------------------------------

// ----- Look for double include
if (!defined("PCL_TAR"))
{
  define( "PCL_TAR", 1 );

  // ----- Error codes
  //	-1 : Unable to open file in binary write mode
  //	-2 : Unable to open file in binary read mode
  //	-3 : Invalid parameters
  //	-4 : File does not exist
  //	-5 : Filename is too long (max. 99)
  //	-6 : Not a valid tar file
  //	-7 : Invalid extracted file size
  //	-8 : Unable to create directory
  //	-9 : Invalid archive extension
  //  -10 : Invalid archive format
  //  -11 : Unable to delete file (unlink)
  //  -12 : Unable to rename file (rename)
  //  -13 : Invalid header checksum


// --------------------------------------------------------------------------------
// ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
// --------------------------------------------------------------------------------

  // ----- Global variables
  $g_pcltar_version = "1.3";

  // ----- Include other libraries
  // This library should be called by each script before the include of PhpZip
  // Library in order to limit the potential 'lib' directory path problem.

  if (!defined("PCLERROR_LIB"))
  {
	include(dirname(__FILE__).DIRECTORY_SEPARATOR.'pclerror.lib.php');
  }
  if (!defined("PCLTRACE_LIB"))
  {
	include(dirname(__FILE__).DIRECTORY_SEPARATOR.'pcltrace.lib.php');
  }

  // --------------------------------------------------------------------------------
  // Function : PclTarCreate()
  // Description :
  //	Creates a new archive with name $p_tarname containing the files and/or
  //	directories indicated in $p_list. If the tar filename extension is
  //	".tar", the file will not be compressed. If it is ".tar.gz" or ".tgz"
  //	it will be a gzip compressed tar archive.
  //	If you want to use an other extension, you must indicate the mode in
  //	$p_mode ("tar" or "tgz").
  //	$p_add_dir and $p_remove_dir give you the ability to store a path
  //	which is not the real path of the files.
  // Parameters :
  //	$p_tarname : Name of an existing tar file
  //	$p_filelist : An array containing file or directory names, or
  //				 a string containing one filename or directory name, or
  //				 a string containing a list of filenames and/or directory
  //				 names separated by spaces.
  //	$p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive,
  //			 if $p_mode is not specified, it will be determined by the extension.
  //	$p_add_dir : Path to add in the filename path archived
  //	$p_remove_dir : Path to remove in the filename path archived
  // Return Values :
  //	1 on success, or an error code (see table at the beginning).
  // --------------------------------------------------------------------------------
  function PclTarCreate($p_tarname, $p_filelist="", $p_mode="", $p_add_dir="", $p_remove_dir="")
  {
	TrFctStart(__FILE__, __LINE__, "PclTarCreate", "tar=$p_tarname, file='$p_filelist', mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
	$v_result=1;

	// ----- Look for default mode
	if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
	{
	  // ----- Extract the tar format from the extension
	  if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
	  {
		// ----- Return
		TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
		return PclErrorCode();
	  }

	  // ----- Trace
	  TrFctMessage(__FILE__, __LINE__, 1, "Auto mode selected : found $p_mode");
	}

	// ----- Look if the $p_filelist is really an array
	if (is_array($p_filelist))
	{
	  // ----- Call the create fct
	  $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
	}

	// ----- Look if the $p_filelist is a string
	else if (is_string($p_filelist))
	{
	  // ----- Create a list with the elements from the string
	  $v_list = explode(" ", $p_filelist);

	  // ----- Call the create fct
	  $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
	}

	// ----- Invalid variable
	else
	{
	  // ----- Error log
	  PclErrorLog(-3, "Invalid variable type p_filelist");
	  $v_result = -3;
	}

	// ----- Return
	TrFctEnd(__FILE__, __LINE__, $v_result);
	return $v_result;
  }
  // --------------------------------------------------------------------------------

  // --------------------------------------------------------------------------------
  // Function : PclTarAdd()
  // Description :
  //	PLEASE DO NOT USE ANY MORE THIS FUNCTION. Use PclTarAddList().
  //
  //	This function is maintained only for compatibility reason
  //
  // Parameters :
  //	$p_tarname : Name of an existing tar file
  //	$p_filelist : An array containing file or directory names, or
  //				 a string containing one filename or directory name, or
  //				 a string containing a list of filenames and/or directory
  //				 names separated by spaces.
  // Return Values :
  //	1 on success,
  //	Or an error code (see list on top).
  // --------------------------------------------------------------------------------
  function PclTarAdd($p_tarname, $p_filelist)
  {
	TrFctStart(__FILE__, __LINE__, "PclTarAdd", "tar=$p_tarname, file=$p_filelist");
	$v_result=1;
	$v_list_detail = array();

	// ----- Extract the tar format from the extension
	if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
	{
	  // ----- Return
	  TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
	  return PclErrorCode();
	}

	// ----- Look if the $p_filelist is really an array
	if (is_array($p_filelist))
	{
	  // ----- Call the add fct
	  $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $v_list_detail, "", "");
	}

	// ----- Look if the $p_filelist is a string
	else if (is_string($p_filelist))
	{
	  // ----- Create a list with the elements from the string
	  $v_list = explode(" ", $p_filelist);

	  // ----- Call the add fct
	  $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, "", "");
	}

	// ----- Invalid variable
	else
	{
	  // ----- Error log
	  PclErrorLog(-3, "Invalid variable type p_filelist");
	  $v_result = -3;
	}

	// ----- Cleaning
	unset($v_list_detail);

	// ----- Return
	TrFctEnd(__FILE__, __LINE__, $v_result);
	return $v_result;
  }
  // --------------------------------------------------------------------------------

  // --------------------------------------------------------------------------------
  // Function : PclTarAddList()
  // Description :
  //	Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
  //	The list can be an array of file/directory names or a string with names
  //	separated by one space.
  //	$p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  //	different from the real path of the file. This is usefull if you want to have PclTar
  //	running in any directory, and memorize relative path from an other directory.
  //	If $p_mode is not set it will be automatically computed from the $p_tarname
  //	extension (.tar, .tar.gz or .tgz).
  // Parameters :
  //	$p_tarname : Name of an existing tar file
  //	$p_filelist : An array containing file or directory names, or
  //				 a string containing one filename or directory name, or
  //				 a string containing a list of filenames and/or directory
  //				 names separated by spaces.
  //	$p_add_dir : Path to add in the filename path archived
  //	$p_remove_dir : Path to remove in the filename path archived
  //	$p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  // Return Values :
  //	1 on success,
  //	Or an error code (see list on top).
  // --------------------------------------------------------------------------------
  function PclTarAddList($p_tarname, $p_filelist, $p_add_dir="", $p_remove_dir="", $p_mode="")
  {
	TrFctStart(__FILE__, __LINE__, "PclTarAddList", "tar=$p_tarname, file=$p_filelist, p_add_dir='$p_add_dir', p_remove_dir='$p_remove_dir', mode=$p_mode");
	$v_result=1;
	$p_list_detail = array();

	// ----- Extract the tar format from the extension
	if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
	{
	  if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
	  {
		// ----- Return
		TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
		return PclErrorCode();
	  }
	}

	// ----- Look if the $p_filelist is really an array
	if (is_array($p_filelist))
	{
	  // ----- Call the add fct
	  $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
	}

	// ----- Look if the $p_filelist is a string
	else if (is_string($p_filelist))
	{
	  // ----- Create a list with the elements from the string
	  $v_list = explode(" ", $p_filelist);

	  // ----- Call the add fct
	  $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
	}

	// ----- Invalid variable
	else
	{
	  // ----- Error log
	  PclErrorLog(-3, "Invalid variable type p_filelist");
	  $v_result = -3;
	}

	// ----- Return
	if ($v_result != 1)
	{
	  TrFctEnd(__FILE__, __LINE__, 0);
	  return 0;
	}
	TrFctEnd(__FILE__, __LINE__, $p_list_detail);
	return $p_list_detail;
  }
  // --------------------------------------------------------------------------------

  // --------------------------------------------------------------------------------
  // Function : PclTarList()
  // Description :
  //	Gives the list of all the files present in the tar archive $p_tarname.
  //	The list is the function result, it will be 0 on error.
  //	Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  //	function will determine the type of the archive.
  // Parameters :
  //	$p_tarname : Name of an existing tar file
  //	$p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  // Return Values :
  //  0 on error (Use PclErrorCode() and PclErrorString() for more info)
  //  or
  //  An array containing file properties. Each file properties is an array of
  //  properties.
  //  The properties (array field names) are :
  //	filename, size, mode, uid, gid, mtime, typeflag, status
  //  Exemple : $v_list = PclTarList("my.tar");
  //			for ($i=0; $i<sizeof($v_list); $i++)
  //			  echo "Filename :'".$v_list[$i][filename]."'<br>";
  // --------------------------------------------------------------------------------
  function PclTarList($p_tarname, $p_mode="")
  {
	TrFctStart(__FILE__, __LINE__, "PclTarList", "tar=$p_tarname, mode='$p_mode'");
	$v_result=1;

	// ----- Extract the tar format from the extension
	if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
	{
	  if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
	  {
		// ----- Return
		TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
		return 0;
	  }
	}

	// ----- Call the extracting fct
	$p_list = array();
	if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "list", "", $p_mode, "")) != 1)
	{
	  unset($p_list);
	  TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
	  return(0);
	}

	// ----- Return
	TrFctEnd(__FILE__, __LINE__, $p_list);
	return $p_list;
  }
  // --------------------------------------------------------------------------------

  // --------------------------------------------------------------------------------
  // Function : PclTarExtract()
  // Description :
  //	Extract all the files present in the archive $p_tarname, in the directory
  //	$p_path. The relative path of the archived files are keep and become
  //	relative to $p_path.
  //	If a file with the same name already exists it will be replaced.
  //	If the path to the file does not exist, it will be created.
  //	Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
  //	function will determine the type of the archive.
  // Parameters :
  //	$p_tarname : Name of an existing tar file.
  //	$p_path : Path where the files will be extracted. The files will use
  //			 their memorized path from $p_path.
  //			 If $p_path is "", files will be extracted in "./".
  //	$p_remove_path : Path to remove (from the file memorized path) while writing the
  //					extracted files. If the path does not match the file path,
  //					the file is extracted with its memorized path.
  //					$p_path and $p_remove_path are commulative.
  //	$p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
  // Return Values :
  //	Same as PclTarList()
  // --------------------------------------------------------------------------------
  function PclTarExtract($p_tarname, $p_path="./", $p_remove_path="", $p_mode="")
  {
	TrFctStart(__FILE__, __LINE__, "PclTarExtract", "tar='$p_tarname', path='$p_path', remove_path='$p_remove_path', mode='$p_mode'");
	$v_result=1;

	// ----- Extract the tar format from the extension
	if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
	{
	  if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
	  {
		// ----- Return
		TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
		return 0;
	  }
	}

	// ----- Call the extracting fct
	if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "complete", $p_path, $p_mode, $p_remove_path)) != 1)
	{
	  TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
	  return(0);
	}

⌨️ 快捷键说明

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