0

when I am trying to extract tar.gz file, I found that some of extraction show checksum error, invalid header block at offset unknown, Couldn't read chunk at offset unknown. And the extraction is failed. Only certain tarball having this issue while others has no problem. I believe there is something wrong in the tarball? But I could not sure.

use strict;
use warnings;
use Archive::Tar;
$tar->read("x.tar.gz");
$tar->extract();
Blurman
  • 195
  • 9
  • 1
    Use an appropriate tag for the language used. – Cyrus Mar 20 '20 at 01:31
  • Can you use`tar` on the file? – Shawn Mar 20 '20 at 05:31
  • 1
    `.gz` usually indicates a gzip-compressed file, and I presume that Archive::Tar expects a tar file. – ikegami Mar 20 '20 at 05:57
  • 1
    ...Except it looks like Archive::Tar should handle gzipped tar files just fine. You could pass a second argument of `COMPRESS_GZIP` to `read` to be explicit about it. Maybe you have an older version of Archive::Tar that requires this. What version of Archive::Tar do you have? (This can be obtained from `$Archive::Tar::VERSION`.) – ikegami Mar 20 '20 at 06:00
  • Documentation [Archive::Tar](https://perldoc.perl.org/Archive/Tar.html) look for GZIP (extension `.gz` indicates that file is compressed with gzip). – Polar Bear Mar 20 '20 at 06:06
  • For an assurance test the 'tar' archive with command `tar ztvf x.tar.gz`. If test passes then file is in OK state. – Polar Bear Mar 20 '20 at 06:07
  • Documentation [tar](http://linuxcommand.org/lc3_man_pages/tar1.html) just in case if you need to find command options. – Polar Bear Mar 20 '20 at 06:11
  • Hi, yes i can use 'tar ztvf' on that file. Would have no issue using '$Archive::Tar' i think. I tried to pass 'COMPRESS_GZIP' to 'read' but nothing happens. As mentioned about older version, may I know how could i check the version? Thanks!! – Blurman Mar 20 '20 at 08:54
  • 1
    @Blurman Check `Archive::Tar->VERSION`. There are details and other methods of checking the version number here: https://stackoverflow.com/questions/135755/how-can-i-find-the-version-of-an-installed-perl-module – Possum Mar 20 '20 at 14:21

1 Answers1

2

Files with extension '.tgz' and '.tar.gz' represent tar achieve compressed with gzip algorithm.

To extract such files with perl code requires to specify compression schema.

Following piece of code demonstrate creation of tar archive of all *.pl files in current directory with gzip compression and then list all added files in the archive.

use strict;
use warnings;
use feature 'say';

use Archive::Tar;

my $archive = 'files.tar.gz';
my $tar = Archive::Tar->new;

$tar->add_files(glob('*.pl'));
$tar->write($archive, COMPRESS_GZIP);
$tar->clear;

say for $tar->list_archive($archive, COMPRESS_GZIP);

How to extract GZIP compressed tar archive

use strict;
use warnings;
use feature 'say';

use Archive::Tar;

my $archive = 'files.tar.gz';
my $tar = Archive::Tar->new;

$tar->read($archive,COMPRESS_GZIP);
$tar->extract();
Polar Bear
  • 6,762
  • 1
  • 5
  • 12
  • I tried '$tar->read($archive,COMPRESS_GZIP);', seems make no difference. – Blurman Mar 20 '20 at 08:59
  • @Blurman -- have you tried to test the file with `tar ztvf filename.tar.gz`? If the command will report an error then the file probably is not complete or was downloaded/copied with an error (less probable). – Polar Bear Mar 20 '20 at 09:40