PHP: Extract a Zip File’s Contents CodeUnit 09 DEC 2009

PHP-logoThe other day I needed to automate the unzipping and processing of files contained within a zip archive. Using the existing PHP Zip library and some help off the web, this is what I was left with:

(Note, you need to enable php_zip.dll extension within php.ini in order to use the code below)

$zip = zip_open($zipfilename);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen($targetdir . zip_entry_name($zip_entry), "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);

Nifty.

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.

  • thanks for script
    .-= yakup´s last blog ..Manisaspor-stanbul BykAehir Belediye Ma Canl Yayn-Ma Bilgileri =-.

  • thanks for script
    .-= yakup´s last blog ..Manisaspor-stanbul BykAehir Belediye Ma Canl Yayn-Ma Bilgileri =-.

  • No problem man! :)

  • No problem man! :)

  • iam very impressed of your tips and i as know You can use PHP to rotate a image on your web server. This is a nice feature of include if you let users upload photos. You can rotate the image by any number of the degrees that you specify. Learn to

  • iam very impressed of your tips and i as know You can use PHP to rotate a image on your web server. This is a nice feature of include if you let users upload photos. You can rotate the image by any number of the degrees that you specify. Learn to

  • Eugene Koekemoer

    Awesome script, thanx!

    Had a problem though with folders:
    My zip file looks like this;

    files.zip =>
    ———some_folder =>
    ————————somefile.txt
    ————————somefile1.txt
    ————————sub_folder =>
    —————————————–subfile.txt
    —————————————–subfile1.txt

    So it failed because the folders weren't created.
    I updated your script to fix this problem.

    // You neglected to specify the $target for us noobs… lol
    $target = $_SERVER['DOCUMENT_ROOT'].'/somedirectory/';
    $zipfile = $target.$zip;

    $zip = zip_open($zipfile);
    if ($zip) {

    // Optional, if you want to extract the contents into a directory
    // with the same name as the zip file.
    // Can be set with a checkbox matching:
    # if(isset($_POST['some_checkbox_in_your_form']))
    mkdir(str_replace(“.zip”, “”, $zipfile));

    while($zip_entry = zip_read($zip)) {

    // Simple check if it's a file, or a directory, by checking for an extension.
    // This will fail if you have a directory with a .
    // ie. this.is.some.directory
    if(!strstr(zip_entry_name($zip_entry), '.')) {

    // If it doesn't have an extention, create a folder
    mkdir($target.zip_entry_name($zip_entry));

    // Else create the file… [The rest of your script as-is]
    } else {
    $fp = fopen($target.zip_entry_name($zip_entry), “w”);
    $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
    fwrite($fp,$buf);
    zip_entry_close($zip_entry);
    fclose($fp);
    }
    }
    zip_close($zip);
    }

    Using your script for future developments on the Ultimate-CMS system.
    Have an awesome day!