Zip
PHP Manual

例

例1 Zip アーカイブの作成

<?php

$zip 
= new ZipArchive();
$filename = "./test112.zip";

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 . "/too.php","/testfromfile.php");
echo 
"numfiles: " . $zip->numFiles . "\n";
echo 
"status:" . $zip->status . "\n";
$zip->close();
?>

例2 アーカイブの詳細の出力および一覧表示

<?php
$za 
= new ZipArchive();

$za->open('test_with_comment.zip');
print_r($za);
var_dump($za);
echo 
"numFiles: " . $za->numFiles . "\n";
echo 
"status: " . $za->status  . "\n";
echo 
"statusSys: " . $za->statusSys . "\n";
echo 
"filename: " . $za->filename . "\n";
echo 
"comment: " . $za->comment . "\n";

for (
$i=0; $i<$za->numFiles;$i++) {
    echo 
"index: $i\n";
    
print_r($za->statIndex($i));
}
echo 
"numFile:" . $za->numFiles . "\n";
?>

例3 Zip ストリームラッパーによる OpenOffice メタ情報の読み込み

<?php
$reader 
= new XMLReader();

$reader->open('zip://' . dirname(__FILE__) . '/test.odt#meta.xml');
$odt_meta = array();
while (
$reader->read()) {
    if (
$reader->nodeType == XMLREADER::ELEMENT) {
        
$elm = $reader->name;
    } else {
        if (
$reader->nodeType == XMLREADER::END_ELEMENT && $reader->name == 'office:meta') {
            break;
        }
        if (!
trim($reader->value)) {
            continue;
        }
        
$odt_meta[$elm] = $reader->value;
    }
}
print_r($odt_meta);
?>

この例は旧 API (PHP 4 用) を使用します。まず ZIP ファイルアーカイブをオープンし、アーカイブ内の各ファイルを読み込み、 その内容を出力します。この例で使用するアーカイブ test2.zip は、ZZIPlib のソース配布物に含まれているテスト用アーカイブのひとつです。

例4 Zip の使用例

<?php

$zip 
= zip_open("/tmp/test2.zip");

if (
$zip) {
    while (
$zip_entry = zip_read($zip)) {
        echo 
"Name:               " . zip_entry_name($zip_entry) . "\n";
        echo 
"Actual Filesize:    " . zip_entry_filesize($zip_entry) . "\n";
        echo 
"Compressed Size:    " . zip_entry_compressedsize($zip_entry) . "\n";
        echo 
"Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";

        if (
zip_entry_open($zip, $zip_entry, "r")) {
            echo 
"File Contents:\n";
            
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
            echo 
"$buf\n";

            
zip_entry_close($zip_entry);
        }
        echo 
"\n";

    }

    
zip_close($zip);

}
?>

Zip
PHP Manual