Blurman
Blurman

Reputation: 609

Perl Could not extract tarball using Archive::Tar

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

Upvotes: 0

Views: 492

Answers (1)

Polar Bear
Polar Bear

Reputation: 6798

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

Upvotes: 2

Related Questions