File Coverage

blib/lib/Path/Tiny/Archive/Tar.pm
Criterion Covered Total %
statement 49 50 98.0
branch 12 20 60.0
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 74 83 89.1


line stmt bran cond sub pod time code
1             package Path::Tiny::Archive::Tar;
2              
3             # ABSTRACT: Tar/untar add-on for file path utility
4              
5 2     2   141581 use strict;
  2         20  
  2         57  
6 2     2   10 use warnings;
  2         4  
  2         65  
7              
8 2     2   1265 use Archive::Tar qw( COMPRESS_GZIP COMPRESS_BZIP );
  2         165067  
  2         153  
9 2     2   21 use Compress::Raw::Zlib qw( :level );
  2         3  
  2         273  
10 2     2   13 use Path::Tiny qw( path );
  2         4  
  2         92  
11              
12 2     2   1051 use namespace::clean;
  2         28009  
  2         13  
13              
14 2     2   471 use Exporter qw( import );
  2         5  
  2         143  
15              
16              
17             our $VERSION = '0.001';
18              
19             our %EXPORT_TAGS = ( const =>[qw(
20             COMPRESSION_NONE
21             COMPRESSION_GZIP
22             COMPRESSION_GZIP_DEFAULT
23             COMPRESSION_GZIP_NONE
24             COMPRESSION_GZIP_FASTEST
25             COMPRESSION_GZIP_BEST
26             COMPRESSION_BZIP2
27             )] );
28             our @EXPORT_OK = @{ $EXPORT_TAGS{const} };
29              
30              
31             BEGIN {
32 2     2   103 push(@Path::Tiny::ISA, __PACKAGE__);
33             }
34              
35             use constant {
36 2         9 COMPRESSION_NONE => undef,
37             COMPRESSION_GZIP => COMPRESS_GZIP,
38             COMPRESSION_GZIP_DEFAULT => Z_DEFAULT_COMPRESSION,
39             COMPRESSION_GZIP_NONE => Z_NO_COMPRESSION,
40             COMPRESSION_GZIP_FASTEST => Z_BEST_SPEED,
41             COMPRESSION_GZIP_BEST => Z_BEST_COMPRESSION,
42             COMPRESSION_BZIP2 => COMPRESS_BZIP,
43 2     2   10 };
  2         4  
44              
45              
46             sub tar {
47 4     4 1 21319 my ($self, $dest, $level) = @_;
48              
49 4         13 $dest = path($dest);
50              
51 4         65 my $tar = Archive::Tar->new;
52              
53 4 100       49 if ($self->is_file) {
    50          
54 3         69 my $file = Archive::Tar::File->new(file => $self->stringify());
55              
56 3 50       1308 return unless $file;
57              
58 3         14 $file->name($self->basename);
59 3         104 $file->prefix('');
60              
61 3 50       31 $tar->add_files($file) or return;
62             }
63             elsif ($self->is_dir) {
64 1         37 my $paths = $self->iterator({ recurse => 1 });
65              
66 1         30 while (my $path = $paths->()) {
67 2         256 my $file = Archive::Tar::File->new(file => $path->stringify());
68              
69 2 50       546 return unless $file;
70              
71 2         10 $file->rename($path->relative($self));
72              
73 2 50       565 $tar->add_files($file) or return;
74             }
75             }
76             else {
77 0         0 return;
78             }
79              
80 4 100       276 $tar->write($dest->stringify(), defined $level ? $level : ()) or return;
    50          
81              
82 4         5551 return $dest;
83             }
84              
85              
86             sub untar {
87 2     2 1 11545 my ($self, $dest) = @_;
88              
89 2         6 $dest = path($dest);
90              
91 2         90 my $files = Archive::Tar->iter($self->stringify());
92              
93 2 50       4114 return unless $files;
94              
95 2         7 while (my $file = $files->()) {
96 3 50       3317 $file->extract($dest->child($file->full_path)->stringify()) or return;
97             }
98              
99 2         2243 return $dest;
100             }
101              
102              
103             1;
104              
105             __END__