没有 exec() 的 PHP Untar-gz?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9416508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:46:37  来源:igfitidea点击:

PHP Untar-gz without exec()?

phpexectar

提问by Hyman Wilsdon

How would I untar-gz a file in php without the use of exec('tar')or any other commands, using pure PHP?

如何在不使用exec('tar')或任何其他命令的情况下使用纯 PHP解压 php 中的文件?

My problem is as follows; I have a 26mb tar.gz file that needs to be uploaded onto my server and extracted. I have tried using net2ftp to extract it, but it doesn't support tar.gz uncompressing after upload.

我的问题如下;我有一个 26mb 的 tar.gz 文件需要上传到我的服务器并提取。我试过用net2ftp解压,但不支持tar.gz上传后解压。

I'm using a free web host, so they don't allow any exec()commands, and they don't allow access to a prompt. So how would I go about untaring this?

我使用的是免费的网络主机,所以他们不允许任何exec()命令,也不允许访问提示。那么我将如何解开这个呢?

Does PHP have a built in command?

PHP 有内置命令吗?

回答by j0k

Since PHP 5.3.0 you do not need to use Archive_Tar.

从 PHP 5.3.0 开始,您不需要使用Archive_Tar.

There is new class to work on tar archive: The PharData class.

有一个新的类可以处理 tar 存档:PharData 类

To extract an archive (using PharData::extractTo()which work like the ZipArchive::extractTo()):

要提取存档(使用PharData::extractTo()类似于 的工作方式ZipArchive::extractTo()):

try {
    $phar = new PharData('myphar.tar');
    $phar->extractTo('/full/path'); // extract all files
} catch (Exception $e) {
    // handle errors
}

And if you have a tar.gz archive, just decompress it before extract (using PharData::decompress()):

如果您有 tar.gz 存档,只需在提取前解压缩(使用PharData::decompress()):

// decompress from gz
$p = new PharData('/path/to/my.tar.gz');
$p->decompress(); // creates /path/to/my.tar

// unarchive from the tar
$phar = new PharData('/path/to/my.tar');
$phar->extractTo('/full/path');

回答by netcoder

PEAR provides the Archive_Tarclass, which supports both Gzip and BZ2 compressions, provided you have the zlib and bz2 extensions loaded, respectively.

PEAR 提供了Archive_Tar,它支持 Gzip 和 BZ2 压缩,前提是您分别加载了 zlib 和 bz2 扩展。