Php 可以在.zip文件中写入数据吗

Php 可以在.zip文件中写入数据吗,php,file-put-contents,Php,File Put Contents,根据这个例子: <?php $file = 'people.txt'; // Open the file to get existing content $current = file_get_contents($file); // Append a new person to the file $current .= "John Smith\n"; // Write the contents back to the file file_put_contents($file, $curre

根据这个例子:

<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

也可以将数据写入.zip文件吗

<?php
$file = 'people.zip';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

创建临时文件并将数据写入其中

// #1 create tmp data file
$data = 'Hello, World!';
$dataFile = 'file.txt';
file_put_contents($dataFile, $data);
创建zip文件并将数据文件放入其中

// #2 create zip archive
$zip = new ZipArchive();
$zipFile = 'test.zip';
if ($zip->open($zipFile, ZipArchive::CREATE)) {
    $zip->addFile($dataFile, $dataFile);
}
$zip->close();
如果创建zip后不需要,也可以选择删除数据文件

// #3 delete
unlink($dataFile);

创建临时文件并将数据写入其中

// #1 create tmp data file
$data = 'Hello, World!';
$dataFile = 'file.txt';
file_put_contents($dataFile, $data);
创建zip文件并将数据文件放入其中

// #2 create zip archive
$zip = new ZipArchive();
$zipFile = 'test.zip';
if ($zip->open($zipFile, ZipArchive::CREATE)) {
    $zip->addFile($dataFile, $dataFile);
}
$zip->close();
如果创建zip后不需要,也可以选择删除数据文件

// #3 delete
unlink($dataFile);

您可以将数据写入文件,然后将文件放入zip。好的,bu tit不可能将数据直接写入.zip文件?不,还有另一种方法,使用CLI提取文件并获取文件内容附加新内容并将新文件添加到zip并替换为旧文件位置。您可以将数据写入文件,并将文件放入zip。好的,bu tit不可能将数据直接写入.zip文件?不,还有另一种方法可以做到这一点,使用CLI提取文件并获取文件内容附加新内容并将新文件添加到zip并替换为旧文件位置。