您好,登錄后才能下訂單哦!
本篇文章為大家展示了 ZipArchive壓縮函數(shù)怎么在php中使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
php是一個(gè)嵌套的縮寫名稱,是英文超級(jí)文本預(yù)處理語(yǔ)言,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來(lái)做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。
例1、生成zip 壓縮文件
復(fù)制代碼 代碼如下:
<?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 、壓縮文件夾下面的所有文
復(fù)制代碼 代碼如下:
<?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í)也將文件刪除。
上述內(nèi)容就是 ZipArchive壓縮函數(shù)怎么在php中使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。