2012-04-02 10 views
1

私は1500を超えるPDFファイルを含むzipファイルを解凍しようとしています。圧縮されたファイルは、一度に20MBのファイルでサーバーのメモリをオーバーフローさせないように、一部のフォルダに解凍する必要があります。PHPを使用して大きなzipをフォルダに解凍して

すでに部品を解凍する方法の例が見つかりました。ただし、この方法では、展開されたファイルを表示できるディレクトリなどは作成されません。 1つのファイルしか作成されません。これはディレクトリではなく、新しいジップのようです。

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

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

この関数は、上で説明したように、まだ他のzipファイルのように見えるファイルを作成します。フォルダを作成して、最初に解凍して$ dstNameとして使用すると、ファイルが見つからないという警告が表示されます。また、リンク先の最後に "/"をつけた "ファイル"を作成させると、その警告が表示されます。

fopenの代わりにopendirを使用しても警告は出ませんが、何も抽出されていないようですが、ハンドラのタイプが間違っていると思います。

この大きな圧縮ファイルを部分的にフォルダに解凍するにはどうすればよいですか?

答えて

2

(PK)ジップとGZipは全く異なる2つのフォーマットです。 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'); 
?> 
関連する問題