开发者

Can a downloaded zip file be opened, without saving it as a file first?

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);
                 开发者_StackOverflow社区   }
                    fclose($handle);                  
            }else{
                $this->errorLog('Unable to create temporary zip file in '.sys_get_temp_dir());
            }                          
        }else{
            $this->errorLog('Unable to download url '.$insightUrl);
        } 


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');


Try using PHP Zip Functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