To use this PHP extension class, you need (PHP 5 >= 5.2.0, PECL zip >= 1.1.0), some methods require PHP 5.2.+, and php.ini configuration supports zip
For the win system, simply remove the annotation for the php_zip.dll extension, and then restart the HTTP service (IIS or Apache)
Linux has not been tested, the theory is not very different
Function:
1. Extract zip file
2, compressed files into a zip file
3, append files to the zip file
4, package the folder into a ZIP file (you need to cycle to add files and create an empty folder)
5. Delete the items in the compressed file
--------------------- ziparchive objects commonly used in the introduction of methods---------------------
Test Convention:
The test file is Text.zip, and the compressed file contains three compressed files (Hello.txt, Word.txt, ooxx.jpg) as follows
Copy Code code as follows:
Text.zip
Hello.txt
Word.txt
Ooxx.jpg
Open the zip file for further action
Ziparchive::open
(PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
Mixed Ziparchive::open (string $filename [, int $flags])
2nd parameter Explanation
Ziparchive::overwrite always creates a new file that is overwritten if the specified zip file exists
Ziparchive::create if the specified zip file does not exist, create a new
ZIPARCHIVE::EXCL if the specified zip file exists, an error occurs
Ziparchive::checkcons
return value:
If the return value equals the following property, indicates a corresponding error or returns true
$res = = ziparchive::er_exists file already EXISTS. (File already exists)
$res = = Ziparchive::er_incons Zip archive inconsistent. (Inconsistent file compression)
$res = = ziparchive::er_inval Invalid argument. (Invalid parameter)
$res = = Ziparchive::er_memory Malloc failure. (Memory error?) This is not certain)
$res = = ziparchive::er_noent no such file. (No such document)
$res = = Ziparchive::er_nozip not a zip archive. (No compressed file)
$res = = Ziparchive::er_open can ' t open file. (No files can be opened)
$res = = Ziparchive::er_read Read error. (read error)
$res = = Ziparchive::er_seek seek error. (Find errors)
Copy Code code as follows:
<?php
$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
if ($res = = TRUE) {
echo ' OK ';
Extract to test folder
$zip->extractto (' Test ');
$zip->close ();
} else {
Echo ' failed, code: '. $res;
}
?>
Returns the name of the compressed file based on the index of the list within the compressed file
Ziparchive::getnameindex
String Ziparchive::getnameindex (int $index [, int $flags])
Copy Code code as follows:
<?php
$zip = new Ziparchive ();
$res = $zip->open (' Test.zip ');
if ($res = = TRUE) {
Var_dump ($zip->getnameindex (0)); Hello.txt
Var_dump ($zip->getnameindex (1)); Word.txt
Var_dump ($zip->getnameindex (2)); Ooxx.jpg
} else {
Echo ' failed, code: '. $res;
}
$zip->close ();
?>
Gets the text stream of the file, based on the file name within the compression
Ziparchive::getstream
Resource Ziparchive::getstream (String $name)
Copy Code code as follows:
<?php
$zip = new Ziparchive ();
$res = $zip->open (' Test.zip ');
if ($res = = TRUE) {
$stream = $zip->getstream (' hello.txt ');
} else {
Echo ' failed, code: '. $res;
}
$zip->close ();
$str = Stream_get_contents ($stream); Here, note the text encoding that gets
Var_dump ($STR);
?>
Modify the file name within the compressed file based on the index within the compressed file (starting from 0)
Ziparchive::renameindex
BOOL Ziparchive::renameindex (int $index, string $newname)
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)
Returns TRUE on success or FALSE on failure.
Copy Code code as follows:
<?php
$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
if ($res = = TRUE) {
Modify the first file in the compressed file into Newname.txt
$zip->renameindex (0, ' newname.txt ');
$zip->close ();
} else {
Echo ' failed, code: '. $res;
}
?>
Modify the file name within the compressed file according to the file name in the compressed file
Ziparchive::renamename
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)
Copy Code code as follows:
<?php
$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
if ($res = = TRUE) {
Modify the Word.txt in the compressed file into Newword.txt
$zip->renamename (' word.txt ', ' newword.txt ');
$zip->close ();
} else {
Echo ' failed, code: '. $res;
}
?>
Get comments for compressed files (zip file comments)
Ziparchive::getarchivecomment
(PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
String Ziparchive::getarchivecomment ([int $flags])
Parameters: ziparchive::fl_unchanged
If the parameter is set to Ziparchive::fl_unchanged, return the original note that has not changed
For example, when you work with the compressed file, you use the Setarchivecomment () method to change or set the annotation
If you add ziparchive::fl_unchanged this parameter, you get the comment before the change, or you get the commented content that has changed
There are also similar:
Ziparchive::getcommentindex get "file annotations" based on the file index in the compressed file
Ziparchive::getcommentname get "file annotations" based on the file name within the compressed file
Note: Here is a comment on the file, not a compressed file (Zip)
Set or modify comments for compressed files (zip file comments)
Ziparchive::setarchivecomment
(PHP 5 >= 5.2.0, PECL zip >= 1.4.0)
BOOL Ziparchive::setarchivecomment (String $comment)
Copy Code code as follows:
<?php
$zip = new Ziparchive;
$res = $zip->open (' Test.zip ', ziparchive::create);
if ($res = = TRUE) {
$zip->addfromstring (' test.txt ', ' file content goes here ');
$zip->setarchivecomment (' New archive comment ');
$zip->close ();
echo ' OK ';
} else {
Echo ' failed ';
}
?>
Delete files within compressed files (that is, delete entries in the file) based on the index in the compressed file
Ziparchive::d Eleteindex
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)
One, how to extract a zip file Extractto ()
Copy Code code as follows:
$zip = new Ziparchive ();
How do I create a compressed file? AddFromString () AddFile ()
That is, to package one or more files into a zip file
1, only need new one Ziparchive object
2, and then use the object's open method to create a zip file
3, then use the AddFile method, the file will be packaged into the zip file just created
4, finally remember to close the object
Copy Code code as follows:
<?php
To create a new Ziparchive object
$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
If the success is turned on
if ($res = = TRUE) {
If Open fails
} else {
Output error code
Echo ' failed, code: '. $res;
}
$zip->close ();
The above mentioned is the entire content of this article, hope to be helpful to everybody.