Antonio Abrantes
Antonio Abrantes

Reputation: 591

ZipArchive compression to create an ODT file

I am trying to create an ODT file in PHP using ZipArchive(). It creates an ODT corrupted file (ccorrupted.odt). If I change the extension of the created file to zip (corrupted.zip) and unzip file usinf 7zip and zip it again, the file is now perfect. What could be the problem ? The corrupted file can be obtained in https://cientistaspatentes.com.br/sinergias/corrupted.odt enter image description here

function create_zip($files = array(),$destination = '',$files2 = array(),$overwrite = false) {
       //if the zip file already exists and overwrite is false, return false
       if(file_exists($destination) && !$overwrite) { return false; }
       //vars
       $valid_files = array();
       //if files were passed in...
       if(is_array($files)) {
         //cycle through each file
         foreach($files as $file) {
           //make sure the file exists
           //if(file_exists($file)) {
             $valid_files[] = $file;
           //}
         }
       }
       
       //print_r($valid_files);exit();
       //if we have good files...
       if(count($valid_files)) {
         //create the archive
         $zip = new ZipArchive();
         //echo "<br>";
         //echo "destination  -  ".$destination."<br>";
         //echo "overwrite  -  ".$overwrite."<br>";

         if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) {
           return false;
         }
//       if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
//         return false;
//       }
         //add the files
         //echo "<br>";
         //print_r($valid_files);
         $i=0;
         foreach($valid_files as $file) {
           $file_n=$file;
           if (strrpos($file,'/')) {
               //echo 'Oi '.strrpos($file,'/').' '.substr($file,strrpos($file,'/')+1).' '.$file.'<br>';
               $file_n=substr($file,strrpos($file,'/')+1);
           }
           //echo $file_n.'  ';
           //echo $zip->addFile($file,$file_n);
           
           $auxF = file_get_contents($file);
           if (!(file_exists($file)))
           {
                $msg_erro = "Erro em compactador.loc.php linha 136. $file ";
                //enviar_email('gerente',$msg_erro,$habilitar_email);
                echo "$msg_erro<BR>";
           }
           $zip->addFromString($files2[$i],$auxF);
           //echo '<br>';
           $i++;
         }
         //debug
         //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
         //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status.'<br>';

         //close the zip -- done!
         $zip->close();
         //echo "fim";exit();
         //check to make sure the file exists
         return file_exists($destination);
       }
       else
       {
         return false;
       }
    }

Upvotes: 0

Views: 61

Answers (1)

Antonio Abrantes
Antonio Abrantes

Reputation: 591

Chris Haas solved the question. Originally Ia have in my php file

    $msg = gera_manifestacao_cgrec($link,$adm,$fileorigem,'','',$numerocd,$divisao,$examinador,$hierarquia,$valores,$divisoes);
?>

<!doctype html>
<HTML>
<HEAD>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

gera_manifestacao_cgrec creates an ODT file. These html aftwards were being inserted in my ODT file. I just put and exit() an solved the problem

    $msg = gera_manifestacao_cgrec($link,$adm,$fileorigem,'','',$numerocd,$divisao,$examinador,$hierarquia,$valores,$divisoes);
    exit();
?>

<!doctype html>
<HTML>
<HEAD>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

Upvotes: 1

Related Questions