in reply to How do i change the file names while untar?

On unix, the location should be /test1/test2.txt, with forward slashes.

If you use Archive::Tar, you can rename any files you like with the rename method.

use Archive::Tar; my $tar = Archive::Tar->new('tarball.tgz', 1) or die $!; $tar->rename($_, changepath($_)) for $tar->list_files; $tar->extract;
where changepath() returns whatever transformed path you need (untested).

File::Spec may be useful in constructing changepath().

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: How do i change the file names while untar?
by BerntB (Deacon) on Oct 02, 2005 at 23:18 UTC
    Wow, that was an elegant api. Thanx for info!

    (I knew about Archive::Tar but have never had a reason to look at it.)

Re^2: How do i change the file names while untar?
by perl_99_monk (Novice) on Sep 28, 2005 at 21:22 UTC
    Thanks a lot Zaxo, I tried to rename but I am getting errors: my $tar = Archive::Tar->read('test.tar', 1) or die $!; $tar->rename($_, '/export/home/test/renametest') for $tar->list_files; print ("$_"); I am getting the errors: Can't call method "list_files" without a package or object reference. and similar message for rename...Any suggestions?

      Try omitting the compression flag since you are dealing with an uncompressed tarball, and call new as the constructor,

      my $tar = Archive::Tar->new('test.tar') or die $!;

      That will make $tar an Archive::Tar object whose methods will be found.

      After Compline,
      Zaxo

        Zaxo, I tried it and the split path does not work either on unix .

          my $tar = Archive::Tar->new('deploy.tar', 1) or die $!;
         $tar->rename($_, changepath($_)) for $tar->list_files; $tar->extract;
          sub changepath { chomp; my $path = $_;
          my ($volume, $directories, $file) = File::Spec->splitpath($path);
          my $dir = File::Spec->catpath($directories, '');
        $_=$dir; }
        THe same code works fine on win 32. I am getting files from windows into unix and then trying to do these operations. Do you think it would be better to rename while tarring itself on windows box? Is that possible? Thanks
        Zaxo, I tried it and the split path does not work either on unix . my $tar = Archive::Tar->new('deploy.tar', 1) or die $!; $tar->rename($_, changepath($_)) for $tar->list_files; $tar->extract; sub changepath { chomp; my $path = $_; my ($volume, $directories, $file) = File::Spec->splitpath($path); my $dir = File::Spec->catpath($directories, ''); $_=$dir; } THe same code works fine on win 32. I am getting files from windows into unix and then trying to do these operations. Do you think it would be better to rename while tarring itself on windows box? Is that possible? Thanks