编辑目录中所有ZIP文件中的所有文件(查找和替换)

时间:2018-08-22 06:11:25

标签: php replace find zip archive

我正在尝试制作一个脚本,该脚本将查看特定目录,打开该目录中的所有zip文件,查找并替换我在所有ZIP文件的所有文件中分配的变量,然后关闭/重新压缩他们。

通常,zip中的文件类型为.htm,.html,.php和.txt。

到目前为止,我已经到了这里...

$zip = new ZipArchive;
$zip->open('testzip.zip'); 

for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
// ...

$oldContents = $zip->getFromName($filename);
//Modify contents:
$newContents = str_replace('#topofpage', '#', $oldContents);
//Delete the old...
$zip->deleteName($filename);
//Write the new...
$zip->addFromString($filename, $newContents);
//And write back to the filesystem.
$zip->close();
}

即使我将print_r正确列出了ZIP中的所有文件,该脚本也只会替换zip中的一个文件。每次刷新都会替换一个不同的文件。

当然,这也仅适用于一个名为ZIP的特定ZIP。我希望脚本将目录中的所有Zip文件提取,并替换所有Zip文件中所有文件的文本。

查找变量= #topofpage

替换变量=#

谢谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我发现,要使其正常工作,我需要在任何循环之前获取存档中文件的数量-否则,随着循环的运行,数量会不断增加,因此zip中的文件数量也会增加。

鉴于较小的更改,以下内容似乎可以正常工作。

以下内容基于当前脚本目录中的目录zips,该目录包含任意数量的zip存档文件。

$search='absent';
$replace=' --BRIGHT YELLOW HIPPOPOTAMUS-- ';



function modifyzip($archive,$search,$replace){
    $zip = new ZipArchive();
    $status = $zip->open( $archive );

    if( $status == true ){
        /* Get the count of files BEFORE the loop */
        $filecount = $zip->numFiles;

        for( $i=0; $i < $filecount; $i++ ){
            $name = $zip->getNameIndex( $i );
            $content = $zip->getFromName( $name );
            $edited = str_replace( $search, $replace, $content );
            $zip->deleteName( $name );
            $zip->addFromString( $name, $edited );
        }   
    }
    $zip->close();  
}

$dir=__DIR__ . '/zips/';
$col=glob( $dir . '*.zip' );
if( $col && count( $col ) > 0 ){
    foreach( $col as $archive ){
        call_user_func( 'modifyzip',$archive,$search,$replace );
    }
}

用于测试的zip文件包含一个wordlist`文件-经过处理后其内容类似于以下内容-但运行了几千行)

abnormal
abnormality
 --BRIGHT YELLOW HIPPOPOTAMUS-- 
absolute
absolve
absorption
abstain
abstraction
abused
abusive
accelerated
accepted

答案 1 :(得分:0)

我会这样:

$dir_array = scandir ( $path_to_zip_dir );
for($i = 0; $i<sizeof($dir_array); $i++) {
  if(substr($dir_array[$i],strlen($dir_array[$i])-4) == ".zip") {
    mkdir($path_to_zip_dir."temp");
    $zip = new ZipArchive;
    $res = $zip->open($path_to_zip_dir.$dir_array[$i]); 
    if ($res === TRUE) {
      $zip->extractTo($path_to_zip_dir."temp/");
      $zip->close();
      $temp_dir_files_array = scandir ($path_to_zip_dir."temp/");
      for($j=0; $j<sizeof($temp_dir_files_array); $j++) {
        $fh = fopen($temp_dir_files_array[$j],'r');
        $file_text = "";
          while ($line = fgets($fh)) {
            //Gets read by line and you can switch out what you need
            str_replace($what_you_are_looking_for,$what_you_replace_with, $line);
            $file_text = $line;
          }
        fclose($fh);
        unlink($temp_dir_files_array[$j]);
        $fh = fopen($temp_dir_files_array[$j],'w');
        fwrite($fh,$file_text);
        fclose($fh);

      }
    }
  }

      //Now repack the zip and delete the tempdirectory
      ...  
}
相关问题