File Coverage

blib/lib/EBook/EPUB/Container/Zip.pm
Criterion Covered Total %
statement 18 47 38.3
branch 0 8 0.0
condition n/a
subroutine 6 8 75.0
pod 2 2 100.0
total 26 65 40.0


line stmt bran cond sub pod time code
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::Container::Zip;
26              
27 1     1   1360 use strict;
  1         2  
  1         43  
28 1     1   5 use EBook::EPUB::Container;
  1         2  
  1         28  
29 1     1   1436 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );;
  1         102725  
  1         214  
30 1     1   11 use File::Temp qw/tempfile/;
  1         2  
  1         88  
31 1     1   6 use Carp;
  1         3  
  1         68  
32              
33 1     1   6 use vars qw(@ISA);
  1         2  
  1         958  
34             @ISA = qw(EBook::EPUB::Container);
35              
36             sub new
37             {
38 0     0 1   my ($class, $zipfile) = @_;
39 0           my $self = $class->SUPER::new();
40 0           $self->{zipfile} = $zipfile;
41              
42 0           return $self;
43             }
44              
45             sub write
46             {
47 0     0 1   my ($self) = @_;
48 0           my $zip = Archive::Zip->new();
49              
50             # mimetype should come first
51 0           $zip->addString("application/epub+zip", "mimetype");
52              
53 0           foreach my $f (@{$self->{files}}) {
  0            
54 0           $zip->addFileOrDirectory($f->{frompath},
55             $f->{containerpath});
56             }
57              
58 0           my (undef, $tmp_container) = tempfile;
59 0 0         if (!defined($self->write_container($tmp_container))) {
60 0           carp "Failed to write container to temporary file $tmp_container";
61 0           unlink($tmp_container);
62 0           return;
63             }
64              
65 0           $zip->addFile($tmp_container, "META-INF/container.xml");
66              
67 0           my (undef, $tmp_encryption) = tempfile;
68 0 0         if ($self->has_encrypted_files) {
69 0 0         if (!defined($self->write_encryption($tmp_encryption))) {
70 0           carp "Failed to write encryption to temporary file $tmp_encryption";
71 0           unlink($tmp_encryption);
72 0           unlink($tmp_container);
73 0           return;
74             }
75              
76 0           $zip->addFile($tmp_encryption, "META-INF/encryption.xml");
77             }
78              
79 0           my $result = $zip->writeToFileNamed($self->{zipfile});
80 0           unlink($tmp_encryption);
81 0           unlink($tmp_container);
82 0 0         return 1 if ($result == AZ_OK);
83             # We failed
84 0           return;
85             }
86              
87             __END__
88              
89             =head1 NAME
90              
91             EBook::EPUB::Container::Zip
92              
93             =head1 SYNOPSIS
94              
95             Zip OEPBS Container implementation
96              
97             my $container = EBook::EPUB::Container::Zip->new('/path/to/file.epub')
98              
99             # EBook::EPUB::Container methods
100             $container->add_path('/path/to/content.ncx', 'DATA/content.nx');
101             $container->add_path('/path/to/page1.xhtml', 'DATA/page1.xhtml');
102             $container->add_path('/path/to/page2.xhtml', 'DATA/page2.xhtml');
103             $container->add_root_file('DATA/content.ncx');
104              
105             # Write it to disk
106             $container->write();
107              
108             =head1 SUBROUTINES/METHODS
109              
110             =over 4
111              
112             =item new($zipfile)
113              
114             Create new instance of EBook::EPUB::Container::Zip object. $zipfile is a file where container should be saved
115              
116             =item write()
117              
118             Create zip file with container contents
119              
120             =back
121              
122             =head1 AUTHOR
123              
124             Oleksandr Tymoshenko, E<lt>gonzo@bluezbox.comE<gt>
125              
126             =head1 BUGS
127              
128             Please report any bugs or feature requests to E<lt>gonzo@bluezbox.comE<gt>
129              
130             =head1 LICENSE AND COPYRIGHT
131              
132             Copyright 2009, 2010 Oleksandr Tymoshenko.
133              
134             L<http://bluezbox.com>
135              
136             This module is free software; you can redistribute it and/or
137             modify it under the terms of the BSD license. See the F<LICENSE> file
138             included with this distribution.