简体   繁体   English

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

[英]Edit all Files in all ZIP Files in a Directory (Find + Replace)

I'm trying to make a script that will look at a particular directory, open all of zip files in that directory, find + replace a variable I assign within all files in all ZIP files, and then close/re-zip them. 我正在尝试制作一个脚本,该脚本将查看特定目录,打开该目录中的所有zip文件,查找并替换我在所有ZIP文件中的所有文件中分配的变量,然后关闭/重新压缩它们。

Typically, the types of files within the zip will be .htm, .html, .php, and .txt. 通常,压缩文件中的文件类型为.htm,.html,.php和.txt。

So far, I've gotten to here... 到目前为止,我已经到了...

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

Even though all of the files in the ZIP are listed properly if I print_r them, the script will only replace one file within the zip. 即使我将print_r正确列出了ZIP中的所有文件,该脚本也只会替换zip中的一个文件。 Every refresh it will replace a different file. 每次刷新都会替换一个不同的文件。

Of course this also only works with one particular ZIP that is named. 当然,这也仅适用于一个名为ZIP的特定ZIP。 I'd like the script to take ALL Zip files in the directory, and replace text in ALL files in ALL Zip files. 我希望脚本将目录中的所有Zip文件接收,并替换所有Zip文件中所有文件的文本。

Find variable = #topofpage 查找变量= #topofpage

Replace variable = # 替换变量=#

Thank you for the help! 感谢您的帮助!

I found that to get this to work I needed to get the count of files within the archive BEFORE any loop - otherwise the count constantly increased as the loop ran and consequently the file count in the zip grew. 我发现要使其正常工作,我需要在进行任何循环之前获取存档中文件的数量-否则,随着循环的运行,数量会不断增加,因此zip中的文件数量也会增加。

The below appears to function correctly given that minor change. 进行较小的更改后,以下内容似乎可以正常运行。

The below is based upon a directory within the current script directory called zips which contains any number of zip archive files. 以下内容基于当前脚本目录中名为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 );
    }
}

The zip files used to test contained a wordlist` file - the contents of which after processing are similar to the following - but run to several thousand lines ) 用于测试的zip文件包含一个wordlist`文件-经过处理后其内容类似于以下内容-但运行了数千行)

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

I would do it this way: 我会这样:

$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
      ...  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM