duangang79177 2011-06-01 07:45
浏览 58
已采纳

是否可以打开下载的zip文件,而不将其另存为文件?

I am downloading a zip file using the zend httpClient, and getting the contents of the file assigned to a variable as such:

$body = $response->getBody();

$body has the contents of a zip file, can it be opened without saving it as a file first, using http://php.net/manual/en/class.ziparchive.php or some other 5.2 native class?

EDIT The suggestions gave some good ideas how it may be doable without making a temporary file, but due to the fact I'll need to be using the proxy adapter already, going to the lengths of creating an own adapter for the purpose of this, just isn't worth it.

I ended up using the tmpname to create a tmp file (which was something I wanted to avoid, but ended up here anyway).

   if ($response->isSuccessful()){
            $tmpfile = tempnam(sys_get_temp_dir(),'Insight');
            if ($tmpfile!==false){                
                    $handle = fopen($tmpfile, "w");
                    if (fwrite($handle, $response->getBody())!==false){
                        $zip = new ZipArchive;
                        if ($zip->open($tmpfile) === TRUE) {
                            echo $zip->getFromIndex(0);
                            $zip->close();
                        } else {
                            $this->errorLog('Unable to open zip file '.$tmpfile);
                        } 

                    }else{
                        $this->errorLog('Unable to write to temporary file '.$tmpfile);
                    }
                    fclose($handle);                  
            }else{
                $this->errorLog('Unable to create temporary zip file in '.sys_get_temp_dir());
            }                          
        }else{
            $this->errorLog('Unable to download url '.$insightUrl);
        } 
  • 写回答

2条回答 默认 最新

  • doujianmin6527 2011-06-01 08:15
    关注

    I have very little knowledge of the Zend Framework, and absolutely nothing of Zend_Http_Client, but maybe you could use compression stream wrappers when initializing the Zend_Http_Client like this:

    $client = new Zend_Http_Client('zip://http://example.org/compressed_file.zip');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用