| 1 | package App::Zip::X; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Getopt::Long qw(GetOptionsFromArray); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Archive::Zip qw(AZ_OK); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # constant integers to specify indentation modes -- see L<XML::LibXML> | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use constant XML_NO_INDENT => 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use constant XML_SIMPLE_INDENT => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub run { # no new() method -- this method both creates the instance and executes the request | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=13 SCAR=33.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | GetOptionsFromArray \@args, $self, | |||||||||||||||||||||||||||||||||||||||||
| 24 | 'unzip!', # unzip mode (default) | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 'zip!', # zip mode | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 'xml_indent!', # handle indentation of XML extracted files (default) | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 'archive=s', # archive name (or first arg on command line) | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 'member=s', # member to extract (or second arg on command line) | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # other syntax : archive name and member name from command line without options | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | $self->{archive} //= shift @args or die "unspecified ZIP archive"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"archive"} //= shift @args
MC/DC0%0/2
$self->{"archive"} //= shift @args
$self->{"archive"}
shift @args
Truth table33%1/3
A //= B
A= $self->{"archive"}B= shift @args
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $self->{member} //= shift @args or die "unspecified member to extract from $self->{zip}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"member"} //= shift @args
MC/DC0%0/2
$self->{"member"} //= shift @args
$self->{"member"}
shift @args
Truth table33%1/3
A //= B
A= $self->{"member"}B= shift @args
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | !@args or die "don't undestand these args: ", join(", ", @args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # default flags and consistency check | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | !($self->{zip} && $self->{unzip}) or die "options -zip and -unzip are mutually exclusive"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"zip"} and $self->{"unzip"}
MC/DC0%0/2
$self->{"zip"} and $self->{"unzip"}
$self->{"zip"}
$self->{"unzip"}
Truth table33%1/3
A and B
A= $self->{"zip"}B= $self->{"unzip"}
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ▶ | $self->{unzip} //= 1 unless $self->{zip}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"unzip"} //= 1
MC/DC0%0/1
$self->{"unzip"} //= 1
$self->{"unzip"}
Truth table50%1/2
A
A= $self->{"unzip"}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # open ZIP archive | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | $self->{zipper} = Archive::Zip->new; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | $self->{zipper}->read($self->{archive}) == AZ_OK | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | or die "cannot open ZIP archive $self->{archive}"; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # decide what to do | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | if ($self->{unzip}) { $self->extract() } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | elsif ($self->{zip}) { $self->replace() } | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | else { die "neither -zip nor -unzip .. not clear what you want to do"} | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub extract { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extract: called CC=5 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # get member contents | ||||||||||||||||||||||||||||||||||||||||||
| 59 | my $contents = $self->{zipper}->contents($self->{member}) | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | or die "no member named '$self->{member}' in $self->{archive}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # add XML indentation if necessary | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | if ($self->{xml_indent} && ($self->{member} =~ /\.xml$/i || $contents =~ /^<\?xml/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
$self->{"member"} =~ /\.xml$/i or $contents =~ /^<\?xml/
MC/DC0%0/3
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
$self->{"xml_indent"}
$self->{"member"} =~ /\.xml$/i
$contents =~ /^<\?xml/
Truth table25%1/4
A and B or C
A= $self->{"xml_indent"}B= $self->{"member"} =~ /\.xml$/iC= $contents =~ /^<\?xml/
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | my $dom = XML::LibXML->load_xml(string => $contents, recover => 1); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $contents = $dom->toString(XML_SIMPLE_INDENT); # already utf8-encoded | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # write on STDOUT | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | binmode STDOUT, ':raw'; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | print $contents; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub replace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # slurp contents from file relative to current directory | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | open my $fh, "<:raw", $self->{member} or die "open $self->{member}: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $contents = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | # remove XML indentation if necessary | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($self->{xml_indent} && ($self->{member} =~ /\.xml$/i || $contents =~ /^<\?xml/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
$self->{"member"} =~ /\.xml$/i or $contents =~ /^<\?xml/
MC/DC0%0/3
$self->{"xml_indent"} and $self->{"member"} =~ /\.xml$/i || $contents =~ /^<\?xml/
$self->{"xml_indent"}
$self->{"member"} =~ /\.xml$/i
$contents =~ /^<\?xml/
Truth table0%0/4
A and B or C
A= $self->{"xml_indent"}B= $self->{"member"} =~ /\.xml$/iC= $contents =~ /^<\?xml/
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $dom = XML::LibXML->load_xml(string => $contents); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $contents = $dom->toString(XML_NO_INDENT); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # replace member in archive and save | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $zipper = $self->{zipper}; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $zipper->removeMember($self->{member}); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $zipper->addString($contents, $self->{member}); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $zipper->overwrite; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | 1; # End of App::Zip::X | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||||||||||||||||