【问题标题】:Can PHP decompress a taz file? (.tar.Z)PHP 可以解压 taz 文件吗? (.tar.Z)
【发布时间】:2016-02-09 19:46:30
【问题描述】:

我尝试使用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);

EDIT2:我认为 LZW 不起作用的原因是 .tar.Z 文件的内容如下所示:

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

我尝试过的 LZW 函数都在其字典中使用 ASCII 字符。这些是什么角色?

【问题讨论】:

  • 你想用纯php来完成吗?你在linux服务器上运行吗?你有能力跑execshell_exec吗?
  • 我不能使用execshell_exec,我更喜欢使用纯PHP。
  • 不要这样链接函数。检查file_get_contents() 是否真的能够读取该文件。如果不能,它会返回布尔值false,然后它会被盲目地传递给zlib_decode(),它当然不能解码false——字符串上下文中的布尔值false是一个空字符串。
  • 随意移植压缩算法,或者做其他人都会做的事情,调用 uncompress / tar 类似 exec() / system() / proc_open() -- github.com/vapier/ncompress/blob/ncompress-4.2.4/compress42.c
  • 您确定存档有效吗?您是否设法在命令行中解压缩它?

标签: php compression lzw


【解决方案1】:

所以你想用 PHP本地解压 taz 文件?试试我的new extension

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 压缩。

【讨论】:

    【解决方案2】:

    根据这个网址https://kb.iu.edu/d/acsy你可以试试

    <?php
    
    $file = '/tmp/archive.z';
    shell_exec("uncompress $file"); 
    

    如果您没有类似 Unix 的操作系统,请检查 https://kb.iu.edu/d/abck 以获取适当的程序。

    【讨论】:

    • OP 表示他对外部源不感兴趣“我不能使用 exec 或 shell_exec,我更喜欢使用纯 PHP”,此外,您也可以通过 tar 运行它,而你在它。
    【解决方案3】:

    该文件是用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?也可以完全在命令行上完成它,并为自己节省一些打字和性能......
    • 正确,主要是为了以更精细的方式捕获错误。
    • 现在有a reliable method 用于在 PHP 中解压这些文件 :)
    【解决方案4】:

    请试试这个。

     <?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/en/phardata.extractto.php 我还没有测试过,但我希望它对你有用。

    【讨论】:

    • 你没有注意。他需要先使用 LZW 压缩解压缩文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    相关资源
    最近更新 更多