我用的是TP5的框架。
一、安装Zipstream依赖
composer require mcnetic/zipstreamer
二、在extend/org目录下创建一个类Steamer.php
<?php
namespace org;
use ZipStreamer\ZipStreamer;
class Streamer {
// streamer instance
private $streamerInstance;
public function __construct() {
$this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]);
}
/**
* Send HTTP headers
* @param string $name
*/
public function sendHeaders($name) {
$extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
$fullName = $name . $extension;
$this->streamerInstance->sendHeaders($fullName);
}
/**
* Stream directory recursively
* @param string $dir
* @param string $internalDir
*/
public function addDirRecursive($dir, $internalDir='') {
$dirname = basename($dir);
$rootDir = $internalDir . $dirname;
if (!empty($rootDir)) {
$this->streamerInstance->addEmptyDir($rootDir);
}
$internalDir .= $dirname . '/';
// prevent absolute dirs
$internalDir = ltrim($internalDir, '/');
$handler = opendir($dir); //打开当前文件夹由$path指定。
while (($filename = readdir($handler)) !== false) {
if ($filename != "." && $filename != "..") {
$file = $dir . '/' . $filename;
if (is_file($file)) {
$filesize = filesize($file);
$fh = fopen($file, 'r');
$this->addFileFromStream($fh, $internalDir . $filename, $filesize);
fclose($fh);
} elseif (is_dir($file)) {
$this->addDirRecursive($file, $internalDir);
}
}
}
}
/**
* Add a file to the archive at the specified location and file name.
*
* @param string $stream Stream to read data from
* @param string $internalName Filepath and name to be used in the archive.
* @param int $size Filesize
* @return bool $success
*/
public function addFileFromStream($stream, $internalName, $size) {
if ($this->streamerInstance instanceof ZipStreamer) {
return $this->streamerInstance->addFileFromStream($stream, $internalName);
} else {
return $this->streamerInstance->addFileFromStream($stream, $internalName, $size);
}
}
/**
* Add an empty directory entry to the archive.
*
* @param string $dirName Directory Path and name to be added to the archive.
* @return bool $success
*/
public function addEmptyDir($dirName) {
return $this->streamerInstance->addEmptyDir($dirName);
}
/**
* Close the archive.
* A closed archive can no longer have new files added to it. After
* closing, the file is completely written to the output stream.
* @return bool $success
*/
public function finalize() {
return $this->streamerInstance->finalize();
}
}
提醒下:他这个压缩格式是zip64的,我反正碰到了解压失败的问题,格式不对。所以我改了zipstreamer的源码,mcnetic\zipstreamer\src下的ZipStreamer.php,将true:
private $zip64 = True;
改成了false,改成false的话,他的格式就是zip格式了。
private $zip64 = False;
三、在下载的逻辑里实例化类
$streamer = new \org\Streamer();
1、设置头部信息
$streamer->sendHeaders($title); //$title就是打包下载的文件名
2、设置清除缓冲区、脚本执行限制的时间及用户断开链接后继续执行
while (ob_get_level()) {
ob_end_clean();
}
set_time_limit(0);
ignore_user_abort(true);
3、下载打包
//我这是foreach遍历文件中的一段代码,参考下
$file = $dir . '/' . $value['path'];
if (is_file($file)) {
$fileSize = filesize($file);
$fh = fopen($file, 'r');
$streamer->addFileFromStream($fh, basename($file), $fileSize);
fclose($fh);
} elseif (is_dir($file)) {
$streamer->addDirRecursive($file);
}
4、流结束
$streamer->finalize();