1

My project consist in a big mongodb/gridfs files database, where each file belongs to a "folder" document, and these folders are ordered following a tree structure with one root node.

Now I need to zip a part of the tree (or maybe the whole structure), keeping the tree and files accordingly (the whole tree will be approximately 10To big).

What's the best strategy to do so ? All files are dynamically provided by gridFS (the cache musnt be used in the zip process).

What's the best way to do so ? Thanks for your help !

ovesco
  • 633
  • 1
  • 8
  • 22
  • 1
    In general I would advise against using the `zip` algorithm for this at all. Take a look at compressed `tar` archives instead. Two advantages: 1. tar archives can be written (to file) in a sequential manner which keeps the memory footprint of the process small and constant and you can use a better compression algorithm than the old `zip`: `gzip` or `bzip2`. There is a PEAR package for that: http://pear.php.net/package/Archive_Tar, probably many other implementations exist. – arkascha Nov 25 '15 at 10:20
  • Da codez, plz! You might want to read [How do I ask a good question](http://stackoverflow.com/help/how-to-ask), which enhances the probability for getting a useful answer _drastically_. You might find [ESR](https://en.m.wikipedia.org/wiki/Eric_S._Raymond)'s excellent essay [How To Ask Questions The Smart Way](http://catb.org/~esr/faqs/smart-questions.html) helpful. – Markus W Mahlberg Dec 02 '15 at 04:42

1 Answers1

2

See answer by 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();
Community
  • 1
  • 1
Arithran
  • 1,219
  • 3
  • 11
  • 22