教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP文件 Zip 压缩与zip解压

PHP文件 Zip 压缩与zip解压

发布时间:2016-10-28   编辑:jiaochengji.com
教程集为您提供PHP文件 Zip 压缩与zip解压等资源,欢迎您收藏本站,我们将为您提供最新的PHP文件 Zip 压缩与zip解压资源


/* 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);


17. PHP解压缩 Zip 文件


/**********************
*@file - path to zip file
*@destination - destination directory for unzipped files
*/
function unzip_file($file, $destination){
// create object
$zip = new ZipArchive() ;
// open archive
if ($zip->open($file) !== TRUE) {
die ('Could not open archive');
}
// extract contents to destination directory
$zip->extractTo($destination);
// close archive
$zip->close();
echo 'Archive extracted to directory';
}

您可能感兴趣的文章:
php利用ZipArchive类实现文件压缩与解压
PHP文件 Zip 压缩与zip解压
php解压缩 Zip 文件的代码
php解压文件的方法 php解压zip文件的例子
php ZipArchive类使用实例详解
PHP文件怎么解压和压缩?(代码示例)
php压缩与解压缩类PclZip的例子
php zip解压缩类pclzip用法举例
php ZipArchive类创建和解压zip文件实例
PHP在线压缩zip的函数代码

[关闭]
~ ~