php zip 内存,PHP核心ZipArchive与ZipStream-PHP

根据我的经验,最好的解决方案是使用aws-sdk-php通过启用了registerStreamWrapper()的s3client访问S3上的对象。然后使用fopen从S3流式传输对象并将该流直接提供给ZipStream的addFileFromStream()函数,并让ZipStream从那里获取它。没有ZipArchive,没有大量内存开销,没有在服务器上创建zip或从Web服务器上的S3复制文件以随后用于流式传输zip。

所以:

//...

$s3Client->registerStreamWrapper(); //required

//test files on s3

$s3keys = array(

"ziptestfolder/file1.txt",

"ziptestfolder/file2.txt"

);

// Define suitable options for ZipStream Archive.

$opt = array(

'comment' => 'test zip file.',

'content_type' => 'application/octet-stream'

);

//initialise zipstream with output zip filename and options.

$zip = new ZipStream\ZipStream('test.zip', $opt);

//loop keys useful for multiple files

foreach ($s3keys as $key) {

// Get the file name in S3 key so we can save it to the zip

//file using the same name.

$fileName = basename($key);

//concatenate s3path.

$bucket = 'bucketname';

$s3path = "s3://" . $bucket . "/" . $key;

//addFileFromStream

if ($streamRead = fopen($s3path, 'r')) {

$zip->addFileFromStream($fileName, $streamRead);

} else {

die('Could not open stream for reading');

}

}

$zip->finish();如果您在Symfony控制器操作中使用ZipStream,请参阅此答案:https://stackoverflow.com/a/44706446/136151

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值