Home  >  Article  >  Backend Development  >  PHP解压tar.gz格式文件的方法_PHP

PHP解压tar.gz格式文件的方法_PHP

WBOY
WBOYOriginal
2016-05-28 11:48:331119browse

本文实例讲述了PHP解压tar.gz格式文件的方法。分享给大家供大家参考,具体如下:

1、运用php自带压缩与归档扩展(phar)

$phar = new PharData('song.tar.gz');
//路径 要解压的文件 是否覆盖
$phar->extractTo('c:/tmp', null, true);

2、首先需要安装pear类管理工具

linux下

/usr/local/php/bin/pear install Archive_Tar

wamp默认没有安装pear,  go-pear.php  适合旧版本的 , php5.3 需要 下载 go-pear.phar

将下载的 文件放到 php目录下(任意目录下都可以) 执行 php go-pear.phar 命令,根据提示按enter即可

①、安装

#这个类实际安装pear时默认就会安装
pear install archive_tar

②、

/**
原理:tar压缩包前512字节有自己的固定格式,根据这512个字节可以获取压缩包的文件名,大小等信息
在配合 gzopen,gzread 便可以解压gz格式
 */
$tar_object = new Archive_Tar("song.tar.gz");
$tar_object->extract('c:/tmp', true);

3、php调用外部命令来执行解压

//必须等命令执行完成,脚本才会推出,popen命令并非异步去执行
error_reporting(E_ALL);
ini_set("display_errors", 1);
$handle = popen("tar -xzvf song.tar.gz 2>&1", 'r');
/*
while(!feof($handle)) {
set_time_limit(3);
  $buffer = fgets($handle);
  echo "$buffer\n";
  ob_flush();
  flush();
}*/
echo "start \n";
pclose($handle);
echo "end \n";

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP操作zip文件及压缩技巧总结》、《PHP基本语法入门教程》、《PHP错误与异常处理方法总结》及《php常用函数与技巧总结》

希望本文所述对大家PHP程序设计有所帮助。

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