PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流。这里整理一下常用的示例供参考。

ZipArchive类中的常用方法

<?php
$zip = new ZipArchive();//新建一个对象/*
$zip->open这个方法第一个参数表示处理的zip文件名。
第二个参数表示处理模式,ZipArchive::OVERWRITE表示如果zip文件存在,
就覆盖掉原来的zip文件。 如果参数使用ZIPARCHIVE::CREATE,
系统就会往原来的zip文件里添加内容。 如果不是为了多次添加内容到zip文件,
建议使用ZipArchive::OVERWRITE。 使用这两个参数,如果zip文件不存在,
系统都会自动新建。 如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE
*/if ($zip->open('demo.zip', ZipArchive::OVERWRITE) === TRUE) {/* ZipArchive类中的所有属性*/echo $zip->status;//Zip Archive 的状态echo $zip->statusSys;//Zip Archive 的系统状态echo $zip->numFiles;//压缩包里的文件数echo $zip->filename;//在文件系统里的文件名,包含绝对路径echo $zip->comment;//压缩包的注释/* ZipArchive类中的常用方法*/$zip->addEmptyDir('css');//在zip压缩包中建一个空文件夹,成功时返回 TRUE, 或者在失败时返回 FALSE$zip->addFile('index.html','in.html');//在zip更目录添加一个文件,并且命名为in.html,第二个参数可以省略$zip->addFromString('in.html','hello world');//往zip中一个文件中添加内容$zip->extractTo('/tmp/zip/');//解压文件到/tmp/zip/文件夹下面$zip->renameName('in.html','index.html');//重新命名zip里面的文件$zip->setArchiveComment('Do what you love,Love what you do.');//设置压缩包的注释$zip->getArchiveComment();//获取压缩包的注释$zip->getFromName('index.html');//获取压缩包文件的内容$zip->deleteName('index.html');//删除文件$zip->setPassword('123456');//设置压缩包的密码$zip->close();//关闭资源句柄}else{echo '文件打开失败';
}


一、解压缩zip文件

$zip = new ZipArchive;//新建一个ZipArchive的对象
/*
通过ZipArchive的对象处理zip文件
$zip->open这个方法的参数表示处理的zip文件名。
如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE
*/
if ($zip->open('test.zip') === TRUE)
{$zip->extractTo('images');//假设解压缩到在当前路径下images文件夹的子文件夹php$zip->close();//关闭处理的zip文件
}


二、将文件压缩成zip文件

$zip = new ZipArchive;
/*
$zip->open这个方法第一个参数表示处理的zip文件名。
第二个参数表示处理模式,ZipArchive::OVERWRITE表示如果zip文件存在,就覆盖掉原来的zip文件。
如果参数使用ZIPARCHIVE::CREATE,系统就会往原来的zip文件里添加内容。
如果不是为了多次添加内容到zip文件,建议使用ZipArchive::OVERWRITE。
使用这两个参数,如果zip文件不存在,系统都会自动新建。
如果对zip文件对象操作成功,$zip->open这个方法会返回TRUE
*/
if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TRUE)
{$zip->addFile('image.txt');//假设加入的文件名是image.txt,在当前路径下$zip->close();
}


三、文件追加内容添加到zip文件

$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {$zip->addFromString('test.txt', 'file content goes here');$zip->close();echo 'ok';
} else {echo 'failed';
}


四、将文件夹打包成zip文件

