• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP ZipArchive 实现压缩解压Zip文件

php 搞代码 4年前 (2022-01-23) 28次浏览 已收录 0个评论

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

一、解压缩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文件}

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::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::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::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 — Undo6来源gaodaimacom搞#^代%!码网搞gaodaima代码 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 ZipArchive 实现压缩解压Zip文件,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


    搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:PHP ZipArchive 实现压缩解压Zip文件

    喜欢 (0)
    [搞代码]
    分享 (0)
    发表我的评论
    取消评论

    表情 贴图 加粗 删除线 居中 斜体 签到

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址