#Read files within `.epub`

1 messages · Page 1 of 1 (latest)

light gazelle
#

Is there a way to read the files within an .epub. I've tried using std.tar but unsure if I'm doing it right. I also looked into std.zip but didn't see a way to iterate through them. Any help would be greatly appreciated.

I guess what I really need is to iterate over directory items from a zip file

  const file = try std.fs.openFileAbsolute(path, .{ .mode = .read_only });
  defer file.close();

  var file_name_buf: [std.fs.max_path_bytes]u8 = undefined;
  var link_name_buf: [std.fs.max_path_bytes]u8 = undefined;
  var iter = std.tar.iterator(file.reader(), .{ .file_name_buffer = &file_name_buf, .link_name_buffer = &link_name_buf });
  while (try iter.next()) |item| {
      std.debug.print("{s}", .{item.name});
  }
stuck lake
#

I checked the source of the zip library and could do this, it seems to work with my manual tests

const std = @import("std");
const zip = std.zip;
const fs = std.fs;

pub fn main() !void {
    var zipfile = try fs.cwd().openFile("hello.zip", .{});
    defer zipfile.close();

    const stream = zipfile.seekableStream();
    var zipit = try zip.Iterator(@TypeOf(stream)).init(stream);

    var filename_buff: [fs.max_path_bytes]u8 = undefined;
    while (try zipit.next()) |entry| {
        const filename = filename_buff[0..entry.filename_len];
        try stream.seekTo(entry.header_zip_offset + @sizeOf(zip.CentralDirectoryFileHeader));
        const len = try stream.context.reader().readAll(filename);
        if (len != filename.len) return error.ZipBadFileOffset;
        std.debug.print("- {s}\n", .{filename});
    }
}
light gazelle
light gazelle
stuck lake
light gazelle
#

lmaoooo, all good

stuck lake
light gazelle
stuck lake
light gazelle
#

but I feel like i've butchered the std.zip code so much lmaoo

stuck lake
#

I’ll check when I’ve my laptop around 🙏
From what I see I don’t get what you’re doing after you get the filename, calculating a hash ?

light gazelle
#

i removed the hash part, I just copied parts from the extract + decompress part and instead of writing to a file, I just write to stdout

stuck lake