1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Manages the zip component part of the epub doc.
//!
//! Provides easy methods to navigate through the epub parts and to get
//! the content as string.

use anyhow::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};

use std::io::{Read, Seek};

/// Epub archive struct. Here it's stored the file path and the list of
/// files in the zip archive.
pub struct EpubArchive<R: Read + Seek> {
    zip: zip::ZipArchive<R>,
    pub path: PathBuf,
    pub files: Vec<String>,
}

impl EpubArchive<BufReader<File>> {
    /// Opens the epub file in `path`.
    ///
    /// # Errors
    ///
    /// Returns an error if the zip is broken or if the file doesn't
    /// exists.
    pub fn new<P: AsRef<Path>>(path: P) -> Result<EpubArchive<BufReader<File>>, Error> {
        let path = path.as_ref();
        let file = File::open(path)?;
        let mut archive = EpubArchive::from_reader(BufReader::new(file))?;
        archive.path = path.to_path_buf();
        Ok(archive)
    }
}

impl<R: Read + Seek> EpubArchive<R> {
    /// Opens the epub contained in `reader`.
    ///
    /// # Errors
    ///
    /// Returns an error if the zip is broken.
    pub fn from_reader(reader: R) -> Result<EpubArchive<R>, Error> {
        let zip = zip::ZipArchive::new(reader)?;

        let files:Vec<String> = zip.file_names().map(|f| f.to_string()).collect();

        Ok(EpubArchive {
            zip,
            path: PathBuf::new(),
            files,
        })
    }

    /// Returns the content of the file by the `name` as `Vec<u8>`.
    ///
    /// # Errors
    ///
    /// Returns an error if the name doesn't exists in the zip archive.
    pub fn get_entry<P: AsRef<Path>>(&mut self, name: P) -> Result<Vec<u8>, Error> {
        let mut entry: Vec<u8> = vec![];
        let name = name.as_ref().display().to_string();
        match self.zip.by_name(&name) {
            Ok(mut zipfile) => {
                zipfile.read_to_end(&mut entry)?;
                return Ok(entry);
            }
            Err(zip::result::ZipError::FileNotFound) => {}
            Err(e) => {
                return Err(e.into());
            }
        };

        // try percent encoding
        let name = percent_encoding::percent_decode(name.as_str().as_bytes()).decode_utf8()?;
        let mut zipfile = self.zip.by_name(&name)?;
        zipfile.read_to_end(&mut entry)?;
        Ok(entry)
    }

    /// Returns the content of the file by the `name` as `String`.
    ///
    /// # Errors
    ///
    /// Returns an error if the name doesn't exists in the zip archive.
    pub fn get_entry_as_str<P: AsRef<Path>>(&mut self, name: P) -> Result<String, Error> {
        let content = self.get_entry(name)?;
        String::from_utf8(content).map_err(Error::from)
    }

    /// Returns the content of container file "META-INF/container.xml".
    ///
    /// # Errors
    ///
    /// Returns an error if the epub doesn't have the container file.
    pub fn get_container_file(&mut self) -> Result<Vec<u8>, Error> {
        let content = self.get_entry("META-INF/container.xml")?;
        Ok(content)
    }
}