php_rar.dll安装方法如下:
放在 /php/ext/下面,在php.ini中加入一行php_rar扩展引用声明 extension=php_rar.dll。 备注:下载压缩包里有windows php_rar.dll文件。
解压压缩包
$fileName = "demo.rar"; function unrar($fileName, $extractTo) {//$fileName压缩包,$extractTo解压地址 $rar_file = rar_open($fileName) or die('could not open rar'); $list = rar_list($rar_file) or die('could not get list'); foreach ($list as $file) { $pattern = '/\".*\"/'; preg_match($pattern, $file, $matches, PREG_OFFSET_CAPTURE); $pathStr = $matches[0][0]; $pathStr = str_replace("\"", '', $pathStr); // print_r($pathStr); $entry = rar_entry_get($rar_file, $pathStr) or die('</br>entry not found'); $entry->extract($extractTo); // extract to the current dir } rar_close($rar_file); }
打印压缩包文件列表
function rar_lists($fileName) { //获取rar压缩文件列表 $rar_file = rar_open($fileName) or die('could not open rar'); $list = rar_list($rar_file) or die('could not get list'); foreach ($list as $file) { $pattern = '/\".*\"/'; preg_match($pattern, $file, $matches, PREG_OFFSET_CAPTURE); $pathStr = $matches[0][0]; $pathStr = str_replace("\"", '', $pathStr); $files[] = $pathStr; } return $files; }