This article mainly introduces the php zip compression and decompression class ZipArchiv user guide, which is very detailed. For more information, see
This article mainly introduces the php zip compression and decompression class ZipArchiv user guide, which is very detailed. For more information, see
To use this PHP extension class, you need to (PHP 5> = 5.2.0, PECL zip> = 1.1.0), some methods need PHP 5.2. +, and php. ini configuration supports zip
For the win system, remove the php_zip.dll extension comments and restart the http service (IIS or Apache ).
Linux has not been tested, and the theoretical difference will not be great
Function:
1. decompress the zip file
2. compress the file into a zip file
3. append a file to a zip file
4. Package the folder into a zip file (You Need To cyclically Add the file and create an empty folder)
5. Delete entries in the compressed file
--------------------- Common methods for ZipArchive objects ---------------------
Test conventions:
The test file is text.zip, which contains three compressed files (hello.txt0000word.txt0000ooxx.jpg), as shown below:
The Code is as follows:
Text.zip
Hello.txt
Word.txt
Ooxx.jpg
Open the zip file for further operations
ZipArchive: open
(PHP 5> = 5.2.0, PECL zip> = 1.1.0)
Mixed ZipArchive: open (string $ filename [, int $ flags])
2nd parameter descriptions
ZIPARCHIVE: OVERWRITE always creates a new file. If the specified zip file exists, it overwrites it.
ZIPARCHIVE: CREATE if the specified zip file does not exist, CREATE
ZIPARCHIVE: EXCL if the specified zip file exists, an error is returned.
ZIPARCHIVE: CHECKCONS
Return Value:
If the returned value is equal to the following attribute, it indicates the corresponding error or returns TRUE.
$ Res = ZipArchive: ER_EXISTS File already exists. (The File already exists)
$ Res = ZipArchive: ER_INCONS Zip archive inconsistent. (inconsistent compressed files)
$ Res = ZipArchive: ER_INVAL Invalid argument. (Invalid parameter)
$ Res = ZipArchive: ER_MEMORY Malloc failure. (memory error? This is not sure)
$ Res = ZipArchive: ER_NOENT No such file. (There is No such file)
$ Res = ZipArchive: ER_NOZIP Not a zip archive. (No compressed file exists)
$ Res = ZipArchive: ER_OPEN Can't open file. (the file cannot be opened)
$ Res = ZipArchive: ER_READ Read error. (Read error)
$ Res = ZipArchive: ER_SEEK Seek error. (search error)
The Code is as follows:
<? Php
$ Zip = new ZipArchive;
$ Res = $ zip-> open('test.zip ');
If ($ res = TRUE ){
Echo 'OK ';
// Decompress the package to the test folder.
$ Zip-> extracloud ('test ');
$ Zip-> close ();
} Else {
Echo 'failed', code: '. $ res;
}
?>
Returns the name of the compressed file based on the index of the compressed file list.
ZipArchive: getNameIndex
String ZipArchive: getNameIndex (int $ index [, int $ flags])
The Code is 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 ();
?>
Obtains the text stream of the compressed file based on the file name.
ZipArchive: getStream
Resource ZipArchive: getStream (string $ name)
The Code is 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); // pay attention to the obtained text encoding.
Var_dump ($ str );
?>
Modify the file name in the compressed file based on the index (starting from 0) in the compressed file.
ZipArchive: renameIndex
Bool ZipArchive: renameIndex (int $ index, string $ newname)
(PHP 5> = 5.2.0, PECL zip> = 1.5.0)
Returns TRUE if the call succeeds, or FALSE if the call fails.
The Code is as follows:
<? Php
$ Zip = new ZipArchive;
$ Res = $ zip-> open('test.zip ');
If ($ res = TRUE ){
// Modify the first file in the compressed file to newname.txt
$ Zip-> renameindex(0, 'newname.txt ');
$ Zip-> close ();
} Else {
Echo 'failed', code: '. $ res;
}
?>
Modify the file name in the compressed file according to the file name in the compressed file.
ZipArchive: renameName
(PHP 5> = 5.2.0, PECL zip> = 1.5.0)
The Code is as follows: