欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP自带ZIP压缩、解压缩类ZipArchiv使用指南

程序员文章站 2023-01-15 11:08:08
要使用该php扩展类,需要(php 5 >= 5.2.0, pecl zip >= 1.1.0),部分方法需要 php 5.2.+,且php.ini配置支持zi...

要使用该php扩展类,需要(php 5 >= 5.2.0, pecl zip >= 1.1.0),部分方法需要 php 5.2.+,且php.ini配置支持zip
对于win系统,直接去掉php_zip.dll 扩展的注释,然后重启http服务(iis或apache)即可
linux还没有试验,理论上差别不会很大

 功能:
1、解压缩zip文件
2、将文件压缩成zip文件
3、追加文件到zip文件
4、将文件夹打包成zip文件(需要循环添加文件与创建空文件夹)
5、删除压缩文件中的条目

--------------------- ziparchive对象常用方法介绍 ---------------------

测试约定:
 测试文件为text.zip,该压缩文件包含了三个被压缩的文件(hello.txt、word.txt、ooxx.jpg),如下所示

复制代码 代码如下:

text.zip
     hello.txt
     word.txt
     ooxx.jpg

打开zip文件,以便进一步操作
ziparchive::open
 (php 5 >= 5.2.0, pecl zip >= 1.1.0)
 mixed ziparchive::open ( string $filename [, int $flags ] )

第2个参数讲解

ziparchive::overwrite    总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉
ziparchive::create        如果指定的zip文件不存在,则新建一个
ziparchive::excl        如果指定的zip文件存在,则会报错   
ziparchive::checkcons

返回值: 

 如果返回值等于下面的属性,表示对应的错误 或者 返回true
 $res == ziparchive::er_exists    file already exists.(文件已经存在)
$res == ziparchive::er_incons    zip archive inconsistent.(压缩文件不一致)
$res == ziparchive::er_inval    invalid argument.(无效的参数)
$res == ziparchive::er_memory    malloc failure.(内存错误?这个不确定)
$res == ziparchive::er_noent    no such file.(没有这样的文件)
$res == ziparchive::er_nozip    not a zip archive.(没有一个压缩文件)
$res == ziparchive::er_open        can't open file.(不能打开文件)
$res == ziparchive::er_read        read error.(读取错误)
$res == ziparchive::er_seek        seek error.(查找错误)

复制代码 代码如下:

<?php
 $zip = new ziparchive;
 $res = $zip->open('test.zip');
 if ($res === true) {
     echo 'ok';
     //解压缩到test文件夹
    $zip->extractto('test');
     $zip->close();
 } else {
     echo 'failed, code:' . $res;
 }
 ?>

根据压缩文件内的列表索引,返回被压缩文件的名称

ziparchive::getnameindex
 string ziparchive::getnameindex ( int $index [, int $flags ] )

复制代码 代码如下:

 <?php
 $zip = new ziparchive();
 $res = $zip->open('test.zip');
 if ($res === true) {
     var_dump($zip->getnameindex(0)); // hello.txt
     var_dump($zip->getnameindex(1)); // word.txt
     var_dump($zip->getnameindex(2)); // ooxx.jpg
 } else {
     echo 'failed, code:' . $res;
 }
 $zip->close();
 ?>

根据压缩内的文件名称,获取该文件的文本流

ziparchive::getstream
 resource ziparchive::getstream ( string $name )

复制代码 代码如下:

 <?php
 $zip = new ziparchive();
 $res = $zip->open('test.zip');
 if ($res === true) {
     $stream = $zip->getstream('hello.txt');
 } else {
     echo 'failed, code:' . $res;
 }
 $zip->close();
 $str = stream_get_contents($stream); //这里注意获取到的文本编码
var_dump($str);
 ?>

根据压缩文件内的索引(从0开始)修改压缩文件内的文件名

ziparchive::renameindex
 bool ziparchive::renameindex ( int $index , string $newname )
 (php 5 >= 5.2.0, pecl zip >= 1.5.0)

成功时返回 true, 或者在失败时返回 false。

复制代码 代码如下:

<?php
 $zip = new ziparchive;
 $res = $zip->open('test.zip');
 if ($res === true) {
     //把压缩文件内第一个文件修改成newname.txt
     $zip->renameindex(0,'newname.txt');
     $zip->close();
 } else {
     echo 'failed, code:' . $res;
 }
 ?>

根据压缩文件内的文件名,修改压缩文件内的文件名

ziparchive::renamename
 (php 5 >= 5.2.0, pecl zip >= 1.5.0)

复制代码 代码如下:

 <?php
 $zip = new ziparchive;
 $res = $zip->open('test.zip');
 if ($res === true) {
     //把压缩文件内的word.txt修改成newword.txt
     $zip->renamename('word.txt','newword.txt');
     $zip->close();
 } else {
     echo 'failed, code:' . $res;
 }
 ?>

获取压缩文件的注释(zip的文件注释)

ziparchive::getarchivecomment
 (php 5 >= 5.2.0, pecl zip >= 1.1.0)
 string ziparchive::getarchivecomment ([ int $flags ] )
参数:ziparchive::fl_unchanged
如果参数设置为 ziparchive::fl_unchanged, 返回原始的还没有改变的注释
 例如,在处理该压缩文件时,使用setarchivecomment()方法改变或设置注释时
 如果加上ziparchive::fl_unchanged这个参数,则表示获取改变之前的注释内容,否则获取已经改变的注释内容
 类似的还有:
ziparchive::getcommentindex 根据压缩文件内的文件索引获取【文件注释】
ziparchive::getcommentname    根据压缩文件内的文件名称获取【文件注释】
 注意:这里的是文件注释,不是压缩文件(zip)的注释

 设置或修改压缩文件的注释(zip的文件注释)
ziparchive::setarchivecomment
 (php 5 >= 5.2.0, pecl zip >= 1.4.0)
 bool ziparchive::setarchivecomment ( string $comment )

复制代码 代码如下:

 <?php
 $zip = new ziparchive;
 $res = $zip->open('test.zip', ziparchive::create);
 if ($res === true) {
     //$zip->addfromstring('test.txt', 'file content goes here');
     $zip->setarchivecomment('new archive comment');
     $zip->close();
     echo 'ok';
 } else {
     echo 'failed';
 }
 ?>

根据压缩文件内的索引删除压缩文件内的文件(也就是删除档案内的条目)

ziparchive::deleteindex
 (php 5 >= 5.2.0, pecl zip >= 1.5.0)

一、如何解压缩一个zip文件 extractto()

复制代码 代码如下:

 $zip = new ziparchive();

一、如何创建压缩文件? addfromstring() addfile()

即是是把一个或多个文件打包成一个zip文件

1、只需要new一个ziparchive对象
2、然后使用该对象的open方法创建一个zip文件
3、接着使用addfile方法,将要打包的文件写入刚刚创建的zip文件中
4、最后记得关闭该对象

复制代码 代码如下:

<?php
 //建立一个新的ziparchive的对象
$zip = new ziparchive;
 $res = $zip->open('test.zip');
 //如果打开成功
if ($res === true) {
 //如果打开失败
} else {
     //输出出错的代码
    echo 'failed, code:' . $res;
 }
 $zip->close();

以上所述就是本文的全部内容了,希望能对大家有所帮助。