File Coverage

blib/lib/Net/SFTP/Foreign/Tempdir/Extract/File.pm
Criterion Covered Total %
statement 18 34 52.9
branch 0 12 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 25 54 46.3


line stmt bran cond sub pod time code
1             package Net::SFTP::Foreign::Tempdir::Extract::File;
2 7     7   53 use strict;
  7         19  
  7         212  
3 7     7   40 use warnings;
  7         16  
  7         180  
4 7     7   37 use File::Tempdir qw{};
  7         18  
  7         143  
5 7     7   2119 use Path::Class::File 0.34 qw{}; #move_to capability
  7         66869  
  7         176  
6 7     7   4306 use Archive::Extract qw{};
  7         1082464  
  7         284  
7 7     7   66 use base qw{Path::Class::File};
  7         18  
  7         3030  
8              
9             our $VERSION = '0.14';
10              
11             =head1 NAME
12              
13             Net::SFTP::Foreign::Tempdir::Extract::File - Path::Class::File with an extract method
14              
15             =head1 SYNOPSIS
16              
17             use Net::SFTP::Foreign::Tempdir::Extract;
18             my $sftp = Net::SFTP::Foreign::Tempdir::Extract->new(user=>$user, match=>qr/\.zip\Z/);
19             my $file = $sftp->next; # isa Net::SFTP::Foreign::Tempdir::Extract::File
20              
21             =head1 DESCRIPTION
22              
23             Net::SFTP::Foreign::Tempdir::Extract::File is a convince wrapper around L, L and L
24              
25             =head1 USAGE
26              
27             my $archive = Net::SFTP::Foreign::Tempdir::Extract::File->new( $path, $filename );
28             my @files = $archive->extract; #array of Net::SFTP::Foreign::Tempdir::Extract::File files
29              
30             =head2 extract
31              
32             Extracts tar.gz and Zip files to temporary directory (any format supported by L)
33              
34             my @files = $archive->extract; #list of Net::SFTP::Foreign::Tempdir::Extract::File files
35             my $files = $archive->extract; #array reference of Net::SFTP::Foreign::Tempdir::Extract::File files
36              
37             Note: These files are temporary and will be cleaned up when the file object variable goes out of scope.
38              
39             =cut
40              
41             sub extract {
42 0     0 1   my $self = shift;
43 0           my $ae = Archive::Extract->new(archive=>"$self");
44 0           my $ae_dir = File::Tempdir->new;
45 0 0         $ae->extract(to => $ae_dir->name) or die $ae->error; #extracts all files to a temp dir
46 0           my @files = ();
47 0           my $filenames = $ae->files; #array reference in scalar context
48             #loop through each file, bless and move to individual temp folders
49 0           foreach my $filename (@$filenames) {
50 0           my $tmpdir = File::Tempdir->new; #separate tmp directory for each file for fine grained cleanup
51 0 0         die(sprintf(qq{Error: Dir "%s" is not a directory}, $tmpdir->name)) unless -d $tmpdir->name;
52 0           my $file = $self->new($ae_dir->name => $filename); #isa Path::Class::File object
53 0 0         die(sprintf(qq{Error: File "%s" is not readable.}, $file)) unless -r $file;
54 0 0         $file->move_to(Path::Class::File->new($tmpdir->name, $filename)) or die("Error: Failed to move file to temp directory");
55 0           $file->{"__tmpdir"} = $tmpdir; #needed for scope clean up of File::Tempdir object
56 0 0         die(sprintf(qq{Error: File "%s" is not readable.}, $file)) unless -r $file;
57 0           push @files, $file;
58             }
59 0 0         return wantarray ? @files : \@files;
60             }
61              
62             #head2 __tmpdir
63             #
64             #property to keep the tmp directory in scope for the life of the file object
65             #
66             #cut
67              
68             =head1 TODO
69              
70             Support other archive formats besides zip
71              
72             =head1 BUGS
73              
74             Send email to author and log on RT.
75              
76             =head1 SUPPORT
77              
78             DavisNetworks.com supports all Perl applications including this package.
79              
80             =head1 AUTHOR
81              
82             Michael R. Davis
83             CPAN ID: MRDVT
84             Satellite Tracking of People, LLC
85             mdavis@stopllc.com
86             http://www.stopllc.com/
87              
88             =head1 COPYRIGHT
89              
90             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
91              
92             The full text of the license can be found in the LICENSE file included with this module.
93              
94             =head1 SEE ALSO
95              
96             L, L, L
97              
98             =cut
99              
100             1;