Class: EpubTools::UnpackEbook

Inherits:
Object
  • Object
show all
Defined in:
lib/epub_tools/unpack_ebook.rb

Overview

Unpacks an EPUB (.epub file) into a directory

Instance Method Summary collapse

Constructor Details

#initialize(epub_file, output_dir = nil, verbose: false) ⇒ UnpackEbook

epub_file: path to the .epub file output_dir: directory to extract into; defaults to basename of epub_file without .epub



9
10
11
12
13
14
15
16
17
18
# File 'lib/epub_tools/unpack_ebook.rb', line 9

def initialize(epub_file, output_dir = nil, verbose: false)
  @epub_file = File.expand_path(epub_file)
  default_dir = [File.dirname(@epub_file), File.basename(@epub_file, '.epub')].join("/")
  @output_dir = if output_dir.nil? || output_dir.empty?
                  default_dir
                else
                  output_dir
                end
  @verbose = verbose
end

Instance Method Details

#runObject

Extracts all entries from the EPUB into the output directory



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/epub_tools/unpack_ebook.rb', line 21

def run
  validate!
  FileUtils.mkdir_p(@output_dir)
  Zip::File.open(@epub_file) do |zip|
    zip.each do |entry|
      dest_path = File.join(@output_dir, entry.name)
      if entry.directory?
        FileUtils.mkdir_p(dest_path)
      else
        FileUtils.mkdir_p(File.dirname(dest_path))
        entry.extract(dest_path) { true }
      end
    end
  end
  puts "Unpacked #{File.basename(@epub_file)} to #{@output_dir}" if @verbose
end