📄 pcltar.lib.php
字号:
if (($p_tar = @gzopen($p_tarname, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Open a temporary file in write mode $v_temp_tarname = uniqid("pcltar-").".tmp"; TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname"); if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0) { // ----- Close tar file gzclose($p_tar); // ----- Error log PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Read the first 512 bytes block $v_buffer = gzread($p_tar, 512); // ----- Read the following blocks but not the last one if (!gzeof($p_tar)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i=1; // ----- Read new 512 block and write the already read do{ // ----- Write the already read block $v_binary_data = pack("a512", "$v_buffer"); gzputs($v_temp_tar, $v_binary_data); $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i"); // ----- Read next block $v_buffer = gzread($p_tar, 512); } while (!gzeof($p_tar)); TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks"); } } // ----- Look for uncompressed tar file else if ($p_mode=="tar") { // ----- Open the tar file if (($p_tar = fopen($p_tarname, "r+b")) == 0) { // ----- Error log PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Go to the beginning of last block TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar))); fseek($p_tar, $v_size-512); TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar))); } // ----- Look for unknown type else { // ----- Error log PclErrorLog(-3, "Invalid tar mode $p_mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Look for type of archive to add if ($p_mode_add == "tgz") { TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add"); // ----- Open the file in read mode if (($p_tar_add = @gzopen($p_tarname_add, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Read the first 512 bytes block $v_buffer = gzread($p_tar_add, 512); // ----- Read the following blocks but not the last one if (!gzeof($p_tar_add)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i=1; // ----- Read new 512 block and write the already read do{ // ----- Write the already read block $v_binary_data = pack("a512", "$v_buffer"); if ($p_mode=="tar") fputs($p_tar, $v_binary_data); else gzputs($v_temp_tar, $v_binary_data); $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i"); // ----- Read next block $v_buffer = gzread($p_tar_add, 512); } while (!gzeof($p_tar_add)); TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks"); } // ----- Close the files gzclose($p_tar_add); } // ----- Look for uncompressed tar file else if ($p_mode=="tar") { // ----- Open the file in read mode if (($p_tar_add = @fopen($p_tarname_add, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Read the first 512 bytes block $v_buffer = fread($p_tar_add, 512); // ----- Read the following blocks but not the last one if (!feof($p_tar_add)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i=1; // ----- Read new 512 block and write the already read do{ // ----- Write the already read block $v_binary_data = pack("a512", "$v_buffer"); if ($p_mode=="tar") fputs($p_tar, $v_binary_data); else gzputs($v_temp_tar, $v_binary_data); $i++; TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i"); // ----- Read next block $v_buffer = fread($p_tar_add, 512); } while (!feof($p_tar_add)); TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks"); } // ----- Close the files fclose($p_tar_add); } // ----- Call the footer of the tar archive $v_result = PclTarHandleFooter($p_tar, $p_mode); // ----- Look for closing compressed archive if ($p_mode == "tgz") { // ----- Close the files gzclose($p_tar); gzclose($v_temp_tar); // ----- Unlink tar file if (!@unlink($p_tarname)) { // ----- Error log PclErrorLog(-11, "Error while deleting archive name $p_tarname"); } // ----- Rename tar file if (!@rename($v_temp_tarname, $p_tarname)) { // ----- Error log PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } // ----- Look for closing uncompressed tar file else if ($p_mode=="tar") { // ----- Close the tarfile fclose($p_tar); } // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } // --------------------------------------------------------------------------------// --------------------------------------------------------------------------------// ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****// ***** *****// ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****// -------------------------------------------------------------------------------- // -------------------------------------------------------------------------------- // Function : PclTarHandleCreate() // Description : // Parameters : // $p_tarname : Name of the tar file // $p_list : An array containing the file or directory names to add in the tar // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive // Return Values : // -------------------------------------------------------------------------------- function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir="", $p_remove_dir="") { TrFctStart(__FILE__, __LINE__, "PclTarHandleCreate", "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'"); $v_result=1; $v_list_detail = array(); // ----- Check the parameters if (($p_tarname == "") || (($p_mode != "tar") && ($p_mode != "tgz"))) { // ----- Error log if ($p_tarname == "") PclErrorLog(-3, "Invalid empty archive name"); else PclErrorLog(-3, "Unknown mode '$p_mode'"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Look for tar file if ($p_mode == "tar") { // ----- Open the tar file if (($p_tar = fopen($p_tarname, "wb")) == 0) { // ----- Error log PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Call the adding fct inside the tar if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1) { // ----- Call the footer of the tar archive $v_result = PclTarHandleFooter($p_tar, $p_mode); } // ----- Close the tarfile fclose($p_tar); } // ----- Look for tgz file else { // ----- Open the tar file if (($p_tar = @gzopen($p_tarname, "wb")) == 0) { // ----- Error log PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Call the adding fct inside the tar if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1) { // ----- Call the footer of the tar archive $v_result = PclTarHandleFooter($p_tar, $p_mode); } // ----- Close the tarfile gzclose($p_tar); } // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } // -------------------------------------------------------------------------------- // -------------------------------------------------------------------------------- // Function : PclTarHandleAppend() // Description : // Parameters : // $p_tarname : Name of the tar file // $p_list : An array containing the file or directory names to add in the tar // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive // Return Values : // -------------------------------------------------------------------------------- function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir) { TrFctStart(__FILE__, __LINE__, "PclTarHandleAppend", "tar=$p_tarname, list, mode=$p_mode"); $v_result=1; // ----- Check the parameters if ($p_tarname == "") { // ----- Error log PclErrorLog(-3, "Invalid empty archive name"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } clearstatcache(); // ----- Check the file size if ((!is_file($p_tarname)) || (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar"))) { // ----- Error log if (!is_file($p_tarname)) PclErrorLog(-4, "Archive '$p_tarname' does not exist"); else PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Look for compressed archive if ($p_mode == "tgz") { // ----- Open the file in read mode if (($p_tar = @gzopen($p_tarname, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Open a temporary file in write mode $v_temp_tarname = uniqid("pcltar-").".tmp"; TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname"); if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0) { // ----- Close tar file gzclose($p_tar); // ----- Error log PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Read the first 512 bytes block $v_buffer = gzread($p_tar, 512); // ----- Read the following blocks but not the last one if (!gzeof($p_tar)) { TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file"); $i=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -