PHP performs zip and rar unzip method implementation code

  • 2020-03-31 21:25:10
  • OfStack

Zip: 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 unzip a program in PHP was to write command and execute functions like exec()
This may be possible under Windows, but switching to Unix would not work because of account permissions issues
So is there a way to have a function that you can use without having to go to command
It took me several days to find a way to do it... XD)
Zip first, since PHP is built into itself to provide Zip related functions (but you have to have a ziplib function first) but not very good
In the case of extract alone, the built-in function is only responsible for extracting files, not extracting them in sequence
This loses the value of extract
And to talk about the PclZip, itself has to provide extension, so there is no Ziplib is not bad
No installation, just include it when you use it again
For example: < ? PHP include (' pclzip. Lib. PHP); ? > such
In addition, extract will be extracted in the order of the folder, rather than the extract file
The usage is like this
 
<?php 
require_once('pclzip.lib.php'); 
$archive = new PclZip('archive.zip'); 
if ($archive->extract() == 0) {  
die("Error : ".$archive->errorInfo(true)); 
} 
?> 

You can also specify the unzip path, like this
 
<?php 
include('pclzip.lib.php'); 
$archive = new PclZip('archive.zip'); 
if ($archive->extract(PCLZIP_OPT_PATH, 'data') {  
die("Error : ".$archive->errorInfo(true)); 
} 
?> 

It would be better to write a script that automatically creates a directory, because the function itself does not know whether the first layer of the file is a file or a folder.
Then there is Rar, which is a big problem. Since PHP itself does not provide Rar related functions, it needs to ask a third party function to use it
Fortunately, PECL (The PHP Extension Community Library)
The rar package is available
But you have to install it manually
For Unix, you can refer to the following installation method

The fetch http://pecl.php.net/get/rar-x.x.x.tgz
Gunzip rar - XXX. TGZ
The tar XVF rar - XXX. The tar
CD rar - XXX
phpize
./configure && make & make install

Of course, if freebsd, port will be faster

CD/usr/ports/archivers/pecl - rar
The make
Make install

Remember to restart apache after installation
After the installation can be done testing
 
<?php 
$rar_file = rar_open('example.rar') or die("Failed to open Rar archive"); 
 
$entries_list = rar_list($rar_file); 
print_r($entries_list); 
?> 

Note that if port is installed, the version will be relatively new (the official website is only up to 0.3.1, and the port installation is up to 0.3.4), so there will be some differences in usage
But there is no difference in extract usage
The usage is like this
 
<?php 
$rar_file = rar_open('example.rar') or die("Can't open Rar archive"); 
 
$entries = rar_list($rar_file); 
foreach ($entries as $entry) { 
$entry->extract('/dir/extract/to/'); /*/dir/extract/to/ I can just switch to some other path */ 
} 
rar_close($rar_file); 
?> 

As with the Zip section, it's better to set up the directory automatically

Related articles: