PHP Compression and archive-zip

Source: Internet
Author: User
Tags ziparchive
This extension allows you to read and write the zip archive transparently and the files inside them.

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

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

Example #2 output document details and list

<?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";? >

Example #3 Zip Stream package to read meta information for a OpenOffice file

<?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);? >

This example uses the old API (PHP 4), which opens a ZIP file archive, reads each file in the archive, and outputs the contents of the file. The Test2.zip document used in this example is one of the test documents 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);    }? >

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::d eleteindex-delete An entry in the archive using its index

Ziparchive::d eletename-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 do in the archive

Ziparchive::unchangearchive-revert All global changes do in the archive.

Ziparchive::unchangeindex-revert all changes do to a entry at the given index

Ziparchive::unchangename-revert all changes do to a entry with the given name.

Related functions

Zip_close-close a ZIP file archive

Zip_entry_close-close a directory entry

Zip_entry_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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.