【问题标题】:Add files to an existing ZIP file将文件添加到现有 ZIP 文件
【发布时间】:2017-08-29 05:05:36
【问题描述】:

编辑:下面回答了这个问题。如果您想像我一样压缩目录/文件夹,请参阅:How to zip a whole folder using PHP

我有一个带有计时器的应用程序,可以自动从我的服务器下载 ZIP 文件。

但是 ZIP 文件每天都在更改。

当有人使用应用程序时,应用程序用户将收到“550 文件不可用”错误,因为 ZIP 文件被删除并重新添加(这是因为应用程序计时器每 900 毫秒执行一次)。

那么,与其删除 ZIP 文件并使用新数据重新创建它,如何在不重新创建 ZIP 文件的情况下添加新数据?

目前我用这个:

$zip = new ZipArchive;
// Get real path for our folder
$rootPath = realpath('../files_to_be_in_zip');

// Initialize archive object
$zip = new ZipArchive();
$zip->open('../zouch.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)
{
    // 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();

这段代码获取files_to_be_in_zip文件夹的内容并用它重新创建“zouch.zip”文件。

是的,我知道新的数据完整路径...它是$recentlyCreatedFile

编辑:我在http://php.net/manual/en/ziparchive.addfile.php找到了这段代码

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->addFile('/path/to/index.txt', 'newname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

但我也想在现有的 ZIP 中创建一个目录。

有什么帮助吗?

谢谢!

【问题讨论】:

    标签: php


    【解决方案1】:

    当您打开 zip 时,您在第二个参数中指定它是新创建的还是覆盖的。删除第二个参数应该使您的脚本按原样工作。以下是您的代码,已实现所需的编辑。

    $zip = new ZipArchive;
    // Get real path for our folder
    $rootPath = realpath('../files_to_be_in_zip');
    
    $zip->open('../zouch.zip');
    
    // 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();
    

    但是,如果您有数据已经​​在 ZIP 文件中,但将来需要被替换,那么您必须使用 ZipArchive::OVERWRITE

    $zip->open('../zouch.zip', ZipArchive::OVERWRITE);
    

    【讨论】:

    • 删除ZipArchive::CREATE | ZipArchive::OVERWRITE 参数修复了一切!非常感谢:-)
    • 乐于助人,感谢您及时接受答复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多