File Coverage

blib/lib/Path/Tiny/Archive/Zip.pm
Criterion Covered Total %
statement 29 34 85.2
branch 8 14 57.1
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 46 57 80.7


line stmt bran cond sub pod time code
1             package Path::Tiny::Archive::Zip;
2              
3             # ABSTRACT: Zip/unzip add-on for file path utility
4              
5 1     1   76963 use strict;
  1         14  
  1         31  
6 1     1   6 use warnings;
  1         1  
  1         36  
7              
8 1     1   642 use Archive::Zip qw( :ERROR_CODES );
  1         79401  
  1         98  
9             #use Carp qw( croak );
10 1     1   9 use Path::Tiny qw( path );
  1         2  
  1         62  
11              
12              
13             our $VERSION = '0.002';
14              
15              
16             BEGIN {
17 1     1   221 push(@Path::Tiny::ISA, __PACKAGE__);
18             }
19              
20              
21             sub zip {
22 2     2 1 2226 my ($self, $dest) = @_;
23              
24 2         14 my $zip = Archive::Zip->new;
25              
26 2 100       91 if ($self->is_file) {
    50          
27 1         25 $zip->addFile($self->realpath->stringify(), $self->basename);
28             }
29             elsif ($self->is_dir) {
30 1         49 $zip->addTree($self->realpath->stringify(), '');
31             }
32              
33 2         6624 $dest = path($dest);
34              
35 2 50       76 unless ($zip->writeToFileNamed($dest->realpath->stringify()) == AZ_OK) {
36 0         0 return;
37             }
38              
39 2         6971 return $dest;
40             }
41              
42              
43             sub unzip {
44 2     2 1 13601 my ($self, $dest) = @_;
45              
46 2         24 my $zip = Archive::Zip->new();
47              
48 2 50       92 unless ($zip->read($self->realpath->stringify()) == AZ_OK) {
49 0         0 return;
50             }
51              
52 2         3140 $dest = path($dest);
53 2 50       118 if ($dest->is_file) {
54 0         0 return;
55             }
56 2 50       54 unless ($dest->is_dir) {
57 0         0 $dest->mkpath();
58             }
59              
60 2 50       32 unless ($zip->extractTree(undef, $dest->realpath->stringify()) == AZ_OK) {
61 0         0 return;
62             }
63              
64 2         5351 return $dest;
65             }
66              
67              
68             1;
69              
70             __END__