【发布时间】:2012-08-04 16:26:27
【问题描述】:
我一直在根据问题在我的网站上测试以下代码: LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing
<?php
// make sure to send all headers first
// Content-Type is the most important one (probably)
//
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');
// use popen to execute a unix command pipeline
// and grab the stdout as a php stream
// (you can use proc_open instead if you need to
// control the input of the pipeline too)
//
$fp = popen('zip -0 -r - file1 file2 file3', 'r');
// pick a bufsize that makes you happy (8192 has been suggested).
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
$buff = fread($fp, $bufsize);
echo $buff;
flush();
}
pclose($fp);
动态流式传输压缩效果很好。
但是有一个问题是它没有将文件大小发送给用户。大概是因为它以动态压缩的形式发送数据。
由于我使用的是零压缩压缩,有什么方法可以让它将尺寸发送给用户,因为尺寸是已知的? 谢谢
【问题讨论】:
标签: php bash streaming zip lamp