doushuangai9733 2017-08-28 19:37
浏览 35
已采纳

将文件添加到现有ZIP文件

EDIT: The question is answered below. If you want to ZIP a directory/folder like what I did, see this: How to zip a whole folder using PHP

I have an application with a timer that automatically download a ZIP file from my server.

But the ZIP file is being changed every day.

When someone use the application, the application user will get a "550 File unavailable" error because the ZIP file was removed and added again (that's because the application timer is executed every 900 milliseconds).

So instead of removing the ZIP file and creating it again with the new data, how to add the new data without re-creating the ZIP file?

Currently I use this:

$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();

And this code get the contents of the files_to_be_in_zip folder and re-create the "zouch.zip" file with it.

And yes, I know the new data fullpath... it is $recentlyCreatedFile

EDIT: I found this code on 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';
}
?>

But I want to create a directory in the existing ZIP too.

Any help?

Thanks!

  • 写回答

1条回答 默认 最新

  • dtny30176 2017-08-28 19:47
    关注

    When you open the zip, you are specifying for it to either be newly created or overwritten in your second parameter. Removing that second parameter should make your script work as it is. Below is your code with the required edits already implemented.

    $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();
    

    However, if you have data that are already in the ZIP file but will need to be REPLACED in the future then you have to use ZipArchive::OVERWRITE

    $zip->open('../zouch.zip', ZipArchive::OVERWRITE);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入