Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php进行文件Zip压缩的代码

php进行文件Zip压缩的代码

发布时间:2015-03-19   编辑:www.jquerycn.cn
php进行文件Zip压缩的代码,以下示例,使用了php中的ZipArchive。<br />

php进行文件Zip压缩的代码,有需要的朋友可以参考下。
以下示例,使用了php中的ZipArchive。
 

复制代码 代码如下:

<?php
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return 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
{
return false;
}
}
/***** Example Usage ***/
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');
create_zip($files, 'myzipfile.zip', true);
?>

您可能感兴趣的文章:
php进行文件Zip压缩的代码
PHP文件怎么解压和压缩?(代码示例)
php利用ZipArchive类实现文件压缩与解压
php压缩与解压缩类PclZip的例子
php zip解压缩类pclzip用法举例
php ZipArchive类使用实例详解
php解压缩 Zip 文件的代码
PHP压缩解压缩类PclZip用法教程
PHP在线压缩zip的函数代码
php在线压缩打包rar并自动下载文件的例子

关键词: zip  ziparchive  压缩   
[关闭]