tags:

views:

2083

answers:

5

I want to archive a directory (I don't know whether I can call "I want to tar a directory"). I want to preserve the access permissions at the other end when I un-tar it. I should I tackle this problem in perl.

Thanks for the response but why I'm asking for doing it Perl is I want it independent of the platforms. I want to transfer one big file to multiple machines. Those machines can be of any platform. I should be able to untar this tar file properly right? So I want to write my own tar and untar programs. Why I'm using Perl is to make it platform independent. So I can not use tar command by opening the shell in script and stuff like that. The Archive::Tar module only deals with tarred file but it has no option to archive files.

+6  A: 

You might want to look at Archive::Tar on CPAN. (I am just guessing, I have never used it myself.) Why do you insist on doing it in Perl?

zoul
I want it to be platform independent...
ram
Archive tar is available with ActivePerl.
Mathieu Longtin
+2  A: 

You can use the Archive::Tar Perl module, or you can execute tar directly.

If you run with the option of using tar from the commandline, use the -p flag to preserve permissions.

If you are literally just looking to tar up the directory, I'd just run the command directly, you don't need to use Perl. If you need to do some fancy processing afterwards, maybe you should. It depends.

Phil
+2  A: 

Here is a simple example:

#!/usr/bin/perl -w

use strict;
use warnings 'all';
use Archive::Tar;

# Create a new tar object:
my $tar = Archive::Tar->new();

# Add some files:
$tar->add_files( </path/to/files/*.html> );
# */ fix syntax highlighing in stackoverflow.com

# Finished:
$tar->write( 'file.tar' );

# Now extract:
my $tar = Archive::Tar->new();
$tar->read( 'file.tar' );
$tar->extract();
JDrago
+1  A: 

It sounds to me like rsync might be a better solution for this, but you haven't said much about what other constraints you have.

brian d foy
A: 

Two parameters; the name of the compressed tar file and the name of directory you want in the tar file. eg

tarcvf test.tar.gz mydir
#!/usr/bin/perl -w 

use strict;
use warnings 'all';
use Archive::Tar;
use File::Find;


my $archive=$ARGV[0];
my $dir=$ARGV[1];

if ($#ARGV != 1) {
    print "usage: tarcvf test.tar.gz directory\n";
    exit;
}

# Create inventory of files & directories
my @inventory = ();
find (sub { push @inventory, $File::Find::name }, $dir);

# Create a new tar object
my $tar = Archive::Tar->new();

$tar->add_files( @inventory );

# Write compressed tar file
$tar->write( $archive, 9 );
Colin