false,'data'=>'[shell_exec error!] No Result!
'.LNG('explorer.unzipRarTips')); } return array('code'=>true,'data'=>$result); } /** * 防止通过构造文件名,进行shell注入 */ static function extract($file,$dest,$ext,$partName=false,$passwd=false) { $dest_before = $dest; $dest = $dest_before.md5(rand_string(40).time()).'/';mk_dir($dest); $passwd = $passwd ?" -p".escapeShell($passwd).' ':''; $paramAdd = $partName ? (is_array($partName) ? $partName[0] : $partName):''; $paramAdd = $paramAdd ? ' '.escapeShell(trim($paramAdd,'/')):''; $paramType = (!$partName || is_array($partName)) ? ' x':' e';// 为空或数组=>保持文件夹结构;传入字符串则单纯解压文件,不保留文件夹结构; if($ext == 'rar'){ $param = $paramType.' -y '.$passwd.escapeShell($file).' '.escapeShell($dest); $command = self::bin('rar').$param.$paramAdd; }else{ if($ext == 'bz2'){$ext = 'bzip2';} $param = $paramType.' -y -t'.escapeShell($ext).$passwd.' -o'.escapeShell($dest).' '.escapeShell($file); $command = self::bin('7z').$param.$paramAdd; } //pr($command,$partName,$paramAdd);exit; $result = self::run($command); //7za 兼容 rar解压大文件 if(strstr($result['data'],'is not RAR archive') && shell_exec('7za')){ $param = $paramType.' -y -o'.escapeShell($dest).' '.escapeShell($file); $command = '7za '.$param.$paramAdd; $result = self::run($command); } //echo "
";var_dump($result,$command,$ext,$partName);exit;
		if(!$result['code']){
		    del_dir($dest);
			return $result;
		}

		//子目录解压移除多余层级目录
		if( is_array($partName) ){
			$thePath = trim(str_replace("\\",'/',$partName[0]),'/');
			$pathGroup = explode('/',$thePath);
			//一级目录解压不用移动
			if(count($pathGroup) > 1){
				move_path($dest.$partName[0],$dest.get_path_this($thePath));
				del_dir($dest.$pathGroup[0]);
			}else{
				$dest_before = get_path_father($dest_before);
			}
		}
		
		//扩展名处理;文件名重命名处理
		$arr = dir_list($dest);
		foreach($arr as $f){
			$itemPath = str_replace(array($dest,"\\"),array('','/'),$f);
			//$itemPath = unzip_pre_name($itemPath); // 已经自动处理为系统编码了,就不需要再转码(windows下 rar)
			$from = $dest.get_path_father($itemPath).get_path_this($f);
			if(strstr($itemPath,'/') == false){
				$from = $dest.get_path_this($f);
			}
			if($dest.$itemPath != $from){
				@rename($from,$dest.$itemPath);
			}
		}
		move_path($dest,$dest_before);del_dir($dest);
		return $result;
	}
	
	static function listContent($file,$ext=''){
		$ext = $ext ? $ext : get_path_ext($file);
		if($ext == 'rar'){
			return self::listContentRar($file);
		}else{
			return self::listContent7z($file);
		}
	}

	static function listContentRar($file) {
		$command = self::bin('rar').' v '.escapeShell($file);
		$result = self::run($command);	
		//7za 兼容 rar解压大文件
		if(strstr($result['data'],'is not RAR archive') && shell_exec('7za')){
			return self::listContent7z($file,'7za l ');
		}
		if(!$result['code']){
			return $result;
		}

		preg_match('/--------  ----\n([\d\D]*)\n-----------/i', $result['data'], $match);
		if(!is_array($match) || strlen($match[1]) < 10){
			return array('code'=>false,'data'=>'Match Nothing Content!
'.LNG('explorer.unzipRarTips')); } //windows :...D... 93691 82633 88% 2016-12-09 02:20 396CC62C 000/a/32486963.png //linux: :-rwxr-xr-x 93691 82643 88% 2016-12-09 02:20 396CC62C 000/a/32486963.png // $reg = '/\s*([-\.\w]+)\s+(\d+)\s+(\d+)\s+\d+%|-+>\s+(\d{2,4}-\d{2}-\d{2} \d{2}:\d{2})\s+\w+\s+(.*)\n/i'; $reg = '/\s*([-\.\w]+)\s+(\d+)\s+(\d+)\s+\d+%\s+(\d{2,4}-\d{2}-\d{2} \d{2}:\d{2})\s+\w+\s+(.*)\n/i'; preg_match_all($reg,$match[1]."\n",$matchItem); if( !is_array($matchItem) || count($matchItem) != 6 || count($matchItem[0]) == 0 ){ return array('code'=>false,'data'=>'Match Nothing Item!
'.LNG('explorer.unzipRarTips')); } $itemArr = array(); for ($i = 0; $i < count($matchItem[0]); $i++) { $mode = strtoupper($matchItem[1][$i]); $isFolder = substr($mode,0,1) == 'D' || substr($mode,3,1) == 'D'; $itemArr[] = array( 'mtime' => strtotime($matchItem[4][$i]), 'size' => $matchItem[2][$i], 'z_size' => $matchItem[3][$i], 'filename' => trim($matchItem[5][$i]), 'index' => $i, 'folder' => intval($isFolder) ); } //debug_out($result,$match,$matchItem,$itemArr); return array('code'=>true,'data'=>$itemArr); } static function listContent7z($file,$bin=false) { $command = self::bin('7z').' l '.escapeShell($file); if($bin){ $command = $bin.escapeShell($file); } $result = self::run($command); if(!$result['code']){ return $result; } preg_match('/-----------\n([\d\D]*)\n--------------/i', $result['data'], $match); if(!is_array($match) || strlen($match[1]) < 10){ return array('code'=>false,'data'=>'Match Nothing Content!
'.LNG('explorer.unzipRarTips')); } //2017-03-08 11:22:16 ..... 10727 9385 000\test11.docx //2017-03-09 13:43:10 ....A 6254 000\111.md $reg = '/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (D?\.+A?)\s+(\d+)\s+(\d*)\s+(.*)/i'; preg_match_all($reg,$match[1],$matchItem); if( !is_array($matchItem) || count($matchItem) != 6 || count($matchItem[0]) == 0 ){ return array('code'=>false,'data'=>'Match Nothing Item!
'.LNG('explorer.unzipRarTips')); } $itemArr = array(); for ($i = 0; $i < count($matchItem[0]); $i++) { $itemArr[] = array( 'mtime' => strtotime($matchItem[1][$i]), 'size' => $matchItem[3][$i], 'z_size' => $matchItem[4][$i], 'filename' => trim($matchItem[5][$i]), 'index' => $i, 'folder' => substr($matchItem[2][$i],0,1) == 'D' ); } //debug_out($result,$match,$matchItem,$itemArr); return array('code'=>true,'data'=>$itemArr);; } /** * [create description] * @param [type] $file [creat file to] * @param [type] $ext [ext:7z,xz,bz2,gzip,tar,zip] * @param [type] $files [array from] * @param boolean $passwd [password] * @return [type] [description] */ // static function create($file,$files,$ext,$passwd=false) { // $passwd = $passwd? " -p".$passwd.' ':""; // $spearat = (PATH_SEPARATOR != ':')?("&& ".substr($files,0,2)." "):"";//win=>; linux=>: // $command = 'cd "'.$files.'" '.$spearat.' &&';//cd到所在文件夹; // $command = $command.self::bin().' a -r -y -t'.$ext.' '.$passwd.' "'.$file.'" *'; // return self::run($command); // } }