PHP能解压taz文件吗?(.tar.Z)

13

我尝试使用Zlib来解压文件,但它只是显示“数据错误”并给了我一个空文件。

这是我尝试的代码:

// Open a new temp file to write new file to
$tempFile = fopen("tempFile", "w");
// Make sure tempFile is empty
ftruncate($tempFile, 0);

// Write new decompressed file 
fwrite($tempFile, zlib_decode(file_get_contents($path))); // $path = absolute path to data.tar.Z

// close temp file
fclose($tempFile);

我也尝试将它分为几部分进行解压,从.tar.Z到.tar再到一个文件。我尝试使用LZW函数去掉.Z,但是未能使其正常工作。有没有办法可以解决这个问题?

编辑: 这里是我尝试的一些代码。只是为了确保file_get_contents正常工作。我仍然收到“数据错误”的消息。

$tempFile = fopen("tempFile.tar", "w");
// Make sure tempFile is empty
ftruncate($tempFile, 0);

// Write new decompressed file 
$contents = file_get_contents($path);
if ($contents) {
    fwrite($tempFile, gzuncompress($contents));
}

// close temp file
fclose($tempFile);

编辑2: 我认为LZW无法正常工作的原因是因为.tar.Z文件的内容看起来像这样:

��3dЀ��0p���a�
H�H��ŋ3j��@�6l�

我尝试过的LZW函数都在它们的词典中使用ASCII字符,这些字符是什么类型的?


1
随意移植压缩算法,或者像其他人一样,使用exec() / system() / proc_open()调用uncompress / tar -- https://github.com/vapier/ncompress/blob/ncompress-4.2.4/compress42.c - hanshenrik
1
你确定归档文件是有效的吗?你能在命令行中成功解压缩它吗? - max
统一归档是一个很好的库,因为它为所有归档类型提供了一致的API,但它仍然依赖于相关的PHP扩展程序的安装,以便能够实际处理任何给定的文件类型。 - Simba
@UndoingTech,我为你创建了一个新的PHP扩展。看看我的新答案。 - quickshiftin
你看过这个吗?链接:点我 :) - pegas
显示剩余13条评论
4个回答

4

你想使用PHP本身解压缩taz文件吗?试试我的扩展程序

lzw_decompress_file('3240_05_1948-1998.tar.Z', '3240_05_1948-1998.tar');
$archive = new PharData('/tmp/3240_05_1948-1998.tar');
mkdir('unpacked');
$archive->extractTo('unpacked');

此外,请注意,zlib函数无法正常工作的原因是您需要LZW压缩,而不是gzip压缩。

1

原帖作者已经表明不想使用外部资源,“我不能使用exec或shell_exec,我更喜欢使用纯PHP”。此外,你可以顺便将其运行通过tar - quickshiftin

0

该文件使用LZW压缩,我尝试了几种方法,但似乎没有可靠的PHP解压缩方法。Cosmin的答案包含了正确的第一步,但在使用系统的uncompress实用程序解压缩文件后,您仍然需要提取TAR文件。这可以使用PHP内置工具来处理其自定义PHAR文件完成。

// the file we're getting
$url = "ftp://ftp.ncdc.noaa.gov/pub/data/hourly_precip-3240/05/3240_05_2011-2011.tar.Z";
// where to save it
$output_dir = ".";
// get a temporary file name
$tempfile = sys_get_temp_dir() . basename($url);
// get the file
$compressed_data = file_get_contents($url);
if (empty($compressed_data)) {
    echo "error getting $url";
    exit;
}
// save it to a local file
$result = file_put_contents($tempfile, $compressed_data);
if (!$result) {
    echo "error saving data to $tempfile";
    exit;
}
// run the system uncompress utility
exec("/usr/bin/env uncompress $tempfile", $foo, $return);
if ($return == 0) {
    // uncompress strips the .Z off the filename
    $tempfile = preg_replace("/.Z$/", "", $tempfile);
    // remove .tar from the filename for use as a directory
    $tempdir = preg_replace("/.tar$/", "", basename($tempfile));
    try {
        // extract the tar file
        $tarchive = new PharData($tempfile);
        $tarchive->extractTo("$output_dir/$tempdir");
        // loop through the files
        $dir = new DirectoryIterator($tempdir);
        foreach ($dir as $file) {
            if (!$file->isDot()) {
                echo $file->getFileName() . "\n";
            }
        }
    } catch (Exception $e) {
        echo "Caught exception untarring: " . $e->getMessage();
        exit;
    }
} else {
    echo "uncompress returned error code $return";
    exit;
}

如果你已经决定使用外部的“compress”程序,那么为什么还要使用“PharData”呢?在这种情况下,最好完全在命令行上执行它,以节省一些打字和提高性能。 - quickshiftin
主要是为了以更细粒度的方式捕获错误。 - miken32
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - quickshiftin

-2
请尝试这个。
 <?php
    try {
        $phar = new PharData('myphar.tar');
        $phar->extractTo('/full/path'); // extract all files
        $phar->extractTo('/another/path', 'file.txt'); // extract only file.txt
        $phar->extractTo('/this/path',
            array('file1.txt', 'file2.txt')); // extract 2 files only
        $phar->extractTo('/third/path', null, true); // extract all files, and overwrite
    } catch (Exception $e) {
        // handle errors
    }
    ?>

来源:http://php.net/manual/zh/phardata.extractto.php 我没有测试过,但希望它能对你有用。


1
你没有注意到。他需要先使用LZW压缩解压文件。 - quickshiftin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接