| 1 | package Archive::SimpleExtractor::Rar; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Archive::Rar; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use File::Copy; | ||
| 7 | use File::Path qw/rmtree/; | ||
| 8 | use Cwd 'abs_path'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version 0.02 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | our $VERSION = '0.02'; | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | =head1 METHODS | ||
| 25 | |||
| 26 | =head2 new | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | sub extract { | ||
| 31 | my $self = shift; | ||
| 32 | my %arguments = @_; | ||
| 33 | my ($archive_file) = $arguments{archive} =~ /\/?([^\/]+)$/; | ||
| 34 | my $old_dir = abs_path($0); | ||
| 35 | my $old_cwd = $0; | ||
| 36 | if ($old_cwd eq $old_dir) { | ||
| 37 | $old_dir =~ s/[^\/]+$//; | ||
| 38 | } else { | ||
| 39 | $old_cwd =~ s/^\.\///; | ||
| 40 | $old_dir =~ s/$old_cwd$//; | ||
| 41 | } | ||
| 42 | $arguments{dir} = abs_path($arguments{dir}).'/'; | ||
| 43 | copy($arguments{archive}, $arguments{dir}.$archive_file); | ||
| 44 | chdir $arguments{dir}; | ||
| 45 | my $rar = Archive::Rar->new( -archive => $archive_file ); | ||
| 46 | if ( $rar->List() ) { | ||
| 47 | unlink $archive_file; | ||
| 48 | chdir $old_dir; | ||
| 49 | return (0, 'Can not read archive file '.$arguments{archive}) | ||
| 50 | } | ||
| 51 | $rar->Extract(-quiet => 1); | ||
| 52 | if ($arguments{tree}) { | ||
| 53 | unlink $archive_file; | ||
| 54 | chdir $old_dir; | ||
| 55 | return (1, 'Extract finished with directory tree'); | ||
| 56 | } else { | ||
| 57 | if (ref $rar->{list} && ref $rar->{list} eq 'ARRAY') { | ||
| 58 | foreach my $file (@{$rar->{list}}) { | ||
| 59 | next unless $file->{name} =~ /\//; | ||
| 60 | my ($filename) = $file->{name} =~ /\/([^\/]+)$/; | ||
| 61 | copy($file->{name}, $filename); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | foreach my $item (<*>) {if (-d $item) {rmtree($item)}} | ||
| 65 | unlink $archive_file; | ||
| 66 | chdir $old_dir; | ||
| 67 | return (1, 'Extract finished without directory tree'); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||