PHP ziparchive class use examples _php tutorial

Source: Internet
Author: User
Tags php ziparchive ziparchive
PHP ziparchive can be said to be a PHP self-contained function, he can compress and decompress the file processing, but before using this class we have to php.ini in the Extension=php_ Zip.dll in front of the semicolon has not been removed, and then restart Apache to use this class library.

Ziparchive Optional Parameters

1.ziparchive::addemptydir

Add a new file directory

2.ziparchive::addfile

Adds a file to the specified zip archive.

3.ziparchive::addfromstring

Add the file and add the content in

4.ziparchive::close

Close Ziparchive

5.ziparchive::extractto

Unzip the compressed package

6.ziparchive::open

Open a ZIP archive package

7.ziparchive::getstatusstring

Returns the state content of compression, including error messages, compressed information, and so on

8.ZipArchive::d Eleteindex

Delete a file in a compressed package, such as: Deleteindex (0) Delete the first file

9.ZipArchive::d eletename

Deletes a file name from a compressed package, and also deletes the file.
......
*/


Instance

First, unzip the zip file

The code is as follows Copy Code
$zip = new ziparchive;//Create an Ziparchive object
/*
Working with Zip files by Ziparchive objects
$zip->open The parameter of this method represents the zip file name processed.
If the operation on the zip file object succeeds, $zip->open this method returns True
*/
if ($zip->open (' test.zip ') = = = TRUE)
{
$zip->extractto (' images ');//Assume that you unzip the subfolder to the Images folder under the current path PHP
$zip->close ();//Close the processed zip file
}

Compress the file into a zip file

The code is as follows Copy Code
$zip = new Ziparchive;
/*
$zip->open The first parameter of this method represents the zip file name processed.
The second parameter represents the processing mode, ziparchive::overwrite means that if the zip file exists, it overwrites the original zip file.
If the parameter uses Ziparchive::create, the system will add content to the original zip file.
If you are not adding content to a zip file more than once, we recommend using Ziparchive::overwrite.
Using these two parameters, if the zip file does not exist, the system will be automatically created.
If the operation on the zip file object succeeds, $zip->open this method returns True
*/
if ($zip->open (' Test.zip ', ziparchive::overwrite) = = = TRUE)
{
$zip->addfile (' image.txt ');//Assuming that the name of the file being added is Image.txt, under the current path
$zip->close ();
}

Third, file append content to zip file

The code is as follows Copy Code
$zip = new Ziparchive;
$res = $zip->open (' Test.zip ', ziparchive::create);
if ($res = = = TRUE) {
$zip->addfromstring (' test.txt ', ' file content goes here ');
$zip->close ();
echo ' OK ';
} else {
Echo ' failed ';
}

Iv. Package a folder into a zip file

The code is as follows Copy Code
< P>function Addfiletozip ($path, $zip) {
$handler = Opendir ($path);//Open the current folder is specified by $path. The
/*
Loop reads all files and folders under the folder
where $filename = Readdir ($handler) is assigned the read file name to $filename each time the loop is looped,
in order not to be trapped in the loop, let the $ FileName!== false.
Be sure to use!==, because if a file name is called ' 0 ', or if some are considered false by the system, you will stop looping with! =
*/
while (($filename = Readdir ($handler))!== false) {
if ($filename! = "." && $filename! = "...") {//Folder file name is '. ' and '. ', do not operate on them
if (Is_dir ($path. "/" . $filename) {//If an object being read is a folder, the recursive
Addfiletozip ($path. "/" . $filename, $zip);
} else {//Add files to the Zip object
$zip->addfile ($path. "/" . $FILENAME);
}
}
}
@closedir ($path);
}

$zip = new ziparchive ();
if ($zip->open (' Images.zip ', ziparchive::overwrite) = = = TRUE) {
Addfiletozip (' images/', $zip);//Call method, operate on the root directory to be packaged and pass the Ziparchive object to the method
$zip->close ();//Close the processed zip file
}


If you only know the file name, and you do not know the exact path to it, you can search for the index of the specified file name, and then rely on the index to get the content.

The code is as follows Copy Code


$zip = new Ziparchive;
if ($zip->open (' test.zip ') = = = TRUE) {
$index = $zip->locatename (' example.php ', ziparchive::fl_nocase| ZIPARCHIVE::FL_NODIR);
$contents = $zip->getfromindex ($index);
}
?>


The above get index relies on the Locatename method, if there are multiple paths within the compressed package with the same name file, it seems that only the first index can be returned, if you want to get all the same name of the index of the file, can only use the stupid method, loop search.

The code is as follows Copy Code

$zip = new Ziparchive;
if ($zip->open (' test.zip ') = = = TRUE) {
for ($i = 0; $i < $zip->numfiles; $i + +)
{
if (Substr_count ($zip->getnameindex ($i), ' example.php ') >0) {
$contents = $zip->getfromindex ($i);
}
}
}
?>

http://www.bkjia.com/PHPjc/632774.html www.bkjia.com true http://www.bkjia.com/PHPjc/632774.html techarticle PHP ziparchive can be said to be a PHP self-contained function, he can compress the file and decompression processing, but use this class before we have to put the Extension=php_zip.dll before the php.ini ...

  • 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.