【问题标题】:PHP core ZipArchive versus ZipStream-PHPPHP 核心 ZipArchive 与 ZipStream-PHP
【发布时间】:2016-09-04 13:26:16
【问题描述】:

我正在检查从 S3 压缩流内容的良好解决方案,我遇到了 ZipStream-PHP API,它已被 s3-bucket-stream-zip-php API 使用,但我猜在核心 PHP 类 ZipArchive 及其功能的帮助下ZipArchive::addFromString 我们可以达到同样的效果。

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

【问题讨论】:

  • 如果有人反对,请在 cmets 中添加您的理由。
  • 我试图就这个话题寻求帮助!如果有人可以根据他们的经验提出建议,那将会很有帮助!
  • 刚看到这个,下面给你一个解决方案。
  • 你得到你需要的答案了吗?
  • @SukhjinderSingh 如果以下解决方案回答了您的问题,请将其标记为解决方案。

标签: php amazon-web-services amazon-s3 ziparchive zipstream


【解决方案1】:

根据我的经验,最好的解决方案是使用 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

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 1970-01-01
    • 2023-04-06
    • 2012-04-08
    • 2019-08-08
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多