Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Archive::Tar extract

by trizen (Hermit)
on Apr 17, 2012 at 10:21 UTC ( #965476=note: print w/replies, xml ) Need Help??

in reply to Archive::Tar extract

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; }

Replies are listed 'Best First'.
Re^2: Archive::Tar extract
by gautamparimoo (Beadle) on Apr 17, 2012 at 10:39 UTC

    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: note [id://965476]
[karlgoethebier]: Listen and compare
[shmem]: karlgoethebier: es ist theologisch umstritten, ob jesus an christi himmelfahrt tatsächlich besoffen und mit bollerwagen zu seinem vater aufgebrochen ist
[karlgoethebier]: shmem: Ja ischweis
[karlgoethebier]: shmem: Vielleicht hat er in den Bollerwagen gebrochen?
[karlgoethebier]: shmem:Ach so. Bollerwagen ist eigentlich was für Kinder. Bei uns fahren sie mit Traktoren und Feuerwehrwagen rum...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2017-05-25 12:37 GMT
Find Nodes?
    Voting Booth?