PHP executes the zip and rar decompression methods to implement code

Source: Internet
Author: User
PHP executes the zip and rar decompression methods first. it will be used for further research. Zip: PclZip http://www.phpconcept.net/pclzip/index.en.php
Rar: PECL rar http://pecl.php.net/package/rar
In the past, we had to execute the decompression program in php. The most common method was to write the command and run it using the execute function such as exec ().
This may work in Windows, but changing to Unix won't be able to run smoothly due to account permissions issues.
Is there any method that provides a function that can be used directly without having to run the command?
Answer: I have (it takes several days to find a method that can be used... XD)
First, let's talk about Zip. because php has built-in zip-related functions (but ziplib functions must be provided first), it is not very useful.
As far as extract is concerned, the built-in function is only responsible for extracting files, rather than extracting files according to folders in sequence.
In this way, the role of extract is lost.
The PclZip file itself provides extension, so there is no difference in Ziplib.
Without installation, you only need to include it when using it again.
For example: This way
In addition, in the extract part, the files are extracted in order of folders, instead of simply extracting files.
Related usage is like this

The code is as follows:


Require_once ('pclzip. lib. php ');
$ Archive = new PclZip('archive.zip ');
If ($ archive-> extract () = 0) {/* the decompression path is the same as the original path */
Die ("Error:". $ archive-> errorInfo (true ));
}
?>


You can also specify the decompression path, such

The code is as follows:


Include ('pclzip. lib. php ');
$ Archive = new PclZip('archive.zip ');
If ($ archive-> extract (PCLZIP_OPT_PATH, 'data') {/* change data to another path */
Die ("Error:". $ archive-> errorInfo (true ));
}
?>


It would be better if you write another script that automatically creates a directory, because the function itself does not determine whether the first layer in the compression file is a folder or folder (I don't think other related functions can do this too !!!)
Rar again, which is a big problem. because php does not provide the rar-related function, you need to seek help from a third-party function.
Fortunately, this PECL (The PHP Extension Community Library)
There is a rar package available for use.
However, you must install it manually.
For Unix, 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 install

Of course, if freebsd is used, port installation will be faster.

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

Remember to restart apache after installation
After installation, you can perform a test.

The code is as follows:


$ Rar_file = rar_open('example.rar ') or die ("Failed to open Rar archive ");
/* Replace example.rar with another folder */
$ Entries_list = rar_list ($ rar_file );
Print_r ($ entries_list );
?>


Note that if port is used for installation, the version will be updated (only 0.3.1 on the official website and 0.3.4 on port), so there will be some discrepancies in usage.
However, there is no difference in extract usage.
Related usage is like this

The code is as follows:


$ Rar_file = rar_open('example.rar ') or die ("Can't open Rar archive ");
/* Replace example.rar with another folder */
$ Entries = rar_list ($ rar_file );
Foreach ($ entries as $ entry ){
$ Entry-> extract ('/dir/extract/to/');/*/dir/extract/to another path */
}
Rar_close ($ rar_file );
?>


Like Zip, it would be better to create a directory 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.