在PHP中将文件添加到现有Zip

时间:2013-01-03 12:32:53

标签: php zip archive

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

1 个答案:

答案 0 :(得分:4)

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