简体   繁体   English

php 中的压缩 zip 文件不显示拉丁字符

[英]Compressed zip file in php doesn't display latin characters

I apologize for my english.我为我的英语道歉。 I need a little help, please.我需要一点帮助,请。 I want to compress a .csv into zip.我想将 .csv 压缩成 zip。 The code does the job but when I open the .csv file, the latin characters (á,é,í,ó,ú,ñ,etc.) are not display correctly.该代码可以完成这项工作,但是当我打开 .csv 文件时,拉丁字符(á、é、í、ó、ú、ñ 等)无法正确显示。 The UTF-8 encoding only works if the file isn't compressed. UTF-8 编码仅在文件未压缩时才有效。 This is my code:这是我的代码:

  $archivo = "REPORTE_PUNTOS.csv";
  $filename = "REPORTE_PUNTOS";

  $zip = new ZipArchive();

  if ($zip->open($filename, ZIPARCHIVE::CREATE) === true) {
    $zip->addFile($archivo);
    $zip->close();
  } else {
    echo 'Error creando '.$filename;
  }

  if (file_exists($filename)) {
    //Download zip file
    header("Content-Encoding: UTF-8");
    header('Content-type: "application/zip"');
    header('Content-Disposition: attachment; filename="'.$filename.'.zip"');

    readfile($filename);

    //Se borra el archivo zip
    unlink($filename);
  }

Thanks.谢谢。

Try to download it with chunks and headers should be like this.尝试用块下载它,标题应该是这样的。 GOOD JOB KEEP GOING.干得好,继续。

<?php 

  $archivo = "REPORTE_PUNTOS.csv";
  $filename = "REPORTE_PUNTOS.zip";

  $zip = new ZipArchive();

  if ($zip->open($filename, ZIPARCHIVE::CREATE) === true) {
    $zip->addFile($archivo);
    $zip->close();
  } else {
    echo 'Error creando '.$filename;
  }

  if (file_exists($filename)) {

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public", false); 
header("Content-Description: File Transfer"); 
header("Content-Type: application/x-zip-compressed"); 
header("Accept-Ranges: bytes"); 
header("Content-Disposition: attachment; filename=\"" . $filename . "\";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: " . filesize($filename)); 
    $chunkSize = 1024 * 1024;
    $handle = fopen($filename, 'rb');
    while (!feof($handle))
    {
        $buffer = fread($handle, $chunkSize);
        echo $buffer;
        ob_flush();
        flush();
    }
    fclose($handle);

    unlink($filename);
  }

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

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