Reputation: 31
Not able to extract a static tar file to custom destination directory:
########################
#
use strict;
use warnings;
use Archive::Tar;
my $source_path='C:\test\Hotfix.tar';
my $dest_path='C:\test\abc\\';
my $tar=Archive::Tar->new();
$tar->read($source_path);
#$tar->extract();
$tar->extract($source_path, $dest_path );
The above script perfectly works if I uncomment #$tar->extract();
. It will extract to the current directory from where I am executing, but my requirement is to extract to $dest_path
. Can you please help me where I am doing mistake?
Upvotes: 2
Views: 134
Reputation: 69314
I recommend reading the documentation for Archive::Tar. The documentation for extract()
says this:
$tar->extract( [@filenames] )
Write files whose names are equivalent to any of the names in @filenames to disk, creating subdirectories as necessary. This might not work too well under VMS. Under MacPerl, the file's modification time will be converted to the MacOS zero of time, and appropriate conversions will be done to the path. However, the length of each element of the path is not inspected to see whether it's longer than MacOS currently allows (32 characters).
If extract is called without a list of file names, the entire contents of the archive are extracted.
So it simply doesn't support the parameters that you're trying to use. It expects either zero or one parameter [Note: This is wrong. See correction below]. If you give it no parameters, it will extract all of the files. If you give it one parameter, then that is expected to be a reference to an array of filenames - it will then try to extract those files from the archive.
I suspect you're getting confused with the parameters for extract_file()
. That takes an optional second parameter which can be used to give the name that you want the extracted file written as.
Update: As mentioned in a comment below, It looks like I misread the documentation. The [...]
in the documentation indicate optional parameters, not an array reference.
So actually, extract()
takes any number of parameters. If you pass it zero parameters, it will extract all of the files from the archive. Any parameters you give it are taken to be filenames and it will extract those files from the archive.
However, I'm still correct in saying that the last parameter passed to extract()
is never interpreted as the destination for the extraction.
Upvotes: 0
Reputation: 62236
Archive::Tar extract
does not work that way. You could chdir into the destination directory before executing extract
:
use warnings;
use strict;
use Archive::Tar;
my $source_path = 'C:\test\Hotfix.tar';
my $dest_path = 'C:\test\abc\\';
my $tar = Archive::Tar->new();
$tar->read($source_path);
chdir $dest_path;
$tar->extract();
Upvotes: 2