这篇文章主要介绍了PHP自带ZIP压缩、解压缩类ZipArchiv使用指南,十分详细,需要的朋友可以参考下
要使用该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.(查找错误)
复制代码 代码如下:
$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 ] )
复制代码 代码如下:
$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 )
复制代码 代码如下:
$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。
复制代码 代码如下:
$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)
复制代码 代码如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!
