在zip内写入文件或压缩文件


Write file inside zip or compress file

我没有找到任何关于这个的东西,我把我的问题放在这里

例如,如果我有一个文件在zip文件或其他类型的压缩文件中包含此内容:

<?php
$name="Jhon";
$phone="123456789";
$city="London";
?>

例如,我想访问此文件并从表单中写入以更改一些信息,也可以从zip文件或压缩文件,tar等读取所有

内容

我还想将此文件包含在一个文件中的 zip 中以获取信息

可以用

php 做到这一点,或者对于另一方面来说这是不可能的?

谢谢,最好的问候

您可以提取文件,对其进行更改,然后将其重新添加回存档(假设您安装了 ZipArchive 类(。

$zip = New 'ZipArchive;
$res = $zip->open('yourarchive.zip');
if (true === $res) 
{
    $zip->extractTo('/my/destination/dir/', 'test.php');
    $contents = file('*your filename*');
    foreach ($contents as $line) 
    {
        //perform alterations;
    }
    unset ($contents[2]); //remove line 2;
    $contents[] = "aaaa"; //append "aaaa" to the end of the file
    file_put_contents('test.php', $contents);
    // do your edits to the file
    $zip->addFile('test.php');
    $zip->close();
}