File Coverage

blib/lib/EBook/EPUB/Lite/Container/Zip.pm
Criterion Covered Total %
statement 42 50 84.0
branch 5 8 62.5
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 58 69 84.0


line stmt bran cond sub pod time code
1             # Copyright (c) 2009, 2010 Oleksandr Tymoshenko
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     4   21 use strict;
  4         8  
  4         104  
28 4     4   20 use warnings;
  4         6  
  4         103  
29 4     4   2165 use EBook::EPUB::Lite::Container;
  4         43  
  4         128  
30 4     4   3265 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );;
  4         318518  
  4         837  
31 4     4   38 use File::Temp qw/tempfile/;
  4         7  
  4         294  
32 4     4   48 use Carp;
  4         8  
  4         261  
33              
34 4     4   20 use vars qw(@ISA);
  4         8  
  4         1299  
35             @ISA = qw(EBook::EPUB::Lite::Container);
36              
37             sub new
38             {
39 2     2 1 11 my ($class, $zipfile) = @_;
40 2         41 my $self = $class->SUPER::new();
41 2         24 $self->{zipfile} = $zipfile;
42              
43 2         11 return $self;
44             }
45              
46             sub write
47             {
48 2     2 1 7 my ($self) = @_;
49 2         28 my $zip = Archive::Zip->new();
50              
51             # mimetype should come first
52 2         131 $zip->addString("application/epub+zip", "mimetype");
53              
54 2         752 foreach my $f (@{$self->{files}}) {
  2         11  
55             $zip->addFileOrDirectory($f->{frompath},
56 15         4460 $f->{containerpath});
57             }
58              
59 2         1011 my (undef, $tmp_container) = tempfile;
60 2 50       1297 if (!defined($self->write_container($tmp_container))) {
61 0         0 carp "Failed to write container to temporary file $tmp_container";
62 0         0 unlink($tmp_container);
63 0         0 return;
64             }
65              
66 2         17 $zip->addFile($tmp_container, "META-INF/container.xml");
67              
68 2         674 my (undef, $tmp_encryption) = tempfile;
69 2 100       994 if ($self->has_encrypted_files) {
70 1 50       9 if (!defined($self->write_encryption($tmp_encryption))) {
71 0         0 carp "Failed to write encryption to temporary file $tmp_encryption";
72 0         0 unlink($tmp_encryption);
73 0         0 unlink($tmp_container);
74 0         0 return;
75             }
76              
77 1         5 $zip->addFile($tmp_encryption, "META-INF/encryption.xml");
78             }
79              
80 2         268 my $result = $zip->writeToFileNamed($self->{zipfile});
81 2         53338 unlink($tmp_encryption);
82 2         149 unlink($tmp_container);
83 2 50       192 return 1 if ($result == AZ_OK);
84             # We failed
85 0           return;
86             }
87              
88             __END__