ZipArchive 压缩以创建 ODT 文件

问题描述 投票:0回答:1

我正在尝试使用 ZipArchive() 在 PHP 中创建 ODT 文件。它会创建 ODT 损坏的文件 (ccorrupted.odt)。如果我将创建的文件的扩展名更改为 zip (corrupted.zip) 并使用 inf 7zip 解压缩文件并再次压缩,则该文件现在是完美的。可能是什么问题呢 ?损坏的文件可以在 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;
       }
    }
php odt
1个回答
0
投票

克里斯·哈斯解决了这个问题。最初我在我的 php 文件中有

    $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 创建 ODT 文件。这些 html 随后被插入到我的 ODT 文件中。我只是 put 并 exit() 解决了问题

    $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" />
© www.soinside.com 2019 - 2024. All rights reserved.