2011-06-01 42 views
1

我使用Zend HttpClient的下载zip文件,并获得分配给一个变量为这样的文件的内容:可以打开下载的压缩文件,而不必先将它保存为文件?

$body = $response->getBody(); 

$body有一个zip文件的内容,可以将其打开不保存它作为一个文件第一,使用http://php.net/manual/en/class.ziparchive.php或其他5.2本地类?

编辑的建议,提出了一些很好的想法如何可以未做临时文件是可行的,但由于我需要使用代理服务器适配器已经,去创建一个的长度自己的适配器的目的,只是不值得。

我结束了使用tmpname创建一个tmp文件(这是我想避免的,但最终在这里结束了)。

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); 
     } 
+0

你就不能使用'zip_open( “http://url/to/download.zip”)'? – 2011-06-01 08:27:46

+0

@Jules需要使用代理,并且可能必须使用cookie。不要说这可能无法用你的方式完成,但如果我仍然可以使用zend http客户端,肯定会更容易。 – Niklas 2011-06-01 08:29:15

回答

1

我有Zend框架的了解甚少,而且绝对没有一个Zend_Http_Client,但是也许初始化Zend_Http_Client这样时,你可以使用compression stream wrappers

$client = new Zend_Http_Client('zip://http://example.org/compressed_file.zip'); 
+0

这并不幸运。消息:不支持Scheme“zip” – Niklas 2011-06-01 09:10:52

+0

并且'compress.zip:// http:// ...'? – 2011-06-01 09:13:29

+0

消息:提供了非法计划,只允许使用字母数字字符 – Niklas 2011-06-01 09:15:02

0

尝试使用PHP Zip Functions

+0

'zip_open()'* only *需要一个文件名,而不是一串数据或其他资源。 – 2011-06-01 07:55:52

+1

我想向你介绍http://php.net/manual/en/function.tmpfile.php – Eugene 2011-06-01 08:02:35

+0

你看过这个问题吗?特别是大胆的部分。 – 2011-06-01 08:03:15

相关问题