| 1 | # Copyright (c) 2009, 2010 Oleksandr Tymoshenko <gonzo@bluezbox.com> | ||||||||||
| 2 | # All rights reserved. | ||||||||||
| 3 | |||||||||||
| 4 | # Redistribution and use in source and binary forms, with or without | ||||||||||
| 5 | # modification, are permitted provided that the following conditions | ||||||||||
| 6 | # are met: | ||||||||||
| 7 | # 1. Redistributions of source code must retain the above copyright | ||||||||||
| 8 | # notice, this list of conditions and the following disclaimer. | ||||||||||
| 9 | # 2. Redistributions in binary form must reproduce the above copyright | ||||||||||
| 10 | # notice, this list of conditions and the following disclaimer in the | ||||||||||
| 11 | # documentation and/or other materials provided with the distribution. | ||||||||||
| 12 | |||||||||||
| 13 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||||||||||
| 14 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||||||
| 15 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||
| 16 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||||||||||
| 17 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||||||||||
| 18 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||||||||||
| 19 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||||||||||
| 20 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||||||||
| 21 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||||||||
| 22 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||||||||
| 23 | # SUCH DAMAGE. | ||||||||||
| 24 | |||||||||||
| 25 | package EBook::EPUB::Lite::Container::Zip; | ||||||||||
| 26 | |||||||||||
| 27 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 4 | ▶ | use EBook::EPUB::Lite::Container; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 4 | ▶ | use Archive::Zip qw( :ERROR_CODES :CONSTANTS );; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 4 | ▶ | use File::Temp qw/tempfile/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 4 | ▶ | use vars qw(@ISA); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 4 | @ISA = qw(EBook::EPUB::Lite::Container); | |||||||||
| 36 | |||||||||||
| 37 | sub new | ||||||||||
| 38 | 2 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 39 | 2 | my ($class, $zipfile) = @_; | |||||||||
| 40 | 2 | my $self = $class->SUPER::new(); | |||||||||
| 41 | 2 | $self->{zipfile} = $zipfile; | |||||||||
| 42 | |||||||||||
| 43 | 2 | return $self; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub write | ||||||||||
| 47 | 2 | ▶ | { | ||||||||
|
Subroutine
write: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||
| 48 | 2 | my ($self) = @_; | |||||||||
| 49 | 2 | my $zip = Archive::Zip->new(); | |||||||||
| 50 | |||||||||||
| 51 | # mimetype should come first | ||||||||||
| 52 | 2 | $zip->addString("application/epub+zip", "mimetype"); | |||||||||
| 53 | |||||||||||
| 54 | 2 | foreach my $f (@{$self->{files}}) { | |||||||||
| 55 | $zip->addFileOrDirectory($f->{frompath}, | ||||||||||
| 56 | 15 | $f->{containerpath}); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 2 | my (undef, $tmp_container) = tempfile; | |||||||||
| 60 | 2 | ▶ | if (!defined($self->write_container($tmp_container))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | carp "Failed to write container to temporary file $tmp_container"; | |||||||||
| 62 | 0 | unlink($tmp_container); | |||||||||
| 63 | 0 | return; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 2 | $zip->addFile($tmp_container, "META-INF/container.xml"); | |||||||||
| 67 | |||||||||||
| 68 | 2 | my (undef, $tmp_encryption) = tempfile; | |||||||||
| 69 | 2 | ▶ | if ($self->has_encrypted_files) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 1 | ▶ | if (!defined($self->write_encryption($tmp_encryption))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | carp "Failed to write encryption to temporary file $tmp_encryption"; | |||||||||
| 72 | 0 | unlink($tmp_encryption); | |||||||||
| 73 | 0 | unlink($tmp_container); | |||||||||
| 74 | 0 | return; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 1 | $zip->addFile($tmp_encryption, "META-INF/encryption.xml"); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 2 | my $result = $zip->writeToFileNamed($self->{zipfile}); | |||||||||
| 81 | 2 | unlink($tmp_encryption); | |||||||||
| 82 | 2 | unlink($tmp_container); | |||||||||
| 83 | 2 | ▶ | return 1 if ($result == AZ_OK); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | # We failed | ||||||||||
| 85 | 0 | return; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | __END__ | ||||||||||