【问题标题】:zip with ZipArchive to PPTX errors使用 ZipArchive 压缩到 PPTX 错误
【发布时间】:2018-01-23 13:58:42
【问题描述】:

遇到了一个奇怪的问题。

我有一个包含 PowerPoint 数据的文件夹,我需要将其压缩然后重命名为 pptx。 如果我手动创建一个 .zip 并将文件复制到其中,然后重命名,它会像预期的那样工作,但是当我使用 ZipArchive 归档数据然后重命名创建的文件时,它不起作用。

以编程方式创建的存档也比手动创建的存档小几 kB。 存档比较工具告诉我,我在两个 zip 中都有完全相同的文件,包括隐藏文件。

但这里真的很奇怪:如果我制作另一个空存档,然后通过简单的选择、拖放操作从以编程方式创建的存档中复制粘贴文件,即新存档重命名为 .pptx 并且与第一个手动创建的文件大小相同时将起作用。

    $dir = 'pptx/test/compiled';
    $zip_file = 'pptx/test/file.zip';

    // Get real path for our folder
    $rootPath = realpath($dir);

    // Initialize archive object
    $zip = new \ZipArchive();
    $zip->open($zip_file, \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)
    {
        // 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();

更新

这似乎只是在 Windows 上的问题,在 Mac 上测试时,它可以正常工作。

【问题讨论】:

    标签: php ziparchive


    【解决方案1】:

    我又遇到了这个问题,在 LibreOffice 论坛上寻求帮助后(这个问题是 Impress 特有的),我发现在 Windows 中归档时使用的文件路径有反斜杠,这导致了问题。

    我在归档函数中添加了两行来清理路径:

            // Get real and relative path for current file
            $filePath = $file->getRealPath();
            $relativePath = substr($filePath, strlen($rootPath) + 1);
    
            // Replace backward with forward slashes, for compatibility issues
            $filePath = str_replace('\\', '/', $filePath);
            $relativePath = str_replace('\\', '/', $relativePath);
    
            // Add current file to archive
            $zip->addFile($filePath, $relativePath);
    

    现在我的代码运行没有问题。 (我使用 LibreOffice headless 作为 exec() 来转换为 pdf,这在以前是行不通的)

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多