php ZipArchive壓縮函數(shù)詳解實(shí)例
用ZipArchive壓縮文件,這個(gè)是php的擴(kuò)展類,自php5.2版本以后就已經(jīng)支持這個(gè)擴(kuò)展,如果你在使用的時(shí)候出現(xiàn)錯(cuò)誤,查看下php.ini里面的extension=php_zip.dll前面的分號(hào)有沒(méi)有去掉,然后再重啟Apache這樣才能使用這個(gè)類庫(kù)。
例1、生成zip 壓縮文件
<?php
/* 生成zip 壓縮文件 */
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) {
$file_info_arr= pathinfo($file);
$zip->addFile($file,$file_info_arr['basename']);//去掉層級(jí)目錄
}
//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;
}
}
define('ROOTPATH',dirname ( __FILE__ )); //網(wǎng)站路徑
$files_to_zip = array(
ROOTPATH.DIRECTORY_SEPARATOR.'PHP+jQuery+Cookbook.pdf',
ROOTPATH.DIRECTORY_SEPARATOR.'TurboListerZeroTemplate.csv'
);
//if true, good; if false, zip creation failed
$filename='my-archive.zip';
$result = create_zip($files_to_zip,$filename);
例2 、壓縮文件夾下面的所有文
<?php
/*
php zip壓縮文件夾下面的所有文件
*/
class HZip
{
/**
* 添加文件和子目錄的文件到zip文件
* @param string $folder
* @param ZipArchive $zipFile
* @param int $exclusiveLength Number of text to be exclusived from the file path.
*/
private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir($folder);
while (false !== $f = readdir($handle)) {
if ($f != '.' && $f != '..') {
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $exclusiveLength);
if (is_file($filePath)) {
$zipFile->addFile($filePath, $localPath);
} elseif (is_dir($filePath)) {
// 添加子文件夾
$zipFile->addEmptyDir($localPath);
self::folderToZip($filePath, $zipFile, $exclusiveLength);
}
}
}
closedir($handle);
}
/**
* Zip a folder (include itself).
* Usage:
* HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
*
* @param string $sourcePath Path of directory to be zip.
* @param string $outZipPath Path of output zip file.
*/
public static function zipDir($sourcePath, $outZipPath)
{
$pathInfo = pathInfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$sourcePath=$parentPath.'/'.$dirName;//防止傳遞'folder' 文件夾產(chǎn)生bug
$z = new ZipArchive();
$z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件
$z->addEmptyDir($dirName);//建立文件夾
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
$z->close();
}
}
//使用方法
HZip::zipDir('yourlife', 'yourlife.zip');
?>
1.ZipArchive::addEmptyDir
添加一個(gè)新的文件目錄
2.ZipArchive::addFile
將文件添加到指定zip壓縮包中。
3.ZipArchive::addFromString
添加的文件同時(shí)將內(nèi)容添加進(jìn)去
4.ZipArchive::close
關(guān)閉ziparchive
5.ZipArchive::extractTo
將壓縮包解壓
6.ZipArchive::open
打開一個(gè)zip壓縮包
7.ZipArchive::getStatusString
返回壓縮時(shí)的狀態(tài)內(nèi)容,包括錯(cuò)誤信息,壓縮信息等等
8.ZipArchive::deleteIndex
刪除壓縮包中的某一個(gè)文件,如:deleteIndex(0)刪除第一個(gè)文件
9.ZipArchive::deleteName
刪除壓縮包中的某一個(gè)文件名稱,同時(shí)也將文件刪除。
- php使用ZipArchive函數(shù)實(shí)現(xiàn)文件的壓縮與解壓縮
- php利用ZipArchive類操作文件的實(shí)例
- php ZipArchive實(shí)現(xiàn)多文件打包下載實(shí)例
- php打包壓縮文件之ZipArchive方法用法分析
- php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解決方法
- php的ZipArchive類用法實(shí)例
- php文件打包 下載之使用PHP自帶的ZipArchive壓縮文件并下載打包好的文件
- php?ZipArchive解壓縮實(shí)現(xiàn)后臺(tái)管理升級(jí)問(wèn)題詳解
相關(guān)文章
windows的文件系統(tǒng)機(jī)制引發(fā)的PHP路徑爆破問(wèn)題分析
這篇文章主要介紹了windows的文件系統(tǒng)機(jī)制引發(fā)的PHP路徑爆破問(wèn)題分析,需要的朋友可以參考下2014-07-07ThinkPHP寫數(shù)組插入與獲取最新插入數(shù)據(jù)ID實(shí)例
這篇文章主要介紹了ThinkPHP寫數(shù)組插入與獲取最新插入數(shù)據(jù)ID的方法,實(shí)例講述了ThinkPHP基于數(shù)組操作數(shù)據(jù)庫(kù)的方法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11Linux下PHP連接Oracle數(shù)據(jù)庫(kù)
PHP連接Oracle是通過(guò)oci8訪問(wèn)的。需要在web服務(wù)器上安裝oracle-instantcilent,使PHP支持Oracle擴(kuò)展包OCI。下面我們來(lái)一步步詳細(xì)介紹下如何使用PHP連接Oracle。2014-08-08PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例
這篇文章主要介紹了PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例,本文講解了目標(biāo)、角色、應(yīng)用場(chǎng)景、優(yōu)勢(shì)等內(nèi)容,并給出代碼實(shí)例,需要的朋友可以參考下2015-05-05thinkphp的dump函數(shù)無(wú)輸出實(shí)例代碼
下面小編就為大家?guī)?lái)一篇thinkphp的dump函數(shù)無(wú)輸出實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11