PHP implementation zip and RAR decompression method implementation code

Source: Internet
Author: User
Tags copy functions include rar zip
Zip:pclzip http://www.phpconcept.net/pclzip/index.en.php
rar:pecl rar Http://pecl.php.net/package/rar
In the past to perform the decompression program in PHP, the most common way is to write command and then use EXEC () and other executive functions to run
This may be possible under Windows, but Unix will not be able to run smoothly due to account permissions issues
Does that have a way of providing functions that you can use directly without having to go down command to run?
The answer is (say) find a few days to find the way to use ... XD)
Zip First, because the PHP built-in provides a zip-related function (but need to have a ziplib function) but not very useful
In light of the extract, the built-in function is only responsible for the simple decompression of the file eucalyptus out, rather than according to the folder in order to extract
It's going to lose extract.
And to speak of this pclzip, itself has provided extension, so there is no ziplib no bad
and is free of installation, only need to use his time include in the
Example: <?php include (' pclzip.lib.php ');?>
In addition, in the Extract section, it will be in accordance with the order of the folder to extract, rather than simply extract the file Eucalyptus out
Related usage like this
Copy CodeThe code is as follows:
<?php
Require_once (' pclzip.lib.php ');
$archive = new Pclzip (' Archive.zip ');
if ($archive->extract () = = 0) {/* The decompression path is 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:
<?php
Include (' pclzip.lib.php ');
$archive = new Pclzip (' Archive.zip ');
if ($archive->extract (pclzip_opt_path, ' data ') {/*data replaced by another path */
Die ("Error:". $archive->errorinfo (true));
}
?>

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

Fetch HTTP://PECL.PHP.NET/GET/RAR-X.X.X.TGZ
Gunzip rar-xxx.tgz
TAR-XVF Rar-xxx.tar
CD rar-xxx
Phpize
./configure && make && make install

Of course, if FreeBSD, it would be quicker to use port.

Cd/usr/ports/archivers/pecl-rar
Make
Make install

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

The comparison should note, if uses the port installs the words, the version will be relatively new (official website only then 0.3.1,port installs the word already to 0.3.4), therefore the usage will have some discrepancy
But there is no difference in extract usage.
Related usage like this
Copy CodeThe code is as follows:
<?php
$rar _file = Rar_open (' Example.rar ') or Die ("Can ' t open rar archive");
/*example.rar replaced by other files Eucalyptus can be * *
$entries = rar_list ($rar _file);
foreach ($entries as $entry) {
$entry->extract ('/dir/extract/to/'); /*/dir/extract/to/replaced by other paths can be * *
}
Rar_close ($rar _file);
?>

As with the zip section, it would be better if you set up catalogs automatically.

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.