Reputation: 175
I want to use Perl's Archive::Tar module to backup things. I am tying to use Archive::Tar
to back up files and folders and files in folders. With my current code, the first two work, but the third doesn't.
use strict; use warnings;
use Archive::Tar;
use Tk;
my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;
my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;
$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;
MainLoop;
sub compress {
my $input = $inputEntry -> get;
my @input;
unless ( $input !~ m/ / ) {
@input = split ( m/ /, $input );
} else {
@input = ( $input );
}
Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )
}
Upvotes: 1
Views: 87
Reputation: 130
Your steps to reproduce aren't clear and more examples could be helpful, but your script seems to work fine for me. I was able to add both folders and files, and files within folders successfully to the archive. For each file within a folder, I had to type the entire relative path eg. 'sample/file1.txt'.
You may be looking for a way to add the files within a folder automatically. Something like this would do the trick:
use strict; use warnings;
use Archive::Tar;
use Tk;
my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;
my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;
$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;
MainLoop;
sub compress {
my $input = $inputEntry -> get;
my @input;
my @dirfiles;
unless ( $input !~ m/ / ) {
@input = split ( m/ /, $input );
} else {
@input = ( $input );
}
foreach(@input) { if(-d $_) { push(@dirfiles,glob "'${_}/*'"); } }
push(@input,@dirfiles);
Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )
}
I think there are still remaining issues, such as: This script appears to fail on files/folders with spaces in their names, even when escaped or quoted.
Upvotes: 2