doujia3441 2016-10-29 22:21
浏览 53

LAMP:如何从远程URL /文件创建.Zip并将其流式传输到客户端

With external services like S3 for object storage, the old problem of providing custom zip archives of stored files gets a bit more complicated. One way would be for a web server to copy all the resources to temporary local files, compile them into a .zip, then return the zip to the user, but this is slow and resource intensive.

Can this be done similarly to the solution for local files? e.g. can you curl the files, pipe them into zip in streaming mode, then out to the user on the fly?

  • 写回答

1条回答 默认 最新

  • douyi0219 2016-10-29 22:21
    关注

    Yes — you can have zip operate on a set of fifos rather than a set of files. Here’s the code:

    $sh = 'mkfifo a.jpg b.jpg ; '.                     // 1
      'curl -s some_service.com/a.jpg > a.jpg & ' .    // 2
      'curl -s some_service.com/b.jpg > b.jpg & ' .    // 
      'zip -FI - a.jpg b.jpg | cat > out.fifo';        // 3
    
    // Open output fifo & curl-zip task pipe
    posix_mkfifo('out.fifo', 0777);                    // 4
    $pipe_output = popen('cat out.fifo', 'r');         //
    $pipe_curlzip = popen($sh, 'r');                   //
    

    It works as follows:

    1. We have two remote files: some_service.com/a.jpg and some_service.com/b.jpg – we create a fifo for each, each fifo having the name of the corresponding file as we wish it to appear in our zip file. So for some_service.com/a.jpg, we create a fifo called a.jpg.
    2. Kick off two curl tasks to feed the remote files a.jpg and b.jpg into each’s respective fifo. These curl tasks will hang until something reads from their output fifos, so we run them in the background (&).
    3. Start our zip task, giving it our two fifos as input. We cat its streaming output into a third fifo – out.fifo – to allow our PHP process to stream the file to the user.
    4. Kick things off.

    It's then just a matter of reading what comes out of out.fifo and sending it to the user:

    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="Download.zip"');
    
    while (!feof($pipe_output)) {
        echo fread($pipe_output, 128);   // Your magic number here
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波