Heim  >  Artikel  >  Backend-Entwicklung  >  PHP zip解压缩类PclZip使用方法总结

PHP zip解压缩类PclZip使用方法总结

WBOY
WBOYOriginal
2016-06-20 13:04:181020Durchsuche

PHP zip解压缩方法很多,除了php自带的解压缩扩展实现外,下面介绍一种功能强大PHP ZIP加压缩类。

PHP zip解压缩类PclZip介绍

PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip、PKZIP);且能对此类类档案进行处理,包括产生压缩档、列出压缩档的内容以及解压缩档案等等。由于能够在伺服器端进行压缩与解压缩的动作,所以相当方便使用。

PclZip定义一个PclZip类别,其类别物件可视为一个ZIP档案,亦提供method来进行处理。

如何使用PHP zip解压缩类PclZip

1.基础

所有的功能都由pclzip.lib.php这个档案提供,PclZip library可于其首页(www.phpconcept.net/pclzip/index.en.php)下载。所有的PKZIP档案其实就是一个PclZip的类别物件。当产生一个PclZip档案(ie, PclZip类别物件),就会先产生一个压缩档,且档名已经指定,但此压缩档的内容尚未存在:

< ?PHP <br />         require_once('pclzip.lib.php'); <br />         $archive = new PclZip("archive.zip"); <br />?>

此物件提供了一些public method可用来处理此档案。

2.参数

每一个method有其各自可使用的参数,包括有必须与非必须的参数:
< ?PHP <br />         require_once('pclzip.lib.php'); <br />         $archive = new PclZip('archive.zip'); <br />  <br />         $v_list = $archive->add('dev/file.txt', <br />                                    PCLZIP_OPT_REMOVE_PATH, 'dev'); <br />?>

上例中的'dev/file.txt'就是必须参数;'PCLZIP_OPT_REMOVE_PATH'则为非必须参数。当然有些method也可以只包含非必须的参数:
< ?PHP <br />         $list = $archive->extract(PCLZIP_OPT_PATH, "folder", <br />                          PCLZIP_OPT_REMOVE_PATH, "data", <br />                                PCLZIP_CB_PRE_EXTRACT, "callback_pre_extract",); <br />?>

上例中原本压缩档内档案存放的路径为/data,不过你可以指定解压缩至/folder中。此外,在解压缩之前,会呼叫callback function('callback_pre_extract()'),此function可让使用者在解压缩的过程中变更档案存放路径与档名,或是选择某些档案不解压缩。

3.回传值

每个method所回传的值可能会不同,将会在每个method中说明。不过大部分的method回传0、error或是阵列。

4.错误处理

从版本1.3之后,错误处理已经整合至PclZip类别中,当一个method回传错误码,可以得知一些额外的讯息以方便错误处理:
* errorName():回传错误名称
* errorCode():回传错误码
* errorInfo():回传错误的描述接下来会举几个例子来说明如何使用PclZip。PclZip实例1、产生ZIP压缩档
PclZip($zipname):为PclZip constructor,$zipname为PKZIP压缩档的档名。
主要是产生一个PclZip物件,即一个PKZIP压缩档;但此时,只有压缩档产生出来,并做一些检查(例如是否有开启zlib extension...等),除此之外,并没有做其他动作。
create($filelist, [optional arguments list]):将参数$filelist指定的档案或目录(包含当中所有档案与子目录)加入上述所产生的压缩档中。
而非必要的参数则能够修改压缩档内的档案存放路径。

此method可用的参数可以参考网志(www.phpconcept.net/pclzip/man/en/index.php)。

下面的示例说明如何产生PKZIP压缩档(档名为archive.zip),并将file.txt、data/text.txt以及目录folder(包含当中的档案与子目录)加入刚刚产生的archive.zip中:

< ?PHP<br />        include_once('pclzip.lib.php');<br />        $archive = new PclZip('archive.zip');<br />        $v_list = $archive->create('file.txt,data/text.txt,folder');<br />        if ($v_list == 0) {<br />            die("Error : ".$archive->errorInfo(true));<br />        }<br />?>

下面的示例说明基本上与上例一样产生archive.zip,但在将file.txt与text.txt压缩于其中时,将路径由data/改为install/ ;因此,在archive.zip中这两个档案的路径会是install/file.txt与install/text.txt
< ?PHP<br />        include_once('pclzip.lib.php');<br />        $archive = new PclZip('archive.zip');<br />        $v_list = $archive->create('data/file.txt,data/text.txt',<br />                                         PCLZIP_OPT_REMOVE_PATH, 'data',<br />                                         PCLZIP_OPT_ADD_PATH, 'install');<br />        if ($v_list == 0) {<br />            die("Error : ".$archive->errorInfo(true));<br />        }<br /><p>?></p>

PHP zip解压缩类PclZip实例

1、列出压缩档内容

listContent( ) :列出压缩档中的内容,包括档案的属性与目录:

<span style="font-size: 14px;">< ?PHP</span><br />        include_once('pclzip.lib.php');<br />        $zip = new PclZip("test.zip");<br /> <br />        if (($list = $zip->listContent()) == 0) {<br />        die("Error : ".$zip->errorInfo(true));<br />        }<br /> <br />        for ($i=0; $i<sizeof ($list); $i++) {<br />            for(reset($list[$i]); $key = key($list[$i]); next($list[$i])) {<br />                echo "File $i / [$key] = ".$list[$i][$key]."<br />";<br />            }<br />            echo "<br />";<br />        }<br /><p>?><span style="font-size: 14px;">

上例将会回传结果:
File 0 / [filename] = data/file1.txt
File 0 / [stored_filename] = data/file1.txt
File 0 / [size] = 53
File 0 / [compressed_size] = 36
File 0 / [mtime] = 1010440428
File 0 / [comment] =
File 0 / [folder] = 0
File 0 / [index] = 0
File 0 / [status] = okFile 1 / [filename] = data/file2.txt
File 1 / [stored_filename] = data/file2.txt
File 1 / [size] = 54
File 1 / [compressed_size] = 53
File 1 / [mtime] = 1011197724
File 1 / [comment] =
File 1 / [folder] = 0
File 1 / [index] = 1

File 1 / [status] = ok

2、解压缩档案

extract([options list]) :解压缩PKZIP中的档案或目录。

[options list]可用的参数。

这些参数能让使用者在解压缩的时候有更多的选项,譬如指定变更解压缩档案的路径、指定只解压缩某些档案或不解压缩某些档案或者是将档案解压缩成字串输出(可用于readme档)。下例是一个简单的解压缩档案示例,将压缩档archive.zip内的档案解压缩至目前的目录:

<span style="font-size: 14px;">< ?PHP</span><br />        require_once('pclzip.lib.php');<br />        $archive = new PclZip('archive.zip');<br /> <br /> <br />        if ($archive->extract() == 0) {<br />            die("Error : ".$archive->errorInfo(true));<br />        }<br />?>

下例是进阶的解压缩档案使用,archive.zip中所有档案都解压缩于data/中,而特别指明在install/release中的所有档案也直接丢于data/中,而非data/install/ release:
< ?PHP<br />        include('pclzip.lib.php');<br />        $archive = new PclZip('archive.zip');<br />        if ($archive->extract(PCLZIP_OPT_PATH, 'data',<br />                  PCLZIP_OPT_REMOVE_PATH, 'install/release') == 0) {<br />                                die("Error : ".$archive->errorInfo(true));<br />        }<br /><p>?>

项目地址:http://www.phpconcept.net/pclzip/


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn