1

I`m trying to extract a tar file using Phar Classe, but is returning the following error:

UnexpectedValueException Object ( [message:protected] => phar error: "/home/filelocation/file.tar" is a corrupted tar file (truncated) [string:Exception:private] => [code:protected] => 0 [file:protected] => /home/inplan/public_html/uncompress_files.php [line:protected] => 89 [trace:Exception:private] => Array ( [0] => Array ( [file] => /home/inplan/public_html/uncompress_files.php [line] => 89 [function] => __construct [class] => PharData [type] => -> [args] => Array ( [0] => uploads/maps/true_color_4.tar ) ) ) [previous:Exception:private] => )

PHP:

try {
    $phar = new PharData('uploads/maps/true_color_'.$id_technical_report.'.tar');
    $phar2 = $phar->convertToExecutable (Phar::TAR,Phar::NONE);
    $phar2->extractTo('uploads/maps/');
} catch (Exception $e) {
    print_r($e);
}

When I download this file, I can open and extract normally using Winrar.

The file is generated by an API from Sentinel Hub (https://services.sentinel-hub.com/api/v1/process)

Any ideas why is this happening?

Thanks

Marcos Felipe
  • 100
  • 13
  • I think the error message is quite unambiguous: _"/home/filelocation/file.tar" is a corrupted tar file (truncated)_. Please explain what you've done to resolve this, so how we can help you further. – Ro Achterberg Nov 21 '20 at 20:09
  • Actually the file isn't corrupted. I can download and extract it. – Marcos Felipe Nov 21 '20 at 20:11
  • 1
    Judging from your code, you've then uploaded it too. Are you sure the upload went well? Have you compared checksums? Sometimes files can become corrupted if the proper transfer mode was not set on an FTP session, to give just one example of what might have happened. – Ro Achterberg Nov 21 '20 at 20:13
  • The file was created in the server with the return of the API, using: file_put_contents('uploads/maps/true_color_'.$id_technical_report.'.tar',$response); I downloaded the exactly same file and in Winrar it is working properly. – Marcos Felipe Nov 21 '20 at 20:21
  • How large is it? Are you getting any other errors/warnings in the server logs? – Ro Achterberg Nov 21 '20 at 20:24
  • Btw the point of comparing checksums is to eliminate the chance of the tools behaving differently on the same file. Please perform the checksum anyway just to be positive. – Ro Achterberg Nov 21 '20 at 20:25
  • The file only have 43KB. I`m just getting the HTTP response and using file_put_contents. Is that the right way to save it? – Marcos Felipe Nov 21 '20 at 20:26

1 Answers1

0

I got the same message from an homemade tar program building a tar file. I could not untar it with the PHAR class. The problem was the CRC field, wrote on the last line of the tar file, that was miscalculated by the program that made originely that tar file.

Ben
  • 1