class ReVIEW::EPUBMaker::ZipExporter

Export into zip file for EPUB producer.

Attributes

tmpdir[R]

Public Class Methods

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

Public Instance Methods

export_zip(epubfile) click to toggle source
# File lib/review/epubmaker/zip_exporter.rb, line 30
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/review/epubmaker/zip_exporter.rb, line 38
def export_zip_extcmd(epubfile)
  stage1 = @config['epubmaker']['zip_stage1'].to_s.split
  path1 = stage1[0] || 'zip'
  opt1 = stage1[1] || '-0Xq'
  stage2 = @config['epubmaker']['zip_stage2'].to_s.split
  path2 = stage2[0] || 'zip'
  opt2 = stage2[1] || '-Xr9Dq'

  Dir.chdir(tmpdir) do
    system(path1, opt1, epubfile, 'mimetype')
    addpath = @config['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/review/epubmaker/zip_exporter.rb, line 57
def export_zip_rubyzip(epubfile)
  Dir.chdir(tmpdir) do
    Zip::OutputStream.open(epubfile) do |epub|
      root_pathname = Pathname.new(tmpdir)
      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 @config['zip_addpath'].present?
        export_zip_rubyzip_addpath(epub, File.join(tmpdir, @config['zip_addpath']), root_pathname)
      end
    end
  end
end
export_zip_rubyzip_addpath(epub, dirname, rootdir) click to toggle source
# File lib/review/epubmaker/zip_exporter.rb, line 73
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