php 解压zip/rar

刚开始学习php,在做php在线解压压缩包并读取文件的目录结构的功能,记录下遇到的问题

  1. 刚开始以为可以直接读取压缩包的目录结构(可能我还没找到方法吧),浪费了好多时间

  2. 后来把压缩包解压到指定的目录中,在读取指定目录指定文件夹(压缩包名)的目录结构,之后删除该文件

  3. 在线解压zip可以使用php自带的ZipAchive

文章链接:

http://www.jb51.net/article/61678.htm

http://my.oschina.net/junn/blog/104464

使用上面方法可以解决解压问题,但是也有个坑:

Windows的文件系统是gbk编码的,而PHP ZipArchive中的文件名是UTF-8编码的。换句话说,当有中文字符时会出现乱码

解决方法:

不要用自带的ZipArchive::extractTo()方法去解压ZipArchive文档,改用PHP的zip封装协议解压去解决ZipArchive乱码。网上有说php7.1之后就解决了中文的问题,我还没试过


<?php
$zip = new \ZipArchive;
$zipfile = "./test.zip";
$res = $zip->open($zipfile);
 
$toDir = "./test";
if(!file_exists($toDir)) {
    mkdir($toDir);
}
$docnum = $zip->numFiles;
for($i = 0; $i < $docnum; $i++) {
    $statInfo = $zip->statIndex($i);
    if($statInfo['crc'] == 0) {
        //新建目录
        mkdir($toDir.'/'.substr($statInfo['name'], 0,-1));
    } else {
        //拷贝文件
        copy('zip://'.$zipfile.'#'.$statInfo['name'], $toDir.'/'.$statInfo['name']);
    }
}
 
print_r(scandir($toDir));
 

 4. 在线解压rar文件php不能直接使用,通过查文章发现需要使用php_rar.dll

文章链接:http://blog.csdn.net/cuixiping/article/details/15664119

文章说的很清楚,但是还是有几个坑,我还是记录下

1>  我的电脑是64位的,但是使用的wamp集成环境是32位的,选择dll时候应该选择的x86(32位)的才对

2>  文章中说选择非线程安全的(NTS)的dll,而我使用一直显示php_rar.dll未加载

查询phpinfo 发现


Thread Safety	enabled
因此应该选择的是线程安全(TS)的dll,使用后果然没再出错
3>  选择的php版本要对应,使用高版本的dll和低版本的php我没有测试不知道会不会出错,

我使用的是php5.5.12,下载的是php_rar-3.0.2-5.5-ts-vc11-x86这个dll

4> wamp中的php.ini并不是php文件夹中的那个,而是在Apache文件夹中

5. 附上代码

 

/**

  • 解压zip文件到指定目录
  • $filepath: 文件路径
  • e x t r a c t T o : 解 压 路 径 ∗ / f u n c t i o n d r u n Z i p ( extractTo: 解压路径 */ function dr_unZip( extractTo:/functiondrunZip(filepath,$extractTo) {
    $zip = new ZipArchive;
    $res = z i p − &gt; o p e n ( zip-&gt;open( zip>open(filepath);
    if (KaTeX parse error: Expected '}', got 'EOF' at end of input: …) { //解压缩到extractTo指定的文件夹
    z i p − &gt; e x t r a c t T o ( zip-&gt;extractTo( zip>extractTo(extractTo);
    $zip->close();
    } else {
    echo ‘failed, code:’ . $res;
    }
    }

/**

  • 解压rar文件到指定目录
  • $filepath: 文件路径
  • e x t r a c t T o : 解 压 路 径 ∗ / f u n c t i o n d r u n r a r ( extractTo: 解压路径 */ function dr_unrar( extractTo:/functiondrunrar(filepath,$extractTo) {
    r a r f i l e = r a r o p e n ( rar_file = rar_open( rarfile=raropen(filepath) or die(‘could not open rar’);
    l i s t = r a r l i s t ( list = rar_list( list=rarlist(rar_file) or die(‘could not get list’);
    foreach($list as $file) {
    KaTeX parse error: Can't use function '\"' in math mode at position 13: pattern = '/\̲"̲.*\"/'; pr…pattern, $file, $matches, PREG_OFFSET_CAPTURE);
    p a t h S t r = pathStr= pathStr=matches[0][0];
    KaTeX parse error: Can't use function '\"' in math mode at position 22: …r=str_replace("\̲"̲",'',pathStr);
    e n t r y = r a r e n t r y g e t ( entry = rar_entry_get( entry=rarentryget(rar_file, $pathStr) or die(‘entry not found’);
    e n t r y − &gt; e x t r a c t ( entry-&gt;extract( entry>extract(extractTo); // extract to the current dir
    }
    rar_close($rar_file);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值