首頁 > 後端開發 > php教程 > PHP壓縮與歸檔—Zip

PHP壓縮與歸檔—Zip

伊谢尔伦
發布: 2016-11-22 09:42:21
原創
1389 人瀏覽過

此擴充功能可以讓你透明地讀寫ZIP壓縮文件以及它們裡面的檔案。

在PHP5.2以後,為了使用這些函數,必須在編譯 PHP 時用 --enable-zip 設定選項來提供 zip 支援。

Example #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();
?>
登入後複製

Example #2 輸出文檔細節和列表

<?php
    $za = new ZipArchive();
    $za->open(&#39;test_with_comment.zip&#39;);
    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";
?>
登入後複製

Example #3 Zip 流封裝,讀取一個OpenOffice 文件的元資訊

Example #3 Zip 流封裝,讀取一個OpenOffice 文件的元資訊

Example #3 Zip 流封裝,讀取一個OpenOffice 文件的元資訊

4),它打開了一個ZIP 文件歸檔,讀取歸檔裡的每個文件,並輸出文件內容。此例用到的 test2.zip 文檔是 ZZIPlib 來源分佈裡測試文檔中的一個。

Example #4 Zip 使用範例

<?php
    $reader = new XMLReader();
    $reader->open(&#39;zip://&#39; . dirname(__FILE__) . &#39;/test.odt#meta.xml&#39;);
    $odt_meta = array();
    while ($reader->read()) {
        if ($reader->nodeType == XMLREADER::ELEMENT) {
            $elm = $reader->name;
        } else {
            if ($reader->nodeType == XMLREADER::END_ELEMENT && $reader->name == &#39;office:meta&#39;) {
                break;
            }
            if (!trim($reader->value)) {
                continue;
            }
            $odt_meta[$elm] = $reader->value;
        }
    }
    print_r($odt_meta);
?>
登入後複製
 ZipArchive 類別

ZipArchive::addEmptyDir — Add a new directory::

ZipArchive: FromString — Add a file to a ZIP archive using its contents

ZipArchive::addGlob — Add files from a directory by glob pattern

ZipArchive::addr (opened or newly created)

ZipArchive::deleteIndex — delete an entry in the archive using its index

ZipArchive::deleteName — delete an entry in the archivecive 集鑎

ZipArchive::getArchiveComment — Returns the Zip archive comment

ZipArchive::getCommentIndex — Returns the comment of an entry using the entry index👜

ZipArchive:: getFromIndex — Returns the entry contents using its index

ZipArchive::getFromName — Returns the entry contents using its name

ZipArchive::sNameIndex String — Returns the status error message, system and/or zip messages

ZipArchive::getStream — Get a file handler to the entry defined by its name (read only).

ZipArchive::locateName  Returns the index of the

ZipArchive::locateName  Returns the index of the ipory ::open — Open a ZIP file archive

ZipArchive::renameIndex — Renames an entry defined by its index

ZipArchive::renameName — Renames an entry defined comfive nameo five notoiicom fion sotry defincom IP archive

ZipArchive::setCommentIndex — Set the comment of an entry defined by its index

ZipArchive::setCommentName — Set the comment of an entry defined by its defintail anmepopi Htails metail metail by ed by its index.

ZipArchive::statName — Get the details of an entry defined by its name.

ZipArchive::unchangeAll — Undo all changes .

ZipArchive::unchangeIndex — Revert all changes done to an entry at the given index

ZipArchive::unchangeName — Revert all changes done to an entry with the gigi name. archive

zip_entry_close — Close a directory entry

zip_entry_compressedsize — Retrieve the compressed size of a directory entry

zips_pry_compression ry_filesize — Retrieve the actual file size of a directory entry

zip_entry_name — Retrieve the name of a directory entry

zip_entry_open — Open a directory entry for reading

zip_entry_read — Read from an open directory entry

z 🜥 ZIP file archive

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板