Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

How do I modify the directory structure within Archive::Tar and mofiy its "path". For example I have a set of directories that reside in C:\users\temp\DIR1\DIR2\FILE.txt. I want to tar up \DIR1\DIR2\FILE.txt, and not C:\users\temp. How can I go about this? I am using the following code:

use Archive::Tar; $JOE = $ENV{"TEMP"}; my $tar = Archive::Tar->new; #I then define $city, $category and $page_number $tar->add_files("$JOE/$city/$category/$page_number", "$JOE/$city/$cate +gory/filedesc"); $tar->write('c:\files.tar');

When I then go and look in c:\files.tar, the entire temp path along with the directories I want are there. I have tried to rename them like so:

$tar->rename( "$JOE\$city\$category\$page_number", "/$city/$catego +ry/$page_number" ); $tar->rename( "$JOE/$city/$category/filedesc", "/filedesc" );

But that gives me all kinds of path errors. Can't I specify the "path" or the files within Archive::Tar?

Thank you.

Replies are listed 'Best First'.
Re: Archive::Tar and the paths within
by polettix (Vicar) on Aug 26, 2005 at 08:27 UTC
    The docs require you to use Unix paths, that's probably why you're getting these errors.

    When I need to do path trimming in GNU tar, I usually chdir to the wanted base directory before calling tar. I don't usually feel comfortable with absolute paths: if I want to restore in the root directory I just chdir to "/" before untarring, which lefts me the ability to untar to a temporary directory if I need.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Archive::Tar and the paths within
by InfiniteSilence (Curate) on Aug 25, 2005 at 21:31 UTC
    I don't use Archive::Tar but rather the standard GNU Tar utility and shell out to it. Anyway, if you are interested in doing it that way you can put the names of the files you like in a standard text file, like this:
    /DIR1/DIR2/FILE.TXT /DIR1/DIR2/FILE2.TXT
    Save this text file as foo.dat and then call tar like so:
    tar -cvf mynewtar.tar -T foo.dat
    Only relative file paths are stored. You might try tar --help for more cool things you can do with tar. Note, if you are writing a solution that needs to actually use the functionality of the Archive::Tar module in code, then ignore this post, but if you are simply looking for a way to use tar on Windows then you don't really need the module (yet).

    Celebrate Intellectual Diversity

Re: Archive::Tar and the paths within
by idsfa (Vicar) on Aug 26, 2005 at 15:10 UTC

    chdir is your friend

    use Archive::Tar; chdir $ENV{'TEMP'} or die "Unable to chdir to $ENV{TEMP}\n"; my $tar = Archive::Tar->new; #I then define $city, $category and $page_number $tar->add_files("$city/$category/$page_number", "$city/$category/filedesc"); $tar->write('c:\files.tar');

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon