php 解压rar文件及zip文件的方法

yipeiwu_com5年前PHP代码库
对于zip文件网上的例子很多,rar文件解压php没有直接支持,可以用pecl到http://pecl.php.net/package/rar 下载对应版本的 非线程安全的dll然后扔到php的 ext目录下。

打开php.ini.

加一行

extension=php_rar.dll

重启web服务器 和php
复制代码 代码如下:

public function _unzip($fileName,$extractTO){

$fileName = iconv('utf-8','gb2312',"upload/zip/8月.rar");
// echo $fileName . '</br>';
$extractTo = "upload/zip/TEST/";

$rar_file = rar_open($fileName) or die('could not open rar');
$list = rar_list($rar_file) or die('could not get list');
// print_r($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);

}

相关文章

php中substr()函数参数说明及用法实例

本文实例讲述了php中substr()函数参数说明及用法。分享给大家供大家参考。具体如下: string substr(string $string ,int $start [, int...

php面向对象中static静态属性与方法的内存位置分析

本文实例分析了php面向对象中static静态属性与方法的内存位置。分享给大家供大家参考。具体如下: static静态属性的内存位置——>类,而不是对象。下面做测试来证明一下...

Laravel模板引擎Blade中section的一些标签的区别介绍

Laravel 框架中的 Blade 模板引擎,很好用,但是在官方文档中有关 Blade 的介绍并不详细,有些东西没有写出来,而有些则是没有说清楚。比如,使用中可能会遇到这样的问题: 1...

php中使用preg_replace函数匹配图片并加上链接的方法

介绍:preg_replace 执行正则表达式的搜索和替换,如果只是单纯的匹配字符串建议使用str_replace(),因为其执行效率高的多。mixed preg_replace ( m...

php empty,isset,is_null判断比较(差异与异同)

php empty,isset,is_null判断比较(差异与异同)

一、举例说明 A.一个变量没有定义,我们该怎么样去判断呢 复制代码 代码如下: <?php #不存在$test 变量 $isset= isset($test)?"test is d...