Class: EpubTools::UnpackEbook

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

Overview

Unpacks an EPUB (.epub file) into a directory

Instance Method Summary collapse

Methods included from Loggable

#log

Constructor Details

#initialize(options = {}) ⇒ UnpackEbook

Initializes the class

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :epub_file (String)

    Path to the .epub file to unpack (required)

  • :output_dir (String)

    Directory to extract into (default: basename of epub_file without .epub)

  • :verbose (Boolean)

    Whether to print progress to STDOUT (default: false)



14
15
16
17
18
19
# File 'lib/epub_tools/unpack_ebook.rb', line 14

def initialize(options = {})
  @epub_file = File.expand_path(options.fetch(:epub_file))
  output_dir = options[:output_dir]
  @output_dir = output_dir.nil? || output_dir.empty? ? default_dir : output_dir
  @verbose = options[:verbose] || false
end

Instance Method Details

#runObject

Extracts all entries from the EPUB into the output directory. Returns the output directory.



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

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
  log "Unpacked #{File.basename(@epub_file)} to #{@output_dir}"
  @output_dir
end