用过zip修复工具 修复之后7-zip就能打开了 但是希望不修复也可以用7-zip打开 求帮助
<?php
$log = "/work/log/logbook.zip";
$dir="/work/log";
$del_Log=unlink($log);//删除原有logbook.zip
//重新生成新logbook.zip
class HZip
{
private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir($folder);
while (false !== $f = readdir($handle)) {
if ($f != '.' && $f != '..') {
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $exclusiveLength);
if (is_file($filePath)) {
$zipFile->addFile($filePath, $localPath);
} elseif (is_dir($filePath)) {
// 添加子文件夹
$zipFile->addEmptyDir($localPath);
self::folderToZip($filePath, $zipFile, $exclusiveLength);
}
}
}
closedir($handle);
}
/**
* Zip a folder (include itself).
* Usage:
* HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
*
* @param string $sourcePath Path of directory to be zip.
* @param string $outZipPath Path of output zip file.
*/
public static function zipDir($sourcePath, $outZipPath)
{
$pathInfo = pathInfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$sourcePath=$parentPath.'/'.$dirName;//防止传递'folder' 文件夹产生bug
$z = new ZipArchive();
$z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件
//$z->addEmptyDir($dirName);//建立文件夹
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
$z->close();
}
}
//使用方法
HZip::zipDir($dir, $log);
//下载zip包
$file_name="logbook.zip";
//$file_name=iconv("utf-8","gb2312",$file_name);
$file_path="/work/log/logbook.zip";
if(!file_exists($file_path))
{
echo "没有该文件文件";
return 0;
}
$fp=fopen($file_path,"r");
$file_size=filesize($file_path);
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length:".$file_size);
Header("Content-Disposition: attachment; filename=".$file_name);
$buffer=1024;
$file_count=0;
while(!feof($fp) && $file_count<$file_size)
{
$file_con=fread($fp,$buffer);
$file_count+=$buffer;
echo $file_con;
}
fclose($fp);
?>