PHP executes the zip and rar decompression methods to implement code

Source: Internet
Author: User

Zip: PclZip
Rar: PECL 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.
Example: <? Php include ('pclzip. lib. php');?> 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 Copy codeThe Code is as follows: <? Php
Require_once ('pclzip. lib. php ');
$ Archive = new PclZip(' ');
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, suchCopy codeThe Code is as follows: <? Php
Include ('pclzip. lib. php ');
$ Archive = new PclZip(' ');
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:

Gunzip rar-xxx.tgz
Tar-xvf rar-xxx.tar
Cd rar-xxx
./Configure & make install

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

Make install

Remember to restart apache after installation
After installation, you can perform a test.Copy codeThe Code is as follows: <? Php
$ 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 thisCopy codeThe Code is as follows: <? Php
$ 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: 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.