我正在使用 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;