tags:

views:

72

answers:

2

Hi,

I am a newbee in Perl.

I am reading a directory having some archive files and uncompressing the archive files one by one.

Everything seems well however the files are getting uncompressed in the folder which has the main perl code module which is running the sub modules.

I want the archive to be generated in the folder I specify.

This is my code:

sub ExtractFile
{

 #Read the folder which was copied in the output path recursively and extract if any file is compressed
 my $dirpath = $_[0];

 opendir(D, "$dirpath") || die "Can't open dir $dirpath: $!\n";
 my @list = readdir(D);
 closedir(D);


 foreach my $f (@list) 
 {
  print " \$f = $f";
  if(-f $dirpath."/$f")
  {
   #print " File in  directory $dirpath \n ";#is \$f = $f\n";

   my($file_name, $file_dirname,$filetype)= fileparse($f,qr{\..*});

   #print " \nThe file extension is $filetype";
   #print " \nThe file name is is $file_name";


   # If compressed file then extract the file
   if($filetype eq ".tar" or $filetype eq ".tzr.gz")
   {

    my $arch_file = $dirpath."/$f";
    print "\n file to be extracted is $arch_file";
    my $tar = Archive::Tar->new($arch_file);
    #$tar->extract() or die ("Cannot extract file $arch_file");

    #mkdir($dirpath."/$file_name");
    $tar->extract_file($arch_file,$dirpath."/$file_name" ) or die ("Cannot extract file $arch_file");
   }

  }
  if(-d $dirpath."/$f")
  {
   if($f eq "." or $f eq "..")
   { next; }
   print " Directory\n";# is $f";
   ExtractFile($dirpath."/$f");
  }

 }


}

The method ExtractFile is called recursively to loop all the archives. When using $tar->extract() it uncompresses in the folder which calls this metohd.

when I use $tar->extract_file($arch_file,$dirpath."/$file_name" ) I get an error :

No such file in archive: '/home/fsang/dante/workspace/output/s.tar' at /home/fsang/dante/lib/Extraction.pm line 80

Please help I have checked that path and input output there is no issue with it.

Seems some usage problem I am not aware of for $tar->extract_file().

Many thanks for anyone resolving this issue.

Regards, Sakshi

A: 

a typo?

$tar->extract_file($arch_file,$dirpath."/$file_name" ) 

should probably be

$tar->extract_file($arch_file,$dirpath."/".$file_name) 
Axarydax
No, you're the one with the typo. Your "fixed" line has an odd number of quotes in it.
cjm
It still gives the same error message
Sakshi Smriti
thanks @cjm - I forgot the quote at the end of the line - edited
Axarydax
These two lines are exactly equivalent. Perl performs variable interpolation on double quoted strings.
cjm
A: 

You've misunderstood extract_file. The first parameter is the name of a file inside the archive to extract. You're passing in the path of the archive itself. You passed that to new; you don't need to pass it in again. As the error message explains, s.tar does not contain a file named /home/fsang/dante/workspace/output/s.tar, so extract_file fails.

You can get a list of files in the archive by using $tar->list_files.

A simpler solution may be to temporarily chdir to the directory you want to extract the archive into. File::pushd provides an easy way to do that.

cjm
Can you please explain what to pass in the first parameter I have also passed the name of the archive file i.e. s.tar but it still says No such file in archive: contain a file named /home/fsang/dante/workspace/output/s.tarSeems it is searching the file in some collection, do I need to add it in some collection property before calling the extract_file methodThanks for your help
Sakshi Smriti
Hi Thanks .. now I understand it better and found the solution using File::pushd Many thanks
Sakshi Smriti