Skip to main content

ZipHandler

Struct ZipHandler 

Source
pub struct ZipHandler<R: Read + Seek> { /* private fields */ }
Expand description

A handler for reading EPUB files as ZIP archives.

This struct wraps a ZipArchive and provides convenience methods for reading specific files needed for EPUB parsing:

  • Locating the OPF file via META-INF/container.xml
  • Reading text files (XML, HTML, CSS, etc.)
  • Reading binary files (images, fonts, etc.)

§Example

use std::path::Path;
use epub_parser::utils::ZipHandler;

let mut handler = ZipHandler::new(Path::new("book.epub"))?;
let opf_path = handler.get_opf_path()?;
println!("OPF location: {}", opf_path);

Implementations§

Source§

impl ZipHandler<File>

Source

pub fn new(path: &Path) -> Result<Self, Error>

Creates a new ZipHandler from a file path.

§Arguments
  • path - The path to the EPUB file.
§Returns

Returns Ok(ZipHandler) on success, or an error if the file cannot be opened or is not a valid ZIP archive.

§Errors

Returns an error if:

  • The file does not exist
  • The file cannot be opened
  • The file is not a valid ZIP archive
Source§

impl<R: Read + Seek> ZipHandler<R>

Source

pub fn new_from_reader(reader: R) -> Result<Self, Error>

Creates a new ZipHandler from any reader that implements Read + Seek.

This is useful for parsing EPUBs from memory (e.g., byte buffers) or network streams.

§Arguments
  • reader - Any type implementing Read + Seek (e.g., Cursor<Vec<u8>>).
§Returns

Returns Ok(ZipHandler) on success, or an error if the reader does not contain a valid ZIP archive.

§Example
use std::io::Cursor;
use epub_parser::utils::ZipHandler;

let data = vec![0u8; 100]; // In practice, this would be EPUB data
// handler = ZipHandler::new_from_reader(Cursor::new(data))?;
Source

pub fn get_opf_path(&mut self) -> Result<String, Error>

Locates the OPF (Open Package Format) file path.

EPUB files contain a META-INF/container.xml file that specifies the location of the OPF file. This method parses that XML and returns the path to the OPF file.

§Returns

Returns the path to the OPF file as a string (e.g., “OEBPS/content.opf”).

§Errors

Returns Error::MissingContainer if META-INF/container.xml is missing. Returns Error::MissingOpf if the OPF path cannot be found.

Source

pub fn read_file(&mut self, path: &str) -> Result<String, Error>

Reads a file from the ZIP archive as a UTF-8 string.

§Arguments
  • path - The path to the file within the ZIP archive.
§Returns

Returns the file contents as a String.

§Errors

Returns an error if:

  • The file does not exist in the archive
  • The file cannot be read
  • The file contains invalid UTF-8
Source

pub fn read_file_as_bytes(&mut self, path: &str) -> Result<Vec<u8>, Error>

Reads a file from the ZIP archive as raw bytes.

This is useful for binary files like images and fonts.

§Arguments
  • path - The path to the file within the ZIP archive.
§Returns

Returns the file contents as a Vec<u8>.

§Errors

Returns an error if the file does not exist or cannot be read.

Auto Trait Implementations§

§

impl<R> Freeze for ZipHandler<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for ZipHandler<R>
where R: RefUnwindSafe,

§

impl<R> Send for ZipHandler<R>
where R: Send,

§

impl<R> Sync for ZipHandler<R>
where R: Sync,

§

impl<R> Unpin for ZipHandler<R>
where R: Unpin,

§

impl<R> UnwindSafe for ZipHandler<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.