简体   繁体   中英

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.

Typically, the types of files within the zip will be .htm, .html, .php, and .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. Every refresh it will replace a different file.

Of course this also only works with one particular ZIP that is named. I'd like the script to take ALL Zip files in the directory, and replace text in ALL files in ALL Zip files.

Find variable = #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.

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.

$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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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