Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Archive::Tar extract

by gautamparimoo (Beadle)
on Apr 17, 2012 at 09:26 UTC ( #965469=perlquestion: print w/replies, xml ) Need Help??
gautamparimoo has asked for the wisdom of the Perl Monks concerning the following question:


I want to extract files from a .tar.gz archive one at a time and perform some computation on each file . Pls suggest a solution .. I tried the given below script but it is not sucessful.

use Archive::Tar; $input="libpst.tar.gz"; my $tar = Archive::Tar->new($input); my @files = $tar->list_files; foreach my $file (@files) { $tar->extract_file($file, "C:/extract/$input"); my $c++; print "$c"; }

The above script gives an error --> Could Not Open File C:/extract/libpst.tar.gz : Permission denied. I think i am not passing the correct extract_path as i want to extract the archive in a different directory. Please help?

Replies are listed 'Best First'.
Re: Archive::Tar extract
by DrHyde (Prior) on Apr 17, 2012 at 10:25 UTC

    If I read the Archive::Tar documentation correctly, you're trying to extract files from libpst.tar.gz and then write each one in turn to file C:/extract/libpst.tar.gz. This is probably not what you want.

    You may find it useful to run your script under strace (or whatever the equivalent is on Windows) so you can see what the underlying system calls are and what it's trying to do to what files.

      I am doing what is written in the documentation , ie extract archive files one by one to specified location but it is not writing to the specified location

        No, you are not doing what is written in the documentation. DrHyde is correct.

        The documentation shows: $tar->extract_file( 'name/in/archive', 'name/i/want/to/give/it' );

        Your code has this line though: $tar->extract_file($file, "C:/extract/$input"); # Extract $file to "C:/extract/libpst.tar.gz"

        If you change it to the following it might work better: $tar->extract_file($file, "C:/extract"); # Extract $file to "C:/extract"

Re: Archive::Tar extract
by trizen (Hermit) on Apr 17, 2012 at 10:21 UTC
    You can try something like this:
    use strict; use warnings; use Archive::Tar; use File::Path qw(make_path); use File::Basename qw(dirname); use File::Spec::Functions qw(catdir); my $input = "libpst.tar.gz"; my $tar = Archive::Tar->new($input); my @files = $tar->list_files; foreach my $file (@files) { my $content = $tar->get_content($file); next if not defined $content; # is a dir # do something with the $content of the file my $file_name = catdir("path", $file); my $dir_name = dirname($file_name); if (not -d $dir_name) { make_path($dir_name) or die "$0: Can't create path $dir_name: +$!"; } open my $fh, '>', $file_name or die "$0: Can't open file $file: $! +"; print {$fh} $content; close $fh; }

      Thanks Trizen but the problem with your script is that when it uses get_contents it is okay for a normal file but if pdf files or any other special files are involved we cannot just simply open it . I need to extract one file at a time to the specified location perform some matching and then move to the next file which is possible with above script but only on normal text files . So plz help in extracting tem to specified location one by one

        Here we go:
        use strict; use warnings; use Archive::Tar; use File::Spec::Functions qw(catdir); my $input = "libpst.tar.gz"; my $tar = Archive::Tar->new($input); my @files = $tar->list_files; my $output_dir = 'path'; # change this foreach my $file (@files) { my $extracted_file = catdir($output_dir, $file); $tar->extract_file($file, $extracted_file); if ($file =~ /\.txt\z/) { # or whatever open my $fh, '<', $extracted_file or do { warn "$0: Can't open + file $file: $!"; next }; while (defined(my $line = <$fh>)) { # do something with $line } close $fh; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965469]
Approved by marto
[ovedpo15]: http://www. node_id=1211676
[ovedpo15]: can someone help with the following question? thank you monks

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-03-24 18:32 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.