File Coverage

blib/lib/Path/Tiny/Archive/Tar.pm
Criterion Covered Total %
statement 46 49 93.8
branch 12 20 60.0
condition n/a
subroutine 10 12 83.3
pod 4 4 100.0
total 72 85 84.7


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   169307 use strict;
  2         26  
  2         69  
6 2     2   14 use warnings;
  2         4  
  2         68  
7              
8 2     2   1525 use Archive::Tar ();
  2         201041  
  2         63  
9 2     2   20 use Compress::Raw::Zlib ();
  2         5  
  2         40  
10 2     2   12 use Exporter qw( import );
  2         5  
  2         73  
11 2     2   12 use Path::Tiny qw( path );
  2         6  
  2         220  
12              
13              
14             our $VERSION = '0.003';
15              
16             our %EXPORT_TAGS = ( const =>[qw(
17             COMPRESSION_NONE
18             COMPRESSION_GZIP
19             COMPRESSION_GZIP_DEFAULT
20             COMPRESSION_GZIP_NONE
21             COMPRESSION_GZIP_FASTEST
22             COMPRESSION_GZIP_BEST
23             COMPRESSION_BZIP2
24             )] );
25             our @EXPORT_OK = @{ $EXPORT_TAGS{const} };
26              
27              
28             BEGIN {
29 2     2   152 push(@Path::Tiny::ISA, __PACKAGE__);
30             }
31              
32             use constant {
33 2         21 COMPRESSION_NONE => undef,
34             # Hack for Archive::Tar: true so don't use default compression and pass
35             # this level to IO::Zlib. And hack for Compress::Zlib: not digit so use
36             # default compression (letters may specify other compression modes).
37             COMPRESSION_GZIP => '-',
38             COMPRESSION_GZIP_DEFAULT => '-',
39             # Hack for Archive::Tar: zero but true so pass 0 level to IO::Zlib.
40             COMPRESSION_GZIP_NONE => '00',
41             COMPRESSION_GZIP_FASTEST => Compress::Raw::Zlib::Z_BEST_SPEED,
42             COMPRESSION_GZIP_BEST => Compress::Raw::Zlib::Z_BEST_COMPRESSION,
43             COMPRESSION_BZIP2 => Archive::Tar::COMPRESS_BZIP,
44 2     2   16 };
  2         5  
45              
46              
47             sub tar {
48 4     4 1 26702 my ($self, $dest, $level) = @_;
49              
50 4         14 $dest = path($dest);
51              
52 4         80 my $tar = Archive::Tar->new;
53              
54 4 100       60 if ($self->is_file) {
    50          
55 3         115 my $file = Archive::Tar::File->new(file => $self->stringify());
56              
57 3 50       1512 return unless $file;
58              
59 3         14 $file->name($self->basename);
60 3         108 $file->prefix('');
61              
62 3 50       34 $tar->add_files($file) or return;
63             }
64             elsif ($self->is_dir) {
65 1         46 my $paths = $self->iterator({ recurse => 1 });
66              
67 1         42 while (my $path = $paths->()) {
68 2         329 my $file = Archive::Tar::File->new(file => $path->stringify());
69              
70 2 50       711 return unless $file;
71              
72 2         11 $file->rename($path->relative($self));
73              
74 2 50       650 $tar->add_files($file) or return;
75             }
76             }
77             else {
78 0         0 return;
79             }
80              
81 4 100       408 $tar->write($dest->stringify(), defined $level ? $level : ()) or return;
    50          
82              
83 4         6615 return $dest;
84             }
85              
86              
87             sub tgz {
88 0     0 1 0 return $_[0]->tar($_[1], COMPRESSION_GZIP);
89             }
90              
91              
92             sub tbz2 {
93 0     0 1 0 return $_[0]->tar($_[1], COMPRESSION_BZIP2);
94             }
95              
96              
97             sub untar {
98 2     2 1 14427 my ($self, $dest) = @_;
99              
100 2         8 $dest = path($dest);
101              
102 2         93 my $files = Archive::Tar->iter($self->stringify());
103              
104 2 50       3970 return unless $files;
105              
106 2         8 while (my $file = $files->()) {
107 3 50       4598 $file->extract($dest->child($file->full_path)->stringify()) or return;
108             }
109              
110 2         2617 return $dest;
111             }
112              
113              
114             1;
115              
116             __END__