Home > Backend Development > PHP Tutorial > PHP compression and archiving—Zip

PHP compression and archiving—Zip

伊谢尔伦
Release: 2016-11-22 09:42:21
Original
1390 people have browsed it

This extension allows you to transparently read and write ZIP archives and the files inside them.

After PHP5.2, in order to use these functions, you must use the --enable-zip configuration option when compiling PHP to provide zip support.

Example #1 Create a Zip archive

<?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();
?>
Copy after login

Example #2 Output document details and list

<?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";
?>
Copy after login

Example #3 Zip stream encapsulation, read the meta information of an OpenOffice file

<?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);
?>
Copy after login

This example uses the old API (PHP 4), it opens a ZIP file archive, reads each file in the archive, and outputs the file contents. The test2.zip file used in this example is one of the test files in the ZZIPlib source distribution.

Example #4 Zip usage example

<?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);
    }
?>
Copy after login

ZipArchive class

ZipArchive::addEmptyDir — Add a new directory

ZipArchive::addFile — Adds a file to a ZIP archive from the given path

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

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

ZipArchive::addPattern — Add files from a directory by PCRE pattern

ZipArchive::close — Close the active archive (opened or newly created)

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

ZipArchive::deleteName — delete an entry in the archive using its name

ZipArchive::extractTo — Extract the archive contents

ZipArchive::getArchiveComment — Returns the Zip archive comment

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

ZipArchive::getCommentName — Returns the comment of an entry using the entry name

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

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

ZipArchive::getNameIndex — Returns the name of an entry using its index

ZipArchive::getStatusString — 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 entry in the archive

ZipArchive ::open — Open a ZIP file archive

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

ZipArchive::renameName — Renames an entry defined by its name

ZipArchive::setArchiveComment — Set the comment of a ZIP archive

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

ZipArchive::setCommentName — Set the comment of an entry defined by its name

ZipArchive::statIndex — Get the details of an entry defined by its index.

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

ZipArchive::unchangeAll — Undo all changes done in the archive

ZipArchive::unchangeArchive — Revert all global changes done in the archive .

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

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

Related functions

zip_close — Close a ZIP file archive

zip_entry_close — Close a directory entry

zip_compressedsize — Retrieve the compressed size of a directory entry

zip_entry_compressionmethod — Retrieve the compression method of a directory entry

zip_entry_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

zip_open — Open a ZIP file archive

zip_read — Read next entry in a ZIP file archive


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template