PHP implementation zip and RAR decompression method implementation Code _php tutorial

Source: Internet
Author: User
rar:pecl rar Http://
In the past to perform the decompressor under PHP, the most common way is to write command and then use EXEC () and other execution functions to run
This may be possible under Windows, but switching to UNIX can be a problem for account permissions and cannot be executed successfully
Is there a way of providing a function that can be used directly without having to go to the command to run?
The answer is (say, for several days to find a way to use ...) XD)
First zip, because PHP built in itself provides a zip-related function (but must first have ziplib function) but not very good
In light of the extract, the built-in function is only responsible for the simple decompression of the file, but not in accordance with the Data folder in order to extract
So we lose extract.
and to speak of Pclzip this, itself has provided extension, so there is no ziplib is not bad
And no installation, just need to use him when the include comes in.
For example: Such
In addition, in the Extract section, it will be extracted in accordance with the order of the Data folder, rather than simply unzip the file
Related usage like this
Copy CodeThe code is as follows:
Require_once (' pclzip.lib.php ');
$archive = new Pclzip (' ');
if ($archive->extract () = = 0) {/* Unzip the path to the same path as the original file */
Die ("Error:". $archive->errorinfo (true));

Of course, you can also specify the decompression path, like this
Copy CodeThe code is as follows:
Include (' pclzip.lib.php ');
$archive = new Pclzip (' ');
if ($archive->extract (pclzip_opt_path, ' data ') {/*data change to a different path */
Die ("Error:". $archive->errorinfo (true));

It would be better to write a script that automatically creates a directory, because the function itself does not determine whether the first layer in the compressed file is a file or a folder (which I think other related functions cannot!!!) )
Again is RAR, this problem is relatively large, because PHP itself does not provide a RAR-related functions, so need to help third-party function to use
Fortunately there is this PECL (the PHP Extension Community Library)
There is a RAR package that can be used
But it has to be installed manually.
If Unix, you can refer to the following installation method

Gunzip rar-xxx.tgz
TAR-XVF Rar-xxx.tar
CD rar-xxx
./configure && make && make install

Of course, if FreeBSD, the port will be faster

Make install

Remember to restart Apache after installation
Can be tested after installation
Copy CodeThe code is as follows:
$rar _file = Rar_open (' Example.rar ') or Die ("Failed to open rar archive");
/*example.rar change to other files */
$entries _list = rar_list ($rar _file);
Print_r ($entries _list);

The comparison to note, if the port installation, the version will be relatively new (official website only to 0.3.1,port installed to 0.3.4), so the usage will be a bit of a discrepancy
But there's no difference in extract usage.
Related usage like this
Copy CodeThe code is as follows:
$rar _file = Rar_open (' Example.rar ') or Die ("Can ' t open rar archive");
/*example.rar change to other files */
$entries = rar_list ($rar _file);
foreach ($entries as $entry) {
$entry->extract ('/dir/extract/to/'); /*/dir/extract/to/change to another path */
Rar_close ($rar _file);

As with the zip section, it's better to set up a directory automatically true techarticle zip:pclzip rar:pecl Rar Http:// In the past to execute the decompression program under PHP, the most common way is to write ...

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