Search code examples
perlperl-module

How to read a file which is gzipped and tar in perl


I have placed the text file "FilenameKeyword.txt" file in E:/Test folder, in my perl script i am trying to traverse through the folder and am i am trying to find a file with filename which has the string "Keyword" in it, later i have printed the content of that file in my script. Now i wish do the same thing for the file which is placed inside tar file which is compressed.

Hypothetical File from where i am trying to extract the details: E:\test.tar.gz

Wanted to know if there are possibility in perl to search and read the file without decompressing /unzipping the hypothetical file.If that is not possible, I shall also allocate some temperory memory to decompress the file , which should deleted after extracting the content from the particular text file.

While Searching in the internet i could it is possible to extract and read the gzip/tar file by using Archive::Extract, being new to Perl - i am really confused on how actually i should make use of it. Could you please help on this....

Input file:FilenameKeyword.txt

Script:

use warnings;
use strict;

my @dirs = ("E:\\Test\\");
my %seen;
while (my $pwd = shift @dirs) {
        opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
        my @files = readdir(DIR);
        closedir(DIR);
        foreach my $file (@files) 
        {
                if (-d $file and ($file !~ /^\.\.?$/) and !$seen{$file}) 
                {
                        $seen{$file} = 1;
                        push @dirs, "$pwd/$file";
                }
                next if ($file !~ /Keyword/i);
                my $mtime = (stat("$pwd/$file"))[9];
                print "$pwd$file";
                print "\n";
                open (MYFILE, "$pwd$file");
                while (my $line = <MYFILE>){
                #print $line;
                my ($date) = split(/,/,$line,2);
                if ($line =~ s!<messageText>(.+?)</messageText>!!is){
                print "$1";
                }
                }

        }
}

Output(In test program file is placed under E:\Test):

E:\Test\FilenameKeyword.txt
1311 messages Picked from the Queue.

Looking for help to retrieve the content of the file which is place under E:\test.tar.gz

Desired Output:

E:\test.tar.gz\FilenameKeyword.txt
1311 messages Picked from the Queue. 

Solution

  • I was stuck in using CPAN module, CPAN module didn't work for me as i have oracle 10g enterprise edition in the same machine, due do some software conflict Active state perl was unable compile and refer to the perl lib for CPAN module, i have uninstalled oracle in my machine to make this work....

    #!/usr/local/bin/perl
    use Archive::Tar;
    my $tar = Archive::Tar->new;
    $tar->read("test.tar.gz");
    $tar->extract();