20 stable releases

Uses new Rust 2024

2.1.5 Oct 29, 2025
2.1.4 May 21, 2025
2.1.3 Apr 21, 2025
2.1.2 May 2, 2024
0.1.2 Jan 20, 2017

#236 in Filesystem

Download history 2628/week @ 2026-04-19 2301/week @ 2026-04-26 2111/week @ 2026-05-03 2380/week @ 2026-05-10 2815/week @ 2026-05-17 3773/week @ 2026-05-24 3870/week @ 2026-05-31 4467/week @ 2026-06-07 3385/week @ 2026-06-14 3276/week @ 2026-06-21 3757/week @ 2026-06-28 5660/week @ 2026-07-05 6393/week @ 2026-07-12 7757/week @ 2026-07-19 10161/week @ 2026-07-26 9049/week @ 2026-08-02

34,517 downloads per month
Used in 43 crates (35 directly)

GPL-3.0 license

1MB
918 lines

epub-rs

Rust library to support the reading of epub files.

Install

Add this to your Cargo.toml:

[dependencies]
epub = "1.2.2"

MSRV

The minimum supported Rust version is 1.85.0.


lib.rs:

EPUB library lib to read and navigate through an epub file contents

Examples

Opening

use epub::doc::EpubDoc;
let doc = EpubDoc::new("test.epub");
assert!(doc.is_ok());
let doc = doc.unwrap();

Getting doc metadata

Metadata is a HashMap storing all metadata defined in the epub

let language = doc.mdata("language");
assert_eq!(language.unwrap().value, "es");

Accessing resources

In the resources var is stored each resource defined in the epub indexed by the id and with the full internal path and mimetype. It's a HashMap<a: String, (b: String, c: String)> where a is the resource id, b is the resource full path and c is the resource mimetype

assert_eq!(23, doc.resources.len());
let tpage = doc.resources.get("titlepage.xhtml");
assert_eq!(tpage.unwrap().path, Path::new("OEBPS/Text/titlepage.xhtml"));
assert_eq!(tpage.unwrap().mime, "application/xhtml+xml");

Navigating using the spine

Spine is a Vec<String> storing the epub spine as resources ids

assert_eq!(17, doc.spine.len());
assert_eq!("titlepage.xhtml", doc.spine[0].idref);

Navigation using the doc internal state

use epub::doc::EpubDoc;
let doc = EpubDoc::new("test.epub");
let mut doc = doc.unwrap();
assert_eq!(0, doc.get_current_chapter());
assert_eq!("application/xhtml+xml", doc.get_current_mime().unwrap());

doc.go_next();
assert_eq!("000.xhtml", doc.get_current_id().unwrap());
doc.go_next();
assert_eq!("001.xhtml", doc.get_current_id().unwrap());
doc.go_prev();
assert_eq!("000.xhtml", doc.get_current_id().unwrap());

doc.set_current_chapter(2);
assert_eq!("001.xhtml", doc.get_current_id().unwrap());
assert_eq!(2, doc.get_current_chapter());
assert!(!doc.set_current_chapter(50));

// doc.get_current() will return a Vec<u8> with the current chapter content
// doc.get_current_str() will return a String with the current chapter content

Getting the cover

use std::fs;
use std::io::Write;
use epub::doc::EpubDoc;

let doc = EpubDoc::new("test.epub");
assert!(doc.is_ok());
let mut doc = doc.unwrap();

let cover_data = doc.get_cover().unwrap();

let f = fs::File::create("/tmp/cover.png");
assert!(f.is_ok());
let mut f = f.unwrap();
let resp = f.write_all(&cover_data);

Dependencies

~4.5–6.5MB
~118K SLoC