ziparchive папки и файлы исключают текущую папку - PullRequest
0 голосов
/ 05 апреля 2019

У меня есть папка с файлами и подпапками:

base_folder_name
  -folder_01
    -file_01
    -file_02
  -folder_02
    -file_01
    ....etc

Когда я заархивирую папку, я получу именно эту структуру.

Но я бы хотел исключить основную папку 'base_folder_name' из zip-файла, чтобы при извлечении файлов структура была:

-folder_01
    -file_01
    -file_02
  -folder_02
    -file_01
    ....etc

Это мой код:

$rootPath = public_path() .'/storage/base_folder_name'; 
    //Initialize archive object
    $zip = new ZipArchive();
    $zip->open(public_path() .'/storage/base_folder_name/'base_folder_name.zip', ZipArchive::CREATE);
            $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);
            log::info($relativePath);
            // Add current file to archive
            $zip->addFile($filePath, $relativePath);
        }
    }
...