class EPUBMaker::ZipExporter

Export into zip file for EPUB producer.

Attributes

tmpdir[R]

Public Class Methods

new(tmpdir, params) click to toggle source
# File lib/epubmaker/zip_exporter.rb, line 27
def initialize(tmpdir, params)
  @tmpdir = tmpdir
  @params = params
end

Public Instance Methods

export_zip(epubfile) click to toggle source
# File lib/epubmaker/zip_exporter.rb, line 32
def export_zip(epubfile)
  if defined?(Zip)
    export_zip_rubyzip(epubfile)
  else
    export_zip_extcmd(epubfile)
  end
end
export_zip_extcmd(epubfile) click to toggle source
# File lib/epubmaker/zip_exporter.rb, line 40
def export_zip_extcmd(epubfile)
  stage1 = @params["epubmaker"]["zip_stage1"].to_s.split
  path1 = stage1[0] || "zip"
  opt1 = stage1[1] || "-0Xq"
  stage2 = @params["epubmaker"]["zip_stage2"].to_s.split
  path2 = stage2[0] || "zip"
  opt2 = stage2[1] || "-Xr9Dq"

  Dir.chdir(tmpdir) do |d|
    system(path1, opt1, epubfile, "mimetype")
    addpath = @params["epubmaker"]["zip_addpath"]
    if addpath
      system(path2, opt2, epubfile, "META-INF", "OEBPS", addpath)
    else
      system(path2, opt2, epubfile, "META-INF", "OEBPS")
    end
  end
end
export_zip_rubyzip(epubfile) click to toggle source
# File lib/epubmaker/zip_exporter.rb, line 59
def export_zip_rubyzip(epubfile)
  Dir.chdir(tmpdir) do |d|
    Zip::OutputStream.open(epubfile) do |epub|
      root_pathname = Pathname.new(tmpdir)
      # relpath = Pathname.new(File.join(tmpdir,'mimetype')).relative_path_from(root_pathname)
      epub.put_next_entry('mimetype', nil, nil, Zip::Entry::STORED)
      epub << "application/epub+zip"

      export_zip_rubyzip_addpath(epub, File.join(tmpdir,'META-INF'), root_pathname)
      export_zip_rubyzip_addpath(epub, File.join(tmpdir,'OEBPS'), root_pathname)
      if @params["zip_addpath"].present?
        export_zip_rubyzip_addpath(epub, File.join(tmpdir, @params["zip_addpath"]), root_pathname)
      end
    end
  end
end
export_zip_rubyzip_addpath(epub, dirname, rootdir) click to toggle source
# File lib/epubmaker/zip_exporter.rb, line 76
def export_zip_rubyzip_addpath(epub, dirname, rootdir)
  Dir[File.join(dirname,'**','**')].each do |path|
    next if File.directory?(path)
    relpath = Pathname.new(path).relative_path_from(rootdir)
    epub.put_next_entry(relpath)
    epub << File.binread(path)
  end
end