File Coverage

blib/lib/Net/SFTP/Foreign/Tempdir/Extract/File.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


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