| 1 | package WWW::SFDC::Zip; | ||
| 2 | # ABSTRACT: Utilities for manipulating base64 encoded zip files. | ||
| 3 | |||
| 4 | use 5.12.0; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Data::Dumper; | ||
| 9 | use File::Path qw(mkpath); | ||
| 10 | use File::Spec::Functions qw(splitpath); | ||
| 11 | use IO::Compress::Zip qw{$ZipError zip :constants}; | ||
| 12 | use IO::File; | ||
| 13 | use IO::Uncompress::Unzip qw($UnzipError); | ||
| 14 | use Log::Log4perl ':easy'; | ||
| 15 | use MIME::Base64; | ||
| 16 | |||
| 17 | |||
| 18 | BEGIN { | ||
| 19 | use Exporter; | ||
| 20 | our @ISA = qw(Exporter); | ||
| 21 | our @EXPORT_OK = qw(unzip makezip); | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | sub unzip { | ||
| 26 | # you need to understand IO::Uncompress::unzip | ||
| 27 | # for this function | ||
| 28 | my ($dest, $data, $callback) = @_; | ||
| 29 | INFO "Unzipping files to $dest"; | ||
| 30 | TRACE "Data to unzip" => $data; | ||
| 31 | LOGDIE "No destination!" unless $dest; | ||
| 32 | |||
| 33 | # Ensure $dest ends with a / | ||
| 34 | $dest =~ s{(?<![/\\])$}{/}; | ||
| 35 | |||
| 36 | $data = decode_base64 $data; | ||
| 37 | my $unzipper = IO::Uncompress::Unzip->new(\$data) | ||
| 38 | or LOGDIE "Couldn't unzip data"; | ||
| 39 | |||
| 40 | my $status; | ||
| 41 | |||
| 42 | for ($status = 1; $status > 0; $status = $unzipper->nextStream()) { | ||
| 43 | my $header = $unzipper->getHeaderInfo(); | ||
| 44 | my (undef, $folder, $name) = splitpath($header->{Name}); | ||
| 45 | |||
| 46 | $folder =~ s{unpackaged/}{}; | ||
| 47 | $folder = $dest.$folder; | ||
| 48 | |||
| 49 | # create folder on disk unless it exists already | ||
| 50 | mkpath($folder) or LOGDIE "Couldn't mkdir $folder: $!" unless -d $folder; | ||
| 51 | |||
| 52 | # skip if the file is a folder, exit on error | ||
| 53 | $status < 0 ? last : next if $name =~ /\/$/; | ||
| 54 | |||
| 55 | # read content into memory | ||
| 56 | my $buffer; | ||
| 57 | my $content; | ||
| 58 | $content .= $buffer while ($status = $unzipper->read($buffer)) > 0; | ||
| 59 | my $path = "$folder/$name"; | ||
| 60 | |||
| 61 | # use callback, if defined | ||
| 62 | $content = $callback->($path, $content) if $callback; | ||
| 63 | |||
| 64 | if ($content) { | ||
| 65 | # open target for writing | ||
| 66 | my $fh = IO::File->new($path, "w") or LOGDIE "Couldn't write to $path: $!"; | ||
| 67 | $fh->binmode(); | ||
| 68 | $fh->write($content); | ||
| 69 | $fh->close(); | ||
| 70 | # update time on target | ||
| 71 | my $stored_time = $header->{'Time'}; | ||
| 72 | utime ($stored_time, $stored_time, $path) or LOGDIE "Couldn't touch $path: $!"; | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | |||
| 76 | return "Success"; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | sub makezip { | ||
| 81 | my ($baseDir, @files) = @_; | ||
| 82 | |||
| 83 | TRACE "File list before grep: " . Dumper \@files; | ||
| 84 | LOGDIE "It is invalid to call makezip with no files." unless scalar @files; | ||
| 85 | |||
| 86 | $baseDir =~ s{(?<![/\\])$}{/}; | ||
| 87 | |||
| 88 | @files = grep {-e $_ && !-d $_} | ||
| 89 | map {$baseDir.$_} | ||
| 90 | @files; | ||
| 91 | |||
| 92 | DEBUG "File list for zipping: " . Dumper \@files; | ||
| 93 | INFO "Writing zip file with ". scalar(@files) ." files"; | ||
| 94 | |||
| 95 | my $result; | ||
| 96 | |||
| 97 | zip | ||
| 98 | \@files => \$result, | ||
| 99 | FilterName => sub { s/$baseDir// if $baseDir; }, | ||
| 100 | Level => 9, | ||
| 101 | Minimal => 1, | ||
| 102 | BinModeIn => 1, | ||
| 103 | or LOGDIE "zip failed: $ZipError"; | ||
| 104 | |||
| 105 | eval { | ||
| 106 | open my $FH, '>', 'data_perl.zip' or die; | ||
| 107 | binmode $FH; | ||
| 108 | print $FH $result; | ||
| 109 | close $FH; | ||
| 110 | }; | ||
| 111 | |||
| 112 | return encode_base64 $result; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ | ||
| 118 | |||
| 119 | =pod | ||
| 120 | |||
| 121 | =head1 NAME | ||
| 122 | |||
| 123 | WWW::SFDC::Zip - Utilities for manipulating base64 encoded zip files. | ||
| 124 | |||
| 125 | =head1 VERSION | ||
| 126 | |||
| 127 | version 0.22 | ||
| 128 | |||
| 129 | =head1 SYNOPSIS | ||
| 130 | |||
| 131 | use WWW::SFDC::Zip qw"unzip makezip"; | ||
| 132 | |||
| 133 | makezip $srcDirectory, @listOfFiles; | ||
| 134 | |||
| 135 | unzip $destDirectory, $base64encodedzipfile, &unzipTimeChanges | ||
| 136 | |||
| 137 | =head1 FUNCTIONS | ||
| 138 | |||
| 139 | =head2 unzip $destFolder, $dataString, $callback | ||
| 140 | |||
| 141 | Takes a some base64 $data and turns it into a file tree, starting | ||
| 142 | at $dest. It does this by turning unpackaged/ into $dest/ whilst | ||
| 143 | unzipping, so the data needs to come from an above-defined retrieve | ||
| 144 | request. | ||
| 145 | |||
| 146 | Whilst each file is in memory, this function calls: | ||
| 147 | |||
| 148 | $callback->($filename, $content) | ||
| 149 | |||
| 150 | In this way, you can use $calback to modify or remove files before | ||
| 151 | they get written to disk. | ||
| 152 | |||
| 153 | =head2 makezip \%options, @fileList | ||
| 154 | |||
| 155 | Creates and returns a zip stream from the file list | ||
| 156 | given. Replaces unpackaged/ with $options{basedir} if set. | ||
| 157 | |||
| 158 | =head1 EXPORT | ||
| 159 | |||
| 160 | Can export unzip and makezip. | ||
| 161 | |||
| 162 | =head1 BUGS | ||
| 163 | |||
| 164 | Please report any bugs or feature requests at L<https://github.com/alexander-brett/WWW-SFDC/issues>. | ||
| 165 | |||
| 166 | =head1 SUPPORT | ||
| 167 | |||
| 168 | You can find documentation for this module with the perldoc command. | ||
| 169 | |||
| 170 | perldoc WWW::SFDC::Zip | ||
| 171 | |||
| 172 | You can also look for information at L<https://github.com/alexander-brett/WWW-SFDC> | ||
| 173 | |||
| 174 | =head1 AUTHOR | ||
| 175 | |||
| 176 | Alexander Brett <alexander.brett@sophos.com> | ||
| 177 | |||
| 178 | =head1 COPYRIGHT AND LICENSE | ||
| 179 | |||
| 180 | This software is Copyright (c) 2015 by Sophos Limited. | ||
| 181 | |||
| 182 | This is free software, licensed under: | ||
| 183 | |||
| 184 | The MIT (X11) License | ||
| 185 | |||
| 186 | The full text of the license can be found in the | ||
| 187 | F<LICENSE> file included with this distribution. | ||
| 188 | |||
| 189 | =cut |