2012-04-02 40 views
1

我想解壓縮包含可能超過1500個pdf文件的zip文件。壓縮文件應該部分解壓縮到一個文件夾中,以便不會一次將20mb文件溢出服務器內存。解壓包含PHP的部分大拉鍊到一個文件夾中

我已經找到了一個關於如何解壓縮的例子。但是,此方法不會創建目錄或可以查看解壓縮文件的內容。它只創建一個文件,它不是一個目錄,它似乎又是一個新的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不會給出警告,但似乎沒有提取出來,然後猜測處理程序是一些錯誤的類型。

如何將這個大的壓縮文件分解到一個文件夾中?

回答

2

(PK)Zip和GZip是兩種完全不同的格式; gzopen無法打開zip檔案。

要解壓縮PKZip存檔,請查看PHP Zip extension

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'); 
?> 
相關問題