2

我正在尝试解压缩一个可能包含 1500 多个 pdf 文件的 zip 文件。压缩文件应分部分解压到一个文件夹中,以免一次 20mb 的文件溢出服务器内存。

我已经找到了一个关于如何分段解压缩的示例。但是,此方法不会创建可以查看解压文件的目录或其他内容。它只创建一个文件,而不是目录,这似乎又是一个新的 zip。

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

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

如上所述,此函数创建一些看起来像另一个 zip 文件的文件。如果我创建了我想首先解压缩的文件夹并将其用作 $dstName,它会发出警告,提示它找不到该文件。此外,当我让它在目标链接的末尾创建一个带有“/”的“文件”时,它会发出警告。

使用 opendir 而不是 fopen 不会发出警告,但似乎没有提取任何内容,猜测处理程序的类型错误。

如何将这个大压缩文件部分解压缩到文件夹中?

4

2 回答 2

2

(PK)Zip和GZip是两种完全不同的格式;gzopen无法打开 zip 档案。

要解压 PKZip 档案,请查看 PHP Zip 扩展

于 2012-04-02T19:09:34.803 回答
1
<?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');
?>
于 2012-04-03T05:33:49.440 回答