您的位置:首页 > 编程语言 > PHP开发

PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载

2016-02-15 14:35 796 查看
原文网址:http://www.cnblogs.com/shsgl/p/4675596.html

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

一、解压缩zip文件

二、将文件压缩成zip文件

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

四、将文件夹打包成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::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

另附二:文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件

总结:        

使用PHP下载文件的操作需要给出四个header(),可参考:PHP下载文件名中文乱码解决方法和PHP下载流程分析
计算文件的大小的时候,并不需要先打开文件,通过filesize($filename)就可以看出,如果需要先打开文件的话,filesize可能就会是这样的形式了filesize($filehandle)
向客户端回送数据的是,记得要设置一个buffer,用来指定每次向客户端输出多少数据,如:$buffer=1023。如果不指定的话,就会将整个文件全部写入内存当中,再一次性的讲数据传送给客户端
通过feof()函数,可以判断要读取的文件是否读完,如果还没读完,继续读取文件($file_data=fread()),并将数据回送给客户端(echo $file_data)
每次下载完成后,在客户端都会刷新下,说明了,其实每次都将数据写入到一个临时文件中,等全部下载完成后,再将所有的数据重新整合在一起
这里我使用的是绝对路径,绝对路径有个好处,就是适应性比较强,而且相对于相对路径,效率更高(免去了查找文件的过程)

分析下技术要点:                



将文件打包成zip格式
下载文件的功能

要点解析:

这里我采用的是php自带的ZipArchive类

    a) 我们只需要new一个ZipArchive对象,然后使用open方法创建一个zip文件,接着使用addFile方法,将要打包的文件写入刚刚创建的zip文件中,最好还得记得关闭该对象。

    b) 注意点:使用open方法的时候,第二个参数$flags是可选的,$flags用来指定对打开的zip文件的处理方式,共有四种情况

  i. ZIPARCHIVE::OVERWRITE总是创建一个新的文件,如果指定的zip文件存在,则会覆盖掉

ii. ZIPARCHIVE::CREATE 如果指定的zip文件不存在,则新建一个

iii. ZIPARCHIVE::EXCL 如果指定的zip文件存在,则会报错

iv. ZIPARCHIVE::CHECKCONS

下载文件的流程:                   

服务器端的工作:

-------------------------------------------

客户端的浏览器发送一个请求到处理下载的php文件。

注意:任何一个操作都首先需要写入到内存当中,不管是视频、音频还是文本文件,都需要先写入到内存当中。

换句话说,将“服务器”上的文件读入到“服务器”的内存当中的这个操作时必不可少的(注意:这里我将服务器三个字加上双引号,主要是说明这一系类的操作时在服务器上完成的)。<br>

既然要将文件写入到内存当中,就必然要先将文件打开

所以这里就需要三个文件操作的函数了:

一:fopen($filename ,$mode)

二:fread ( int $handle , int $length )

三:fclose ( resource $handle )

---------------------------------------

客户端端的工作:

---------------------------------------

那么,如何将已经存在于服务器端内存当中的文件信息流,传给客户端呢?

答案是通过header()函数,客户端就知道该如何处理文件,是保存还是打开等等

最终的效果如下图所示:





页面显示的代码:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: