php 压缩文件为zip格式的函数代码
发布时间:2016-03-08 编辑:jiaochengji.com
本文介绍下,php实现的一个可以将文件压缩为zip格式的函数,用到了ZipArchive类。有需要的朋友,参考下吧。
一个PHP 压缩文件成zip的函数,代码如下:
复制代码 代码示例:
<?php
/* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数
* @$files 数组类型 实例array("1.jpg","2.jpg");
* @destination 目标文件的路径 如"c:/androidyue.zip"
* @$overwrite 是否为覆盖与目标文件相同的文件
* @site http://www.jbxue.com
*/
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
//如果zip文件已经存在并且设置为不重写返回false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
//获取到真实有效的文件名
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
//如果存在真实有效的文件
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
//打开文件 如果文件已经存在则覆盖,如果没有则创建
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
//向压缩文件中添加文件
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
//关闭文件
$zip->close();
//check to make sure the file exists
//检测文件是否存在
return file_exists($destination);
}else{
//如果没有真实有效的文件返回false
return false;
}
}
/****
//测试函数
$files=array('temp.php','test.php');
create_zip($files, 'myzipfile.zip', true);
****/
?>
/* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数
* @$files 数组类型 实例array("1.jpg","2.jpg");
* @destination 目标文件的路径 如"c:/androidyue.zip"
* @$overwrite 是否为覆盖与目标文件相同的文件
* @site http://www.jbxue.com
*/
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
//如果zip文件已经存在并且设置为不重写返回false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
//获取到真实有效的文件名
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
//如果存在真实有效的文件
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
//打开文件 如果文件已经存在则覆盖,如果没有则创建
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
//向压缩文件中添加文件
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
//关闭文件
$zip->close();
//check to make sure the file exists
//检测文件是否存在
return file_exists($destination);
}else{
//如果没有真实有效的文件返回false
return false;
}
}
/****
//测试函数
$files=array('temp.php','test.php');
create_zip($files, 'myzipfile.zip', true);
****/
?>
您可能感兴趣的文章:
php压缩与解压缩类PclZip的例子
php zip解压缩类pclzip用法举例
php ZipArchive类使用实例详解
PHP在线压缩zip的函数代码
php解压文件的方法 php解压zip文件的例子
PHP压缩解压缩类PclZip用法教程
php利用ZipArchive类实现文件压缩与解压
Python zip函数及用法
php 压缩文件为zip格式的函数代码
VBScript压缩单个文件为zip格式的代码
上一篇:PHP正则表达式读取xml数据的代码举例
下一篇:酷讯PHP程序员面试题-笔试部分
[关闭]