Php zip compression and decompression class ZipArchiv User Guide

Source: Internet
Author: User
Tags getstream ziparchive
This article mainly introduces the php zip compression and decompression class ZipArchiv User Guide, which is very detailed. For more information, see The PHP Extension class (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, 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:


<? Php
$ Zip = new ZipArchive;
$ Res = $ zip-> open('test.zip ');
If ($ res = TRUE ){
// Modify word.txt in the compressed file to newword.txt
$ Zip-> renamename('word.txt', 'newword.txt ');
$ Zip-> close ();
} Else {
Echo 'failed', code: '. $ res;
}
?>

Get the comments of compressed files (zip file comments)

ZipArchive: getArchiveComment
(PHP 5> = 5.2.0, PECL zip> = 1.1.0)
String ZipArchive: getArchiveComment ([int $ flags])
Parameter: ZipArchive: FL_UNCHANGED
If the parameter is set to ZipArchive: FL_UNCHANGED, the original comment is returned.
For example, when the setArchiveComment () method is used to change or set comments when processing the compressed file
If the ZipArchive: FL_UNCHANGED parameter is added, the comments before the change are obtained. Otherwise, the changed comments are obtained.
Similar:
ZipArchive: getCommentIndex obtains the [file comment] According to the file index in the compressed file]
ZipArchive: getCommentName get [file comment] based on the file name in the compressed file]
Note: Here is the File annotation, not the zip annotation of the compressed file.

Set or modify the comments of compressed files (zip file comments)
ZipArchive: setArchiveComment
(PHP 5> = 5.2.0, PECL zip> = 1.4.0)
Bool ZipArchive: setArchiveComment (string $ comment)

The code is 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 in the compressed file (that is, deleting entries in the file) based on the indexes in the compressed file)

ZipArchive: deleteIndex
(PHP 5> = 5.2.0, PECL zip> = 1.5.0)

1. how to decompress a zip file extrac.pdf ()

The code is as follows:


$ Zip = new ZipArchive ();

1. how to create a compressed file? AddFromString () addFile ()

Package one or more files into one zip file.

1. you only need to create a new ZipArchive object.
2. use the open method of the object to create a zip file.
3. use the addFile method to write the file to be packaged to the created zip file.
4. close the object.

The code is as follows:


<? Php
// Create a new ZipArchive object
$ Zip = new ZipArchive;
$ Res = $ zip-> open('test.zip ');
// If it is enabled successfully
If ($ res = TRUE ){
// If opening fails
} Else {
// Output error code
Echo 'failed', code: '. $ res;
}
$ Zip-> close ();

The above is all the content of this article, hoping to help you.

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.