function addFileToZip($path, $zip) {$handler = opendir($path); //打开当前文件夹由$path指定。/*循环的读取文件夹下的所有文件和文件夹其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename,为了不陷于死循环,所以还要让$filename !== false。一定要用!==,因为如果某个文件名如果叫'0',或者某些被系统认为是代表false,用!=就会停止循环*/while (($filename = readdir($handler)) !== false) {if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..’,不要对他们进行操作if (is_dir($path . "/" . $filename)) {// 如果读取的某个对象是文件夹,则递归addFileToZip($path . "/" . $filename, $zip);} else { //将文件加入zip对象$zip->addFile($path . "/" . $filename);}}}@closedir($path);
}$zip = new ZipArchive();
if ($zip->open('images.zip', ZipArchive::OVERWRITE) === TRUE) {addFileToZip('images/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法$zip->close(); //关闭处理的zip文件
}


五、几行代码实现PHP文件打包下载zip

<?php
/**     * 没有写成class 或者 function ,需要的朋友自己写,就这么几行。。     */
$filename = "./test/test.zip"; //最终生成的文件名(含路径)
if(!file_exists($filename)){
//重新生成文件   $zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释   if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {   exit('无法打开文件,或者文件创建失败');}   foreach( $datalist as $val){   $attachfile = $attachmentDir . $val['filepath']; //获取原始文件路径   if(file_exists($attachfile)){   $zip->addFile( $attachfile , basename($attachfile));//第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下   }   }   $zip->close();//关闭
}
if(!file_exists($filename)){   exit("无法找到文件"); //即使创建,仍有可能失败。。。。
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小
@readfile($filename);
?>


另外ZipArchive相关方法如下:

  • ZipArchive::addEmptyDir — Add a new directory
  • ZipArchive::addFile — Adds a file to a ZIP archive from the given path
  • ZipArchive::addFromString — Add a file to a ZIP archive using its contents
  • ZipArchive::addGlob — Add files from a directory by glob pattern
  • ZipArchive::addPattern — Add files from a directory by PCRE pattern
  • ZipArchive::close — Close the active archive (opened or newly created)
  • ZipArchive::deleteIndex — delete an entry in the archive using its index
  • ZipArchive::deleteName — delete an entry in the archive using its name
  • ZipArchive::extractTo — Extract the archive contents
  • ZipArchive::getArchiveComment — Returns the Zip archive comment
  • ZipArchive::getCommentIndex — Returns the comment of an entry using the entry index
  • ZipArchive::getCommentName — Returns the comment of an entry using the entry name
  • ZipArchive::getExternalAttributesIndex — Retrieve the external attributes of an entry defined by its index
  • ZipArchive::getExternalAttributesName — Retrieve the external attributes of an entry defined by its name
  • ZipArchive::getFromIndex — Returns the entry contents using its index
  • ZipArchive::getFromName — Returns the entry contents using its name
  • ZipArchive::getNameIndex — Returns the name of an entry using its index
  • ZipArchive::getStatusString — Returns the status error message, system and/or zip messages
  • ZipArchive::getStream — Get a file handler to the entry defined by its name (read only).
  • ZipArchive::locateName — Returns the index of the entry in the archive
  • ZipArchive::open — Open a ZIP file archive
  • ZipArchive::renameIndex — Renames an entry defined by its index
  • ZipArchive::renameName — Renames an entry defined by its name
  • ZipArchive::setArchiveComment — Set the comment of a ZIP archive
  • ZipArchive::setCommentIndex — Set the comment of an entry defined by its index
  • ZipArchive::setCommentName — Set the comment of an entry defined by its name
  • ZipArchive::setExternalAttributesIndex — Set the external attributes of an entry defined by its index
  • ZipArchive::setExternalAttributesName — Set the external attributes of an entry defined by its name
  • ZipArchive::statIndex — Get the details of an entry defined by its index.
  • ZipArchive::statName — Get the details of an entry defined by its name.
  • ZipArchive::unchangeAll — Undo all changes done in the archive
  • ZipArchive::unchangeArchive — Revert all global changes done in the archive.
  • ZipArchive::unchangeIndex — Revert all changes done to an entry at the given index
  • ZipArchive::unchangeName — Revert all changes done to an entry with the given name.

PHP之Zip扩展,解压缩文件,ZipArchive类相关推荐

  1. Linux下的zip压缩解压缩文件夹

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

  2. Java实现解压缩文件和文件夹

    目录 一 前言 二 压缩文件 2.1 压缩多个文件 2.2 压缩文件或文件树 2.3 借助文件访问器压缩 三 解压文件 四 总结 一 前言 项目开发中,总会遇到解压缩文件的时候.比如,用户下载多个文件 ...

  3. php使用ZipArchive扩展实现文件的zip压缩与zip解压

    ZipArchive 是PHP自带的zip扩展类,可以实现对文件或目录实现ZIP文件的压缩和解压,使用前首先要确保PHP ZipArchive扩展已经开启,以下代码亲测可用,但对中文命名的文件不兼容 ...

  4. Java Zip解压缩文件夹工具类 ----ZipUtils

    在项目中如果遇到解压缩 文件的话,可以直接使用这个工具类进行操作.不多说,直接上代码: 1. maven 依赖: 可能会有多余的,没有做处理 <build><plugins>& ...

  5. zip 打包_Thinkphp6利用ZipArchive打包下载文件

    php中文网课程 每日17点准时技术干货分享 基础环境 系统环境:Windows10 x64 PHP集成环境:phpstudy PHP依赖管理工具:Composer (一) 下载tp6框架 compo ...

  6. linux 解压所有以zip结尾的文件_Linux下的压缩zip,解压缩unzip命令详解及实例

    摘自:https://www.cnblogs.com/yves0923/p/10965021.html Linux下的压缩解压缩命令详解及实例 实例:压缩服务器上当前目录的内容为xxx.zip文件 z ...

  7. linux中.sql.gz文件解压,linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结

    本文介绍了linux下的压缩程式tar.gzip.gunzip.bzip2.bunzip2.compress .uncompress. zip. unzip.rar.unrar等程式,以及如何使用它们 ...

  8. Linux的压缩/解压缩文件命令 zip 和 tar

    Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...

  9. Linux 解压缩文件之zip命令

    以下内容来自Linux命令大全 zip命令 zip命令可以用来解压缩文件,或者对文件进行打包操作.zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有".zip"扩展名的压缩 ...

最新文章

  1. linux c 链接详解4-共享库
  2. java post 打开新页面_JAVA后台POST/GET访问方法
  3. Apollo进阶课程㊷丨Apollo实战——车辆与循迹驾驶能力实战
  4. 包学习(一款安卓端小学到高中全部课程精讲APP)
  5. linux 远程调试
  6. Linux 如何创建进程函数与查看进程
  7. HDU2527 Safe Or Unsafe【哈夫曼编码】
  8. Windows核心编程_窗口属性表
  9. Andrew Ng机器学习公开课笔记 -- Generative Learning algorithms
  10. 中小企业固定资产管理办法哪种好?
  11. pdf转word文档总结
  12. 苹果设置播放html5视频,类似苹果官网,使用滚轴事件控制视频播放
  13. TikTok印尼上线“TikTok Seller”;亚马逊禁用 Visa ;2021世界品牌500强出路...洞悉跨境
  14. Python 第三方模块 机器学习 Scikit-Learn模块 有监督学习1 交叉分解,高斯过程,保序回归
  15. 7.26 5 优化浪漫 恋爱中的经济学
  16. Windows XP下如何实现共享上网
  17. 快速Linux重装XP系统
  18. 查询linux下有多少用户,Linux 查看系统现存所有用户命令
  19. JavaSE基础——J2SE概述
  20. Linux远程桌面的设计总结,windows / Linux 远程桌面访问全面总结 / 共享文件

热门文章

  1. python 爱心代码
  2. OMNET++ INET框架学习教程(一)
  3. 微信小程序跨域关于跨域和 Ajax 的说明
  4. Newtonsoft.Json Sample文档
  5. 工业机器人技术试题_工业机器人技术题库及答案
  6. [ATF][Power]ARMv8 arm trust firmware
  7. Echarts三维坐标系
  8. 无障碍模式设计:别让无障碍,成为一种障碍
  9. FME转换LPK层文件到AUTOCAD报错
  10. jquery 实现 图片放大