File Coverage

blib/lib/Archive/SimpleExtractor/Rar.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Archive::SimpleExtractor::Rar;
2              
3 1     1   2642 use warnings;
  1         1  
  1         28  
4 1     1   4 use strict;
  1         2  
  1         24  
5 1     1   354 use Archive::Rar;
  0            
  0            
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;