phpzip

How to zip a whole folder using PHP


I have found here at stackoveflow some code on how to ZIP a specific file, but how about a specific folder?

Folder/
  index.html
  picture.jpg
  important.txt

inside in My Folder, there are files. after zipping the My Folder, i also want to delete the whole content of the folder except important.txt.

Found this here at stack


Solution

  • Zip a whole folder:

    // Remove any trailing slashes from the path
    $rootPath = rtrim($rootPath, '\\/');
    
    // 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 $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();
    

    Zip a whole folder + delete all files except "important.txt":

    // Remove any trailing slashes from the path
    $rootPath = rtrim($rootPath, '\\/');
    
    // 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);
    
    // Initialize empty "delete list"
    $filesToDelete = array();
    
    // Create recursive directory iterator
    /** @var SplFileInfo[] $files */
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($rootPath),
        RecursiveIteratorIterator::LEAVES_ONLY
    );
    
    foreach ($files as $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);
    
            // Add current file to "delete list"
            // delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
            if ($file->getFilename() != 'important.txt')
            {
                $filesToDelete[] = $filePath;
            }
        }
    }
    
    // Zip archive will be created only after closing object
    $zip->close();
    
    // Delete all files from "delete list"
    foreach ($filesToDelete as $file)
    {
        unlink($file);
    }