Files
wsa_server/libraries/MakeZip.php
T
2026-09-14 16:32:07 +08:00

52 lines
1.2 KiB
PHP

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 压缩整个目录
*/
class MakeZip
{
/**
* description:主方法:生成压缩包
* @author: MY
* @param $dir_path 想要压缩的目录:如 './demo/'
* @param $zipName 压缩后的文件名:如 './folder/demo.zip'
* @return string
*/
function zip($dir_path, $zipName)
{
// Get real path for our folder
$rootPath = realpath($dir_path);
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
return true;
}
}