php 打包zip,打包zip并下载之zipstream-php

将图片或文档按要求打包并下载一个用于PHP的快速简单的流式压缩文件下载器。使用这个库可以省去将Zip文件写入磁盘的麻烦。你可以直接发送给用户,这要快得多。它可以与S3桶或任何PSR7流一起工作。

安装composer require maennchen/zipstream-php

打包并下载(官方例子)// Autoload the dependencies

require 'vendor/autoload.php';

// enable output of HTTP headers

$options = new ZipStream\Option\Archive();

$options->setSendHttpHeaders(true);

// create a new zipstream object

$zip = new ZipStream\ZipStream('example.zip', $options);

// create a file named 'hello.txt'

$zip->addFile('hello.txt', 'This is the contents of hello.txt');

// add a file named 'some_image.jpg' from a local file 'path/to/image.jpg'

$zip->addFileFromPath('some_image.jpg', 'path/to/image.jpg');

// add a file named 'goodbye.txt' from an open stream resource

$fp = tmpfile();

fwrite($fp, 'The quick brown fox jumped over the lazy dog.');

rewind($fp);

$zip->addFileFromStream('goodbye.txt', $fp);

fclose($fp);

// finish the zip stream

$zip->finish();

打包并生成到文件// Autoload the dependencies

require 'vendor/autoload.php';

$local_path = '临时目录';

$zipName = '文件名.zip';

file_put_contents($local_path . $zipName, '');

// 创建一个zip文件

$zipOption = new Archive();

$zipOption->setOutputStream(fopen($local_path . $zipName, 'w+'));

$zip = new ZipStream(null, $zipOption);

// 文件

$file_name = 'xxx.docx';

$zip->addFileFromPath($file_name,'本地文件全路径' );

// 路径+文件

$file_name = 'test/xxx.docx';

$zip->addFileFromPath($file_name,'本地文件全路径' );

// 文件流形式加入

$zip->addFile($file_name, $files['contents']);

//完成

$zip->finish();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值