簡體   English   中英

PHP壓縮大型目錄結構的最佳方法

[英]Php best way to zip huge directory structure

我的項目包含一個大的mongodb / gridfs文件數據庫,其中每個文件都屬於一個“文件夾”文檔,並且這些文件夾是按照具有一個根節點的樹結構排序的。

現在,我需要壓縮樹的一部分(或者可能是整個結構),並相應地保留樹和文件(整個樹大約為10To大)。

這樣做的最佳策略是什么? 所有文件都是由gridFS動態提供的(在zip進程中使用了緩存)。

最好的方法是什么? 謝謝你的幫助 !

參見達多爾(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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM