PHP core ZipArchive versus ZipStream-PHP


PHP core ZipArchive versus ZipStream-PHP

我正在检查从S3压缩流内容的好的解决方案,我遇到了ZipStream-PHP API,它已经被S3 -bucket-stream-zip- PHP API使用,但是我想在核心PHP类ZipArchive的帮助下,它的函数ZipArchive::addFromString我们可以实现同样的。

我的查询是ZipStream-PHP API是更好的解决方案,然后ZipArchive从S3或任何其他云服务压缩流内容?

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

:

//...
$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