【问题标题】:Add Files to Existing Zip in PHP [closed]将文件添加到 PHP 中的现有 Zip [关闭]
【发布时间】:2013-01-03 12:32:52
【问题描述】:

如何通过 PHP 将文件添加到现有的 zip?我有一个 zip 文件 test.zip 包含五个文件,现在我想在该 zip 中再添加 20 个文件。所以我更新的 zip 将包含 25 个文件。

【问题讨论】:

    标签: php zip archive


    【解决方案1】:
    array_push('sortname','fullpath');                      
    $zip = new ZipArchive();
    if ($zip->open('/path_to_folder/fileName'.'.zip',ZIPARCHIVE::CREATE) !== TRUE) {
        die ("Could not open archive");
    }
    foreach ($fileList as $key=>$value) {
        $zip->addFile($key,$value) or die ("ERROR: Could not add file: $f");
    }
    $zip->close();
    
    it will create new zip if it does not exist,otherwise add files to the same zip file
    

    我在我的公共文件夹中创建了一个名为 index.php 的 zip 文件,其中压缩了 index.php 文件。然后我编写了以下代码以编程方式向其中添加 2 个新文件,它对我来说非常有效

    $zip = new ZipArchive();
    $filename = APPLICATION_PATH."\..\public\index.zip";
    
    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
        exit("cannot open <$filename>\n");
    }
    try{
        $zip->addFile(APPLICATION_PATH."\..\public\index4.php","index4.php") or die ("ERROR: Could not add file:");
         $zip->addFile(APPLICATION_PATH."\..\public\web.config","web.config") or die ("ERROR: Could not add file:");
    
    }
    catch (Exception $e){
        echo  $e->getMessage();exit;
    }
    echo "numfiles: " . $zip->numFiles . "\n";
    echo "status:" . $zip->status . "\n";
    $zip->close();
    

    【讨论】:

    • 它适用于新的 zip,但不适用于现有的 zip。请在发布答案之前尝试此操作。
    • 我在我的公共文件夹中创建了一个名为 index.php 的 zip 文件,其中压缩了 index.php 文件。然后我编写了以下代码以编程方式向其中添加 2 个新文件,它对我来说非常有用, @biswarup 请重新检查一下,我已经对答案进行了更改
    猜你喜欢
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多