我想使用ZipArchive(或本机PHP类)在内存中创建一个zip文件,并将文件内容读回客户端.这可能吗?如果是这样,怎么样?
我想在此应用程序中压缩的文件总共最多15 MB.我认为我们应该记忆良好.
小智 5
感谢 Frosty Z 提供的优秀库 ZipStream-PHP。我们有一个用例,将一些数量和大小都较大的 zip 文件上传到 S3。官方文档没有提到如何上传到S3。
因此,我们有一个想法,将 ZipStream 输出创建的 zip 直接流式传输到 S3,而无需在服务器上创建 zip 文件。
这是我们提出的工作示例代码:
<?php
# Autoload the dependencies
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use ZipStream\Option\Archive as ArchiveOptions;
//s3client service
$s3Client = new S3Client([
'region' => 'ap-southeast-2',
'version' => 'latest',
'credentials' => [
'key' => '<AWS_KEY>',
'secret' => '<AWS_SECRET_KEY>',
]
]);
$s3Client->registerStreamWrapper(); //required
$opt = new ArchiveOptions();
$opt->setContentType('application/octet-stream');
$opt->setEnableZip64(false); //optional - for MacOs to open archives
$bucket = 'your_bucket_path';
$zipName = 'target.zip';
$zip = new ZipStream\ZipStream($zipName, $opt);
$path = "s3://{$bucket}/{$zipName}";
$s3Stream = fopen($path, 'w');
$zip->opt->setOutputStream($s3Stream); // set ZipStream's output stream to the open S3 stream
$filePath1 = './local_files/document1.zip';
$filePath2 = './local_files/document2.zip';
$filePath3 = './local_files/document3.zip';
$zip->addFileFromPath(basename($filePath1), $filePath1);
$zip->addFileFromPath(basename($filePath2), $filePath2);
$zip->addFileFromPath(basename($filePath3), $filePath3);
$zip->finish(); // sends the stream to S3
?>
Run Code Online (Sandbox Code Playgroud)
小智 3
看一下下面的库,它允许创建 zip 文件并将它们作为流返回:PHPClasses.org。
归档时间: |
|
查看次数: |
18940 次 |
最近记录: |