Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

zip拡張モジュール


もう一つ、zip拡張モジュールも追加されていますが、こちらはデフォルトでコンパイルされません。
ということで、このネタはまたの機会 :-)

ということで、RC6も出たことだし、サクッと試してみました。
configureオプションに

--enable-zip=shared

とし、php.iniで

extension=zip.so

としました。
サンプルはPHPマニュアルにあるサンプルをちょっと変えただけです(本家のマニュアルは相変わらず古いままだな。。。)。

<?php
    $zip = new ZipArchive();
    $filename = "/tmp/test112.zip";
    $thisdir = chop(`pwd`);
    var_dump($thisdir);

    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
        exit("cannot open <$filename>\n");
    }

    $zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
    $zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
    $zip->addFile($thisdir . "/zip.php","/testfromfile.php");
    echo "numfiles: " . $zip->numFiles . "\n";
    echo "status:" . $zip->status . "\n";
    $zip->close();
?>

実行すると、/tmp/test112.zipが作成されます。

$ unzip -l /tmp/test112.zip
Archive:  /tmp/test112.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
       51  10-20-06 13:52   testfilephp.txt1161319972
       52  10-20-06 13:52   testfilephp2.txt1161319972
      576  10-20-06 13:52   /testfromfile.php
 --------                   -------
      679                   3 files
$ 

おお。ナニゲに結構使えるかも。