UndoingTech
UndoingTech

Reputation: 729

Can PHP decompress a taz file? (.tar.Z)

I have tried to use Zlib to decompress the file, but it just said "Data error" and gave me an empty file.

This is the code I tried:

// 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);

I have also tried to decompress it in parts, going from .tar.Z to .tar to just a file. I tried using lzw functions to take off the .Z, but I was unable to make it work. Is there a way to do this?

EDIT: Here is some more code I have tried. Just to make sure the file_get_contents was working. I still get a "data error".

$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: I think the reason why LZW was not working is because the contents of the .tar.Z file looks like this:

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

The LZW functions I have tried both use ASCII characters in their dictionaries. What kind of characters are these?

Upvotes: 13

Views: 1009

Answers (4)

quickshiftin
quickshiftin

Reputation: 69681

So you want to decompress a taz file natively with PHP? Give my new extension a try!

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');

Also note, the reason the zlib functions aren't working is because you need LZW compression, not gzip compression.

Upvotes: 4

Manoj K
Manoj K

Reputation: 477

Please try this.

 <?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
    }
    ?>

Source : http://php.net/manual/en/phardata.extractto.php I haven't tested it but i hope it will work for you.

Upvotes: -2

miken32
miken32

Reputation: 42712

The file is compressed with LZW compression, and I tried a few but there seems to be no reliable method for decompressing these in PHP. Cosmin's answer contains the correct first step but after using your system's uncompress utility to decompress the file, you still have to extract the TAR file. This can be done with PHP's built-in tools for handling its custom PHAR files.

// 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;
}

Upvotes: 0

Cosmin Ordean
Cosmin Ordean

Reputation: 165

according to this url https://kb.iu.edu/d/acsy you can try

<?php

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

if you don't have Unix like OS check https://kb.iu.edu/d/abck for appropriate program.

Upvotes: 1

Related Questions