0

我正在使用 ZipArchive 类来压缩驻留在服务器上的文件。我可以用 WinRar 解压下载的 ZIP,但不能用 WinZip。如果我通过 FileZilla 直接从服务器下载创建的 ZIP,那么我可以使用 Winzip 解压缩它。

请查看我正在使用的以下脚本:-

            $zip = new ZipArchive();
    //create the file and throw the error if unsuccessful
    if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$archive_file_name>\n");
    }

    //add each files of $file_name array to archive
    foreach($file_names as $files)  {

        $zip->addFile($file_path.$files,$files);    
    }

    $zip->close();

    //get the size of the archive
    $zipped_size = filesize($archive_file_name);

    //set the required headers
    header("Content-Description: File Transfer");
    //header("Content-type: application/zip");
    header("Content-type: application/octet-stream");
    header("Cache-Control: public", false);
    header("Pragma: public");
    header("Expires: 0");
    //header("Content-type: application/x-zip-compressed");
    //header("Content-Type: application/force-download");// some browsers need this
    header("Content-Disposition: attachment; filename=CV-archive.zip");
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Content-Transfer-Encoding: utf-8");
    header('Pragma: public');
    //header("Content-Length:". "$zipped_size");

    ob_clean(); //ob_clean and flush are required to make sure no extra line space is generated before output as it will corrupt the zip archive
    flush();
    readfile("$archive_file_name");
    unlink("$archive_file_name"); // Now delete the temp file (some servers need this option)
    foreach($file_names as $files)  {
        unlink($file_path.$files);  
}
    exit; 
4

2 回答 2

2

您的下载脚本正在添加额外的输出,可能是标签之前的空格,<?php或者可能?>是某些包含文件中的标签之后的空格。或者可能unlink($file_path.$files);正在显示警告。

于 2013-05-08T14:29:49.200 回答
0

Though the question is two years old, I found the same behaviour due to a different issue. Files in my archives were of the format "\1-SOMFILENAME" .. and it happens that win rar has been able to extract the files, but Windows Archiver do not.

It turned out the first \ escapes next character and windows archiver is not able to deal with that.

于 2015-07-08T14:23:16.903 回答