Heim  >  Artikel  >  php教程  >  PHP PclZip 创建ZIP压缩文件

PHP PclZip 创建ZIP压缩文件

WBOY
WBOYOriginal
2016-06-06 20:09:272271Durchsuche

PHP 压缩解压类 PclZip 能够压缩与解压缩Zip格式的文件(WinZip、PKZIP),它能对ZIP文件进行处理,包括产生压缩文件、列出压缩文件的内容以及解压ZIP等等。 当期最新的版本为 PclZip 2.8.2 PclZip 类创建ZIP文件 方法定义 方法: PclZip::create($filelist,

PHP 压缩解压类 PclZip 能够压缩与解压缩Zip格式的文件(WinZip、PKZIP),它能对ZIP文件进行处理,包括产生压缩文件、列出压缩文件的内容以及解压ZIP等等。

当期最新的版本为 PclZip 2.8.2

PclZip 类创建ZIP文件 方法定义

方法:

PclZip::create($filelist, [optional arguments])

此方法用来将指定的文件或者指定的文件夹下的所有的文件,生成一个ZIP压缩文件。

参数讲解:

$filelist 可以为:

  • 一个或几个文件名或者文件夹的数组,比如:array(‘file.txt’, ‘data/text.txt’, ‘folder’)
  • 单个文件名或者文件夹的字符串,比如:file.txt
  • 逗号分离的多个文件名或者文件夹的字符串,比如:file.txt,data/text.txt,folder

arguments 为可选参数,但是我也必须讲解几个重要的,经常要使用到:

  • PCLZIP_OPT_REMOVE_ALL_PATH,它能够删除掉压缩文件中的所有目录;
  • PCLZIP_OPT_REMOVE_PATH,它能删除压缩文件中指定的某个目录;
  • PCLZIP_OPT_ADD_PATH,它能给压缩文件统一加入到一个目录中;

使用 PclZip 类创建ZIP压缩文件

通过PclZip 类在服务器端生成ZIP压缩文件,如下图:

PclZip generate ZIP on server 使用 PclZip 类创建ZIP压缩文件

方法调用 (使用前require):

require_once(‘pclzip.lib.php’);

设置生成的ZIP文件保存路径:

$file_zipname = file_directory_path().’/zip/’.crc32(md5($id)).’-’.$version.".zip";
$archive = new PclZip($file_zipname);

删除压缩文件中的所有目录:

$list = $archive->create("data/file.txt images/image.gif", PCLZIP_OPT_REMOVE_ALL_PATH);

删除压缩文件中指定的某个目录:

$list = $archive->add("/usr/local/user/test/file.txt", PCLZIP_OPT_REMOVE_PATH, "/usr/local/user");

压缩文件加入到一个目录中:

$list = $archive->create("file.txt,image.gif", PCLZIP_OPT_ADD_PATH, "backup"); 

(...)
Read the rest of PHP PclZip 创建ZIP压缩文件 (36 words)


© lixiphp for LixiPHP, 2013. | Permalink | No comment | Add to del.icio.us
Post tags: PclZip, PHP, ZIP, 压缩解压

Feed enhanced by Better Feed from Ozh

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn