| 1 | package WWW::SFDC::Zip; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Utilities for manipulating base64 encoded zip files. | ||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use 5.12.0; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.36'; # TRIAL VERSION | |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Path qw(mkpath); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use File::Spec::Functions qw(splitpath); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use IO::Compress::Zip qw{$ZipError zip :constants}; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use IO::Uncompress::Unzip qw($UnzipError); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Log::Log4perl ':easy'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||
| 23 | 1 | our @EXPORT_OK = qw(unzip makezip); | |||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | sub unzip { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
unzip: called CC=13 SCAR=26.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 28 | # you need to understand IO::Uncompress::unzip | ||||||||||||||||||||||||||||||||||||||
| 29 | # for this function | ||||||||||||||||||||||||||||||||||||||
| 30 | 4 | my ($dest, $data, $callback) = @_; | |||||||||||||||||||||||||||||||||||||
| 31 | 4 | INFO "Unzipping files to $dest"; | |||||||||||||||||||||||||||||||||||||
| 32 | 4 | TRACE "Data to unzip" => $data; | |||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | LOGDIE "No destination!" unless $dest; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | # Ensure $dest ends with a / | ||||||||||||||||||||||||||||||||||||||
| 36 | 4 | $dest =~ s{(?<![/\\])$}{/}; | |||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | 4 | $data = decode_base64 $data; | |||||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | my $unzipper = IO::Uncompress::Unzip->new(\$data) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 40 | or LOGDIE "Couldn't unzip data"; | ||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 4 | my $status; | |||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | for ($status = 1; $status > 0; $status = $unzipper->nextStream()) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 45 | 12 | my $header = $unzipper->getHeaderInfo(); | |||||||||||||||||||||||||||||||||||||
| 46 | 12 | my (undef, $folder, $name) = splitpath($header->{Name}); | |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | 12 | $folder =~ s{unpackaged/}{}; | |||||||||||||||||||||||||||||||||||||
| 49 | 12 | $folder = $dest.$folder; | |||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | # create folder on disk unless it exists already | ||||||||||||||||||||||||||||||||||||||
| 52 | 12 | ▶ | mkpath($folder) or LOGDIE "Couldn't mkdir $folder: $!" unless -d $folder; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
-d $folder || (mkpath($folder) || LOGDIE("Couldn't mkdir ${folder}: $!"))
mkpath($folder) or LOGDIE("Couldn't mkdir ${folder}: $!")
MC/DC0%0/3
-d $folder || (mkpath($folder) || LOGDIE("Couldn't mkdir ${folder}: $!"))
-d $folder
mkpath($folder)
LOGDIE("Couldn't mkdir ${folder}: $!")
Truth table0%0/4
A || B or C
A= -d $folderB= mkpath($folder)C= LOGDIE("Couldn't mkdir ${folder}: $!")
| |||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | # skip if the file is a folder, exit on error | ||||||||||||||||||||||||||||||||||||||
| 55 | 12 | ▶ | $status < 0 ? last : next if $name =~ /\/$/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | # read content into memory | ||||||||||||||||||||||||||||||||||||||
| 58 | 12 | my $buffer; | |||||||||||||||||||||||||||||||||||||
| 59 | my $content; | ||||||||||||||||||||||||||||||||||||||
| 60 | 12 | ▶ | $content .= $buffer while ($status = $unzipper->read($buffer)) > 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 61 | 12 | my $path = "$folder/$name"; | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | # use callback, if defined | ||||||||||||||||||||||||||||||||||||||
| 64 | 12 | ▶ | $content = $callback->($path, $content) if $callback; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | 12 | ▶ | if ($content) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 67 | # open target for writing | ||||||||||||||||||||||||||||||||||||||
| 68 | 11 | ▶ | my $fh = IO::File->new($path, "w") or LOGDIE "Couldn't write to $path: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 69 | 11 | $fh->binmode(); | |||||||||||||||||||||||||||||||||||||
| 70 | 11 | $fh->write($content); | |||||||||||||||||||||||||||||||||||||
| 71 | 11 | $fh->close(); | |||||||||||||||||||||||||||||||||||||
| 72 | # update time on target | ||||||||||||||||||||||||||||||||||||||
| 73 | 11 | my $stored_time = $header->{'Time'}; | |||||||||||||||||||||||||||||||||||||
| 74 | 11 | ▶ | utime ($stored_time, $stored_time, $path) or LOGDIE "Couldn't touch $path: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||
| 76 | }; | ||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||
| 78 | 4 | return "Success"; | |||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | sub makezip { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
makezip: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 83 | 1 | my ($baseDir, @files) = @_; | |||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | 1 | TRACE "File list before grep: " . Dumper \@files; | |||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | LOGDIE "It is invalid to call makezip with no files." unless scalar @files; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||
| 88 | 1 | $baseDir =~ s{(?<![/\\])$}{/}; | |||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | 1 | chomp @files; | |||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | @files = grep {-e $_ && !-d $_} | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
-e $_ && !(-d $_)
MC/DC0%0/2
-e $_ && !(-d $_)
-e $_
!(-d $_)
Truth table33%1/3
A && not(B)
A= -e $_B= !(-d $_)
| |||||||||||||||||||||||||||||||||||||||
| 93 | 1 | map {s/^(?!$baseDir)/$baseDir/r} | |||||||||||||||||||||||||||||||||||||
| 94 | @files; | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | 1 | DEBUG "File list for zipping: " . Dumper \@files; | |||||||||||||||||||||||||||||||||||||
| 97 | 1 | INFO "Writing zip file with ". scalar(@files) ." files"; | |||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my $result; | |||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | zip | ||||||||||||||||||||||||||||||||||||||
| 102 | \@files => \$result, | ||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | FilterName => sub { s/$baseDir// if $baseDir; }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | Level => 9, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 105 | Minimal => 1, | ||||||||||||||||||||||||||||||||||||||
| 106 | BinModeIn => 1, | ||||||||||||||||||||||||||||||||||||||
| 107 | or LOGDIE "zip failed: $ZipError"; | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | 1 | eval { | |||||||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | open my $FH, '>', 'data_perl.zip' or die; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 111 | 1 | binmode $FH; | |||||||||||||||||||||||||||||||||||||
| 112 | 1 | print $FH $result; | |||||||||||||||||||||||||||||||||||||
| 113 | 1 | close $FH; | |||||||||||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | 1 | return encode_base64 $result; | |||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | 1 | 1; | |||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||
| 121 | __END__ | ||||||||||||||||||||||||||||||||||||||