我的项目包含一个大的mongodb / gridfs文件数据库,其中每个文件都属于一个“文件夹”文档,这些文件夹是按照具有一个根节点的树结构排序的。
现在我需要压缩树的一部分(或者整个结构),相应地保留树和文件(整棵树大约10倍)。
这样做的最佳策略是什么?所有文件都是由gridFS动态提供的(缓存文件在zip过程中使用)。
最好的方法是什么?谢谢你的帮助!
答案 0 :(得分:1)
请参阅Dador https://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php
的回答// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', 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();