PHP comes with zip compression, decompression class Ziparchiv usage Guide _php tutorial

Source: Internet
Author: User
Tags getstream ziparchive

PHP self-Zip compression, decompression class Ziparchiv usage guide


This article mainly introduces PHP's own zip compression, decompression class Ziparchiv use guide, very detailed, the need for friends can refer to the following

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 comment for the php_zip.dll extension, and then restart the HTTP service (IIS or Apache) to
Linux hasn't been tested yet, it's not a big difference.

Function:
1. Unzip the zip file
2. Compress the file into a zip file
3. append files to zip file
4. Package the folder into a ZIP file (you need to loop to add files and create empty folders)
5. Delete the entries in the compressed file

Introduction to common methods of---------------------Ziparchive objects---------------------

Test conventions:
The test file is text.zip and the compressed file contains three compressed files (Hello.txt, Word.txt, ooxx.jpg), as shown below

The code is 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])

Explanation of the 2nd parameter

Ziparchive::overwrite always creates a new file, and if the specified zip file exists, it will overwrite the
Ziparchive::create if the specified zip file does not exist, create a new
ZIPARCHIVE::EXCL if the specified zip file exists, an error will be given
Ziparchive::checkcons

return value:

If the return value equals the following attribute, indicates the corresponding error or returns true
$res = = ziparchive::er_exists file already EXISTS. (File already exists)
$res = = Ziparchive::er_incons Zip archive inconsistent. (Inconsistent compression file)
$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 files)
$res = = Ziparchive::er_nozip not a zip archive. (without a compressed file)
$res = = Ziparchive::er_open can ' t open file. (Cannot open files)
$res = = Ziparchive::er_read Read error. (read errors)
$res = = Ziparchive::er_seek SEEK error. (Find errors)

The code is as follows:


$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
if ($res = = = TRUE) {
echo ' OK ';
Unzip to test folder
$zip->extractto (' Test ');
$zip->close ();
} else {
Echo ' failed, code: '. $res;
}
?>

Returns the name of the compressed file based on the list index within the compressed file

Ziparchive::getnameindex
String Ziparchive::getnameindex (int $index [, int $flags])

The code is as follows:


$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 name of the file within the compression

Ziparchive::getstream
Resource Ziparchive::getstream (String $name)

The code is as follows:


$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); Note here to get to the text encoding
Var_dump ($STR);
?>

Modifies the file name within the compressed file based on the index within the compressed file (starting with 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.

The code is as follows:


$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
if ($res = = = TRUE) {
Change the first file in a compressed file to 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 filename within the compressed file

Ziparchive::renamename
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

The code is as follows:


$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
if ($res = = = TRUE) {
Modify the Word.txt in the compressed file to Newword.txt
$zip->renamename (' word.txt ', ' newword.txt ');
$zip->close ();
} else {
Echo ' failed, code: '. $res;
}
?>

Get comments on 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, return the original comment that has not changed
For example, when you are working with the compressed file, when you change or set the comment using the Setarchivecomment () method
If you add ziparchive::fl_unchanged to this parameter, it means to get the comment before the change, otherwise get the changed comment content
Similar to the following:
Ziparchive::getcommentindex get "file comment" based on file index in compressed file
Ziparchive::getcommentname get "file comment" based on file name in compressed file
Note: Here is the comment for the file, not the zip file

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)

The code is as follows:


$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 a compressed file (that is, delete entries within the archive) based on the index within the compressed file

Ziparchive::d Eleteindex
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

First, how to unzip a zip file Extractto ()

The code is 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 a new Ziparchive object is required
2. Then use the object's open method to create a zip file
3. Then use the AddFile method to write the file that will be packaged to the zip file you just created
4. Finally, remember to close the object

The code is as follows:


Create a new object for the Ziparchive
$zip = new Ziparchive;
$res = $zip->open (' Test.zip ');
If you open the success
if ($res = = = TRUE) {
If Open fails
} else {
Code to output error
Echo ' failed, code: '. $res;
}
$zip->close ();

The above mentioned is the whole content of this article, I hope to be helpful to everyone.

http://www.bkjia.com/PHPjc/964003.html www.bkjia.com true http://www.bkjia.com/PHPjc/964003.html techarticle PHP with zip compression, decompression class Ziparchiv use guide This article mainly introduces the PHP self-zip compression, decompression class Ziparchiv use guide, very detailed, the need for friends can refer to the following ...

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