File Coverage

blib/lib/Archive/SimpleExtractor.pm
Criterion Covered Total %
statement 28 36 77.7
branch 4 12 33.3
condition 3 12 25.0
subroutine 7 8 87.5
pod 3 3 100.0
total 45 71 63.3


line stmt bran cond sub pod time code
1             package Archive::SimpleExtractor;
2              
3 1     1   23681 use warnings;
  1         3  
  1         37  
4 1     1   5 use strict;
  1         2  
  1         36  
5 1     1   1832 use Module::Load;
  1         1128  
  1         6  
6 1     1   917 use Module::Load::Conditional qw/can_load/;
  1         33512  
  1         365  
7             $Module::Load::Conditional::VERBOSE = 1;
8              
9             =head1 NAME
10              
11             Archive::SimpleExtractor - simple module for extract archives
12              
13             =head1 VERSION
14              
15             Version 0.10
16              
17             =cut
18              
19             our $VERSION = '0.11';
20              
21              
22             =head1 SYNOPSIS
23              
24             use Archive::SimpleExtractor;
25              
26             my $extractor = new Archive::SimpleExtractor;
27            
28             @res = $extractor->extract(archive => 'archive.tar.bz2', dir => './somedir', tree => 1);
29              
30             =cut
31              
32             our $extentions = {
33             'zip' => 'Archive::SimpleExtractor::Zip',
34             'rar' => 'Archive::SimpleExtractor::Rar',
35             'tar' => 'Archive::SimpleExtractor::Tar',
36             'tgz' => 'Archive::SimpleExtractor::Tar',
37             'tar.gz' => 'Archive::SimpleExtractor::Tar',
38             'tar.bz2' => 'Archive::SimpleExtractor::Tar',
39             };
40              
41             =head1 METHODS
42              
43             =head2 new
44              
45             Returns a new object
46              
47             =cut
48              
49             sub new {
50 1     1 1 3454 my $self = shift;
51 1         5 $self = bless {}, $self;
52 1         3 return $self;
53             }
54              
55             =head2 extract
56              
57             @res = $extractor->extract(archive => $archive_file, dir => $destination_dir, [tree => 1]);
58              
59             extract files from archive
60              
61             Atributes HASH:
62              
63              
64              
65             archive => ...
66              
67             path to you archive file
68              
69              
70              
71             dir => ...
72              
73             path will be unpacked archive
74              
75              
76              
77             tree => 1
78              
79             By default extractor unpacked arhive without subdirectories, but all files have been extracted in source dir. If you want save stucture set this argument.
80              
81              
82             use_extractor => [file extention]
83              
84             if archive file have not extentions, your must set this parameter for right select extractor
85              
86              
87             Returns ARRAY:
88              
89             $res[0] - success status. If 1 then OK else error
90              
91             $res[1] - message or error string
92              
93             =cut
94              
95             sub extract {
96 0     0 1 0 my $self = shift;
97 0         0 my %arguments = @_;
98 0 0 0     0 return (0, 'Bad atributes') unless $arguments{archive} || $arguments{dir};
99 0 0       0 return (0, 'No source directory') unless -d $arguments{dir};
100 0         0 my ($res, $extractor) = $self->have_extractor(%arguments);
101 0 0       0 return (0, $extractor) unless $res;
102 0         0 my @result = $extractor->extract(%arguments);
103 0         0 return @result;
104             }
105              
106             =head2 have_extractor
107              
108             Check extractor
109              
110             =cut
111              
112             sub have_extractor {
113 3     3 1 352 my $self = shift;
114 3         8 my %arguments = @_;
115 3         7 my $reg_exp = join('|', keys %{$extentions});
  3         19  
116 3         52 $reg_exp = qr/$reg_exp/;
117 1     1   11 no warnings;
  1         4  
  1         205  
118 3   33     72 my ($ext) = $arguments{use_extractor} || $arguments{archive} =~ /\.($reg_exp)$/;
119 3 50       10 return (0, 'No Extractor') unless $ext;
120 3 50 33     12 return (0, 'No Extractor') unless $extentions->{$ext} || {reverse %$extentions}->{$ext};
121 3   33     10 my $extractor = $extentions->{$ext} || $ext;
122 3 100       19 return can_load(modules => {$extractor => 0.0}) ? (1, $extractor) : (0, 'Bad Extractor');
123             }
124              
125             =head1 AUTHOR
126              
127             Sergey Tomoulevitch, C<< >>
128              
129             =head1 BUGS
130              
131             Please report any bugs or feature requests to C, or through
132             the web interface at L. I will be notified, and then you'll
133             automatically be notified of progress on your bug as I make changes.
134              
135              
136              
137              
138             =head1 SUPPORT
139              
140             You can find documentation for this module with the perldoc command.
141              
142             perldoc Archive::SimpleExtractor
143              
144              
145             You can also look for information at:
146              
147             =over 4
148              
149             =item * RT: CPAN's request tracker
150              
151             L
152              
153             =item * AnnoCPAN: Annotated CPAN documentation
154              
155             L
156              
157             =item * CPAN Ratings
158              
159             L
160              
161             =item * Search CPAN
162              
163             L
164              
165             =back
166              
167              
168             =head1 COPYRIGHT & LICENSE
169              
170             Copyright 2009 Sergey Tomoulevitch.
171              
172             This program is free software; you can redistribute it and/or modify it
173             under the terms of either: the GNU General Public License as published
174             by the Free Software Foundation; or the Artistic License.
175              
176             See http://dev.perl.org/licenses/ for more information.
177              
178              
179             =cut
180              
181             1; # End of Archive::SimpleExtractor