Home  >  Article  >  Backend Development  >  PHP execution zip and rar decompression method implementation code_PHP tutorial

PHP execution zip and rar decompression method implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:20856browse

Zip: PclZip http://www.phpconcept.net/pclzip/index.en.php
Rar: PECL rar http://pecl.php.net/package/rar
In the past, it had to be executed under php The most common way to decompress a program is to write command and then use exec() and other execution functions to run it
This may be possible under Windows, but if you switch to Unix, it will not be executed smoothly due to account permission issues
Is there a method that provides functions that can be used directly without having to issue a command?
The answer is yes (it took me several days to find a method that can be used...XD )
Let’s talk about Zip first, because the PHP built-in itself provides zip-related functions (but you need to have the ziplib function first), but it is not very easy to use.
As far as extract is concerned, the built-in functions are only responsible for extracting. Simply decompress the files, instead of decompressing them sequentially according to the folder
This will lose the function of extract
As for the PclZip I am talking about, it itself provides an extension, so whether there is Ziplib or not does not matter. There’s no difference
and no installation required. You just need to include it when you use it again
For example: In this way
In addition, in extract Parts will be decompressed sequentially according to the order of the folders, instead of simply decompressing the files.
The relevant usage is like this

Copy the code 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 file*/
die("Error : ".$archive->errorInfo(true));
}
?>

Of course, you can also specify the decompression path, like this
Copy the code The code is as follows:

include('pclzip.lib.php');
$archive = new PclZip('archive.zip');
if ($archive- >extract(PCLZIP_OPT_PATH, 'data') { /*data can be replaced with other paths*/
die("Error : ".$archive->errorInfo(true));
}
?>

It would be better if you write another script that automatically creates the directory, because the function itself will not determine whether the first layer in the compressed file is a file or a folder (I think this is something else) Related functions can’t do it either! ! ! )
Then there is Rar, which is a bigger problem. Since PHP itself does not provide rar-related functions, you need to use third-party functions to use it.
Fortunately, there is PECL (The PHP Extension Community Library)
There is a rar package that can be used
But it must be installed manually
If it is 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 it is freebsd, it will be faster to install with port

cd /usr/ports/archivers/pecl-rar
make
make install

Remember to restart apache after installation
After installation, you can test it
Copy the code The code is as follows:

$rar_file = rar_open('example.rar') or die("Failed to open Rar archive");
/*example.rar can be changed to other files*/
$entries_list = rar_list($rar_file);
print_r($entries_list);
?>

It should be noted that if you use port to install, the version will be relatively new (the official website only has 0.3.1, if you install port has reached 0.3.4), so there will be some differences in usage
but there is no difference in the usage of extract
The relevant usage is like this
Copy code The code is as follows:

$rar_file = rar_open('example.rar') or die("Can't open Rar archive");
/*example. rar can be changed to other files*/
$entries = rar_list($rar_file);
foreach ($entries as $entry) {
$entry->extract('/dir/extract/ to/'); /*/dir/extract/to/just change to another path*/
}
rar_close($rar_file);
?>

Like the Zip part, it would be better if it is paired with automatic directory creation.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322598.htmlTechArticleZip: PclZip http://www.phpconcept.net/pclzip/index.en.php Rar: PECL rar http://pecl.php.net/package/rar In the past, the most common way to execute the decompression program under PHP was to write...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn