PHP中的提取ZIP
使用Extractor类使用PHP将存档文件(zip,gzip和rar)提取到指定目的地。
- 包括并初始化提取器类。
- 指定要提取的存档文件(ZIP/GZIP/RAR)的路径。
- 指定要在服务器上提取zip文件的目标。
- 调用Extractor类的extract()函数。
//Include and initialize Extractor class
require_once 'Extractor.class.php';
$extractor = new Extractor;
//Path of archive file
$archivePath = '/path/to/archive.zip';
//Destination path
$destPath = '/destination/dir/';
//Extract archive file
$extract = $extractor->extract($archivePath, $destPath);
if($extract){
echo $GLOBALS['status']['success'];
}else{
echo $GLOBALS['status']['error'];
}
存档文件用于数据压缩。
zip,gzip和rar是常用的文件格式到存档文件。
通常,我们使用计算机软件提取存档文件(zip,gzip,rar等)。
但是,可以使用PHP在飞行中提取存档文件。
我们可以轻松地动态创建ZIP文件,并从带有PHP的脚本中提取ZIP文件。
ZiParchive类提供了一种简单的方法来提取PHP中的存档内容。
我们可以使用PHP中的Zi,7,在服务器上提取ZIP文件。
在本教程中,我们将展示如何在PHP中提取ZIP文件。
不仅可以使用我们的示例代码片段将在PHP中提取其他存档文件(GZIP和RAR)。
要使存档提取过程简单,我们已在PHP类中分组所有代码,并创建了一个名为Extrutionor的自定义库。
此库提供了一个即时和结构化的方法来实现PHP中的文件提取功能。
提取器类
Extractor类有助于使用PHP Zi,7,在服务器上提取存档文件(zip/gzip/rar)。
- extract() - 此函数检查给定文件的扩展并调用合适的提取器功能。
- '$archive' - 要提取的存档文件的相对路径。
- '$destination' - 提取文件的位置。
- ExtractZipArchive() - 它是Extractor类的辅助功能。此函数使用PHP中的ZIPArchive解压缩或者解压缩ZIP存档文件(.zip)。
- ExtractGzipfile() - 它是Extractor类的辅助功能。此函数使用PHP中的GZREAD()解压缩或者解压缩GZIP存档文件(.gz)。
- extractraRarchive() - 它是提取器类的辅助功能。此函数使用RarArchive在PHP中解压缩或者提取RAR存档文件(.rar)。
<?php
/**
* Class Extractor
*
* Extract a archive (zip/gzip/rar) file.
*
* @author onitroad
* @url https://www.onitroad.com
*
*/
class Extractor {
/**
* Checks file extension and calls suitable extractor functions.
*
* @param $archive
* @param $destination
*/
public static function extract($archive, $destination){
$ext = pathinfo($archive, PATHINFO_EXTENSION);
switch ($ext){
case 'zip':
$res = self::extractZipArchive($archive, $destination);
break;
case 'gz':
$res = self::extractGzipFile($archive, $destination);
break;
case 'rar':
$res = self::extractRarArchive($archive, $destination);
break;
}
return $res;
}
/**
* Decompress/extract a zip archive using ZipArchive.
*
* @param $archive
* @param $destination
*/
public static function extractZipArchive($archive, $destination){
//Check if webserver supports unzipping.
if(!class_exists('ZipArchive')){
$GLOBALS['status'] = array('error' => 'Your PHP version does not support unzip functionality.');
return false;
}
$zip = new ZipArchive;
//Check if archive is readable.
if($zip->open($archive) === TRUE){
//Check if destination is writable
if(is_writeable($destination . '/')){
$zip->extractTo($destination);
$zip->close();
$GLOBALS['status'] = array('success' => 'Files unzipped successfully');
return true;
}else{
$GLOBALS['status'] = array('error' => 'Directory not writeable by webserver.');
return false;
}
}else{
$GLOBALS['status'] = array('error' => 'Cannot read .zip archive.');
return false;
}
}
/**
* Decompress a .gz File.
*
* @param $archive
* @param $destination
*/
public static function extractGzipFile($archive, $destination){
//Check if zlib is enabled
if(!function_exists('gzopen')){
$GLOBALS['status'] = array('error' => 'Error: Your PHP has no zlib support enabled.');
return false;
}
$filename = pathinfo($archive, PATHINFO_FILENAME);
$gzipped = gzopen($archive, "rb");
$file = fopen($filename, "w");
while ($string = gzread($gzipped, 4096)) {
fwrite($file, $string, strlen($string));
}
gzclose($gzipped);
fclose($file);
//Check if file was extracted.
if(file_exists($destination.'/'.$filename)){
$GLOBALS['status'] = array('success' => 'File unzipped successfully.');
return true;
}else{
$GLOBALS['status'] = array('error' => 'Error unzipping file.');
return false;
}
}
/**
* Decompress/extract a Rar archive using RarArchive.
*
* @param $archive
* @param $destination
*/
public static function extractRarArchive($archive, $destination){
//Check if webserver supports unzipping.
if(!class_exists('RarArchive')){
$GLOBALS['status'] = array('error' => 'Your PHP version does not support .rar archive functionality.');
return false;
}
//Check if archive is readable.
if($rar = RarArchive::open($archive)){
//Check if destination is writable
if (is_writeable($destination . '/')) {
$entries = $rar->getEntries();
foreach ($entries as $entry) {
$entry->extract($destination);
}
$rar->close();
$GLOBALS['status'] = array('success' => 'File extracted successfully.');
return true;
}else{
$GLOBALS['status'] = array('error' => 'Directory not writeable by webserver.');
return false;
}
}else{
$GLOBALS['status'] = array('error' => 'Cannot read .rar archive.');
return false;
}
}
}
日期:2020-06-02 22:15:30 来源:oir作者:oir
