【问题标题】:How to ZIP an entire folder in PHP, even the empty ones? [duplicate]如何在 PHP 中压缩整个文件夹,甚至是空文件夹? [复制]
【发布时间】:2019-09-19 11:23:30
【问题描述】:

我正在尝试从我的应用程序 (Laravel 5.8) 上的文件夹中下载一个 zip,它正在工作,但它们会跳过所有空文件夹,我需要它们在 zip 中。

我已经尝试过来自 composer 的 ZipArchive (php) 和 chumper/zipper。

知道我该怎么做吗?

这是一个 Linux 服务器,运行 MySQL 5、PHP 7.2 和 Apache2。

$files = glob($folder_path);
\Zipper::make($folder_path.'/'.$folder_main_name.'.zip')->add($files)->close();

这个库只接受 glob,但如果你有任何可行的解决方案,我可以很容易地放弃它。

【问题讨论】:

  • 您是否尝试将点文件(如“.keep”或其他)放入空文件夹中以查看会发生什么?
  • 您是否尝试过不使用 lib 而只是从 Linux 调用 zip 命令?它比人们试图在各种库中重新发明的方式更快、功能更丰富。如果您没有构建应该移植到 Windows 的东西,那么这可以解决您的所有问题。如果你愿意走这条路,请告诉我。
  • 是的,我添加了一个名为“Icon_”的文件并且工作正常。但是我需要空文件夹,因为它将上传到另一台服务器上,并且它必须是空的。
  • @asiby 我还没有尝试过。我现在就去做。泰!
  • 是的。对于操作系统可以做的许多事情,我宁愿让它去做。例如,根据复杂的标准查找一些文件或文件夹。我需要归档所有 git repos,然后我做exec("find %baseDir -type d -name .git", $output, $returnCode)。我使用strtr()%baseDir 替换为我正在扫描的目录。对于压缩东西,我会使用tar 并使用exec("tar czf compressed.tar.gz folder-to-compress", $output, $returnCode)

标签: php zip hide


【解决方案1】:
// 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)
{
    // Is this a directory?
    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);
    }
    else {
        $end2 = substr($file,-2);
        if ($end2 == "/.") {
           $folder = substr($file, 0, -2);
           $zip->addEmptyDir($folder);
        }
    }
}

// Zip archive will be created only after closing object
$zip->close();

** 试试这个。在我的头顶上,未经测试。但这是一般的想法。

【讨论】:

  • 已经试过了,同样的问题,如果文件夹是空的,就会被跳过。
  • 已更新以反映空文件夹。
  • 仍然跳过空文件夹:/ 我还尝试确保 else 块上的文件夹为空,但不起作用。
  • 应该可以的。现在不在我的电脑附近进行调试。但应该会有点。
  • 好的,所以它同时提供了 .和 .. 目录列表中的条目,而不仅仅是目录。试试上面的。
【解决方案2】:
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zipFilename, 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)
{
    // Get real and relative path for current file
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);

    if (!$file->isDir())
    {
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }else {
        if($relativePath !== false)
            $zip->addEmptyDir($relativePath);
    }
}

// Zip archive will be created only after closing object
$zip->close();

嗨。我修改了之前的答案,现在我得到了包含空文件夹的 zip 文件。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多