以相同目录格式创建所有目录和文件的zip

时间:2018-07-06 08:23:06

标签: php ziparchive php-zip-archive

我正在尝试使用与计算机中相同的目录格式来压缩文件数组的zip,我编写的脚本正在工作,唯一的问题是它仅用第二个覆盖目录的第一个元素目录数组中的最后一个元素在zip文件中,我需要从提供的目录数组中添加所有文件。这是我的代码,请告诉我我在做什么错。

// Get real path for our folder
$rootPaths = array(
               'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
               'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
             );

$valid_files = array();
if (is_array($rootPaths)) {
     foreach ($rootPaths as $new_files) {
          if (file_exists($new_files)) {
               $valid_files[] = $new_files;
          }
     }
}

// Initialize archive object
if (count($valid_files)) {
    $zip = new ZipArchive();
    $zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
foreach ($valid_files as $rootPath) {
    $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();
}

此脚本仅使用以下目录的文件和目录结构来创建zip文件。

  

'D:\ xampp \ htdocs \ moko \ wp-content \ plugins \ moko \ templates'

1 个答案:

答案 0 :(得分:0)

您根本不需要创建变量$valid_files。您正在讨论的循环是检查目录中的有效文件,仅检查$rootPaths变量中的目录是否存在。另外,我还更改了将文件添加到zip的方式。每次执行时,循环都会覆盖$files变量。看下面的代码。

// Get real path for our folder
$rootPaths = array(
    'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
    'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
);

if (is_array($rootPaths)) {
    foreach ($rootPaths as $key => $new_files) {
        if (!file_exists($new_files)) {
            unset($rootPaths[$key]);
        }
    }
}
// Initialize archive object
if (count($rootPaths)) {
    $zip = new ZipArchive();
    $zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
    /** @var SplFileInfo[] $files */
    foreach ($rootPaths as $rootPath) {
        $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();
}