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