user1308511
user1308511

Reputation: 21

Unpack large zips in parts with PHP into a folder

I am trying to unzip a zip file containing maybe over 1500 pdf files. The compressed file should be unpacked in parts into a folder to not overflow the server memory with a 20mb file at once.

I already found an example on how to unzip in parts. However, this method does not create a directory or something in which the unpacked files can be viewed. It only creates one file, which is not a directory, which seems like to be a new zip again.

$sfp = gzopen($srcName, "rb");
$fp = fopen($dstName, "w+");

while ($string = gzread($sfp, 4096)) {
    fwrite($fp, $string, strlen($string));
}
gzclose($sfp);
fclose($fp);

This function creates some file that seems like yet an other zip file, as explained above. If I create the folder I want to unzip it to first and use that as $dstName, it gives a warning that it can not find the file. Also when I let it create a "file" with a "/" at the end of the destination link it gives that warning.

Using opendir instead of fopen does not give a warning, but nothing seems to be extracted then, guess the handler is of some wrong type.

How can I unzip this large compressed file in parts into a folder?

Upvotes: 2

Views: 829

Answers (2)

Angelin Nadar
Angelin Nadar

Reputation: 9320

<?php

function unzip($file) {
    $zip = zip_open($file);
    if (is_resource($zip)) {
        $tree = "";
        while (($zip_entry = zip_read($zip)) !== false) {
            echo "Unpacking " . zip_entry_name($zip_entry) . "\n";
            if (strpos(zip_entry_name($zip_entry), DIRECTORY_SEPARATOR) !== false) {
                $last = strrpos(zip_entry_name($zip_entry), DIRECTORY_SEPARATOR);
                $dir = substr(zip_entry_name($zip_entry), 0, $last);
                $file = substr(zip_entry_name($zip_entry), strrpos(zip_entry_name($zip_entry), DIRECTORY_SEPARATOR) + 1);
                if (!is_dir($dir)) {
                    @mkdir($dir, 0755, true) or die("Unable to create $dir\n");
                }
                if (strlen(trim($file)) > 0) {
                    //Downloading in parts
                    $fileSize = zip_entry_filesize($zip_entry);
                    while ($fileSize > 0) {
                        $readSize = min($fileSize, 4096);
                        $fileSize -= $readSize;
                        $content = zip_entry_read($zip_entry, $readSize);
                        if ($content !== false) {
                            $return = @file_put_contents($dir . "/" . $file, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
                            if ($return === false) {
                                die("Unable to write file $dir/$file\n");
                            }
                        }
                    }
                }
                fclose($outFile);
            } else {
                file_put_contents($file, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
            }
        }
    } else {
        echo "Unable to open zip file\n";
    }
}

unzip($_SERVER['DOCUMENT_ROOT'] . '/test/testing.zip');
?>

Upvotes: 1

user149341
user149341

Reputation:

(PK)Zip and GZip are two completely different formats; gzopen cannot open zip archives.

To unpack PKZip archives, take a look at the PHP Zip extension.

Upvotes: 2

Related Questions