Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to read zip file in PHP

Tags:

php

zip

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

like image 852
Sam Avatar asked Aug 30 '10 08:08

Sam


People also ask

How to read zip file in PHP?

Description: Create an object of the ZipArchive class and open a given zip file using $zip->open() method. If it returns TRUE then extract the file to the specified path with extractTo() method by passing path address as an argument in it.

How can I get zip file size in PHP?

PHP zip_entry_filesize() Function The zip_entry_filesize() function returns the actual file size of a ZIP directory entry.

What does PHP zip do?

The PHP zip file function allows the scripts to work with the zip files; using these function; the corresponding libraries will be imported to the PHP scripts. Using the PHP zip extension, the pho version 5 and above to be supported.


2 Answers

use ZipArchive class

$zip = new ZipArchive;
$zip->open('test.zip');
echo $zip->getFromName('filename.txt');
$zip->close();
like image 74
AminFarajzadeh Avatar answered Oct 06 '22 00:10

AminFarajzadeh


Use zip_open and zip_read functions to do it. Documentation to it you can find at http://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' );
?>
like image 30
Svisstack Avatar answered Oct 05 '22 22:10

Svisstack