extern crate zip;
use std::fs;
use std::path;
use std::error::Error;
use std::io::Read;
pub struct EpubArchive {
zip: zip::ZipArchive<fs::File>,
pub path: String,
pub files: Vec<String>,
}
impl EpubArchive {
pub fn new(path: &str) -> Result<EpubArchive, Box<Error>> {
let fname = path::Path::new(path);
let file = try!(fs::File::open(&fname));
let mut zip = try!(zip::ZipArchive::new(file));
let mut files = vec!();
for i in 0..(zip.len()) {
let file = try!(zip.by_index(i));
files.push(String::from(file.name()));
}
Ok(EpubArchive {
zip: zip,
path: String::from(path),
files: files
})
}
pub fn get_entry(&mut self, name: &str) -> Result<Vec<u8>, Box<Error>> {
let mut entry: Vec<u8> = vec!();
let mut zipfile = try!(self.zip.by_name(name));
try!(zipfile.read_to_end(&mut entry));
Ok(entry)
}
pub fn get_entry_as_str(&mut self, name: &str) -> Result<String, Box<Error>> {
let mut entry = String::new();
let mut zipfile = try!(self.zip.by_name(name));
try!(zipfile.read_to_string(&mut entry));
Ok(entry)
}
pub fn get_container_file(&mut self) -> Result<Vec<u8>, Box<Error>> {
let content = try!(self.get_entry("META-INF/container.xml"));
Ok(content)
}
}