title: Creating a zip file on the fly from files stored on S3 using php
body: <p>I have a Laravel web app in which users can upload files. These files can be sensitive and although they are stored on S3 they are only accessed via my webservers (streamed download). Once uploaded users may wish to download a selection of these files.</p>
  
  <p>Previously when users went to download a selection of files my web server would download the files from S3, zip them locally and then send the zip down to the client. However once in production due to file sizes the server response would frequently time out.</p>
  
  <p>As an alternative method I want to zip the files on the fly via <a href="https://github.com/maennchen/ZipStream-PHP" rel="noreferrer">ZipStream</a> but I haven't had much luck. The zip file either ends up with corrupted files or is corrupted itself and incredibly small.</p>
  
  <p>If it possible to pass a stream resource for a file on S3 to ZipStream and what is the best way to address my timeout issues?</p>
  
  <p>I have tried several method my most recent two are as follows:</p>
  
  <pre><code>// First method using fopen
  // Results in tiny corrupt zip files
  if (!($fp = fopen("s3://{$bucket}/{$key}", 'r')))
  {
      die('Could not open stream for reading');
  }
  
  $zip-&gt;addFileFromPath($file-&gt;orginal_filename, "s3://{$bucket}/{$key}");
  fclose($fp);
  
  
  // Second method tried get download the file from s3 before sipping
  // Results in a reasonable sized zip file that is corrupt
  $contents = file_get_contents("s3://{$bucket}/{$key}");
  
  $zip-&gt;addFile($file-&gt;orginal_filename, $contents); 
  </code></pre>
  
  <p>Each of these sits within a loop that goes through each files. After the loop I call $zip->finish(). </p>
  
  <p>Note I do not get any php errors just corrupt files.</p>