tags:

views:

48

answers:

3

file_get_contents("zip:///a/b/c.zip") is returning NULL. How can I read unzipped contents of a zip file in PHP 5+ ?

A: 

Use zip_open and zip_read functions to do it. Documentation to it you can find at http://pl2.php.net/manual/en/function.zip-read.php

<?php
/**
* This method unzips a directory within a zip-archive
*
* @author Florian 'x!sign.dll' Wolf
* @license LGPL v2 or later
* @link http://www.xsigndll.de
* @link http://www.clansuite.com
*/

function extractZip( $zipFile = '', $dirFromZip = '' )
{   
    define(DIRECTORY_SEPARATOR, '/');

    $zipDir = getcwd() . DIRECTORY_SEPARATOR;
    $zip = zip_open($zipDir.$zipFile);

    if ($zip)
    {
        while ($zip_entry = zip_read($zip))
        {
            $completePath = $zipDir . dirname(zip_entry_name($zip_entry));
            $completeName = $zipDir . zip_entry_name($zip_entry);

            // Walk through path to create non existing directories
            // This won't apply to empty directories ! They are created further below
            if(!file_exists($completePath) && preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )
            {
                $tmp = '';
                foreach(explode('/',$completePath) AS $k)
                {
                    $tmp .= $k.'/';
                    if(!file_exists($tmp) )
                    {
                        @mkdir($tmp, 0777);
                    }
                }
            }

            if (zip_entry_open($zip, $zip_entry, "r"))
            {
                if( preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )
                {
                    if ($fd = @fopen($completeName, 'w+'))
                    {
                        fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
                        fclose($fd);
                    }
                    else
                    {
                        // We think this was an empty directory
                        mkdir($completeName, 0777);
                    }
                    zip_entry_close($zip_entry);
                }
            }
        }
        zip_close($zip);
    }
    return true;
}

// The call to exctract a path within the zip file
extractZip( 'clansuite.zip', 'core/filters' );
?>
Svisstack
+1  A: 

look at the build in zip functions: http://php.net/manual/en/book.zip.php

VeeWee
I like the "ZipArchive" class.
Bob Fanger
A: 

The zip:// protocol is provided by the ZIP extension of PHP. Check in your phpinfo() output whether the extension has been installed or not.

joschi
@Joschi, from phpinfo(): Zip => enabledExtension Version => $Id: php_zip.c,v 1.1.2.38 2007/08/06 22:02:32 bjori Exp $Zip version => 2.0.0Libzip version => 0.7.1Registered PHP Streams => zip, php, file, data, http, ftp, compress.bzip2, compress.zlib, https, ftps
Sam
Have you checked if the file you try to open is actually a valid `.zip` file?
joschi
%> file test.ziptest.zip: Zip archive data, at least v2.0 to extract
Sam
Code snippet: $data = file_get_contents('zip:///path/to/file/test.zip', FALSE, NULL, 0, 1024); The $data is NULL I am verified 1. permissions 2. whether its a proper .zip format. Can zip files be read this way???
Sam
Forgot to mention the zip file always contains a single file.
Sam