Search code examples
phpcompressionlzw

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?


Solution

  • 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.