php - 如何在 php 生成的 zip 中排除文件

标签 php zip zlib

我需要知道如何从 php 生成的 zip 中排除文件/文件夹。这是我的代码:

public function archiveBackup($name = '', $source = '', $destination = '') {
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }
    $zip = new ZipArchive();
    if (!$zip->open($destination.$name, ZIPARCHIVE::CREATE)) {
        return false;
    }
    $source = str_replace('\\', '/', realpath($source));
    if (is_dir($source) === true) {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
        foreach ($files as $file) {
            $file = str_replace('\\', '/', realpath($file));
            if(is_dir($file) === true) {
                $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
            }
            else if(is_file($file) === true) {
                $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
            }       
        }
    }
    else if (is_file($source) === true) {
        $zip->addFromString(basename($source), file_get_contents($source));
    }
    $zip->close();
    return true;
}

示例:我需要排除文件夹“themes”和“style.css”文件,该怎么做?

最佳答案

    $exclude = array('themes', 'style.css');

    foreach ($files as $file) {
      if (!in_array($file, $exclude)) {
        $file = str_replace('\\', '/', realpath($file));
        if(is_dir($file) === true) {
            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
        }
        else if(is_file($file) === true) {
            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
        }
      }
    }

关于php - 如何在 php 生成的 zip 中排除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11024941/

相关文章:

php - 无法使用 jQuery 正确访问复杂的 json 对象

java - android okohttp 将数据插入 php 不起作用

bash - 使用 bash 脚本在 hadoop 中压缩不同的目录

c++ - 用zlib提取一个gz文件内容并保存

c++ - 使用 zLib 删除无效

php - Jquery Ajax获取外部url错误

php - MySQL 和 PHP : Atomicity and re-entrancy of a PHP code block executing two subsequent queries - how dangerous?

java - 将大型 CSV 流写入内存中的 ZipOutputStream 消耗的内存是否与 CSV 或潜在 zip 的大小一样多?

java - 更新 .jar 的 jar 命令的快速替代方法

python - 使用 ByteArrays 解压缩 Zlib 字符串