Class: EPUBMaker::ZipExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/epubmaker/zip_exporter.rb

Overview

Export into zip file for EPUB producer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tmpdir, config) ⇒ ZipExporter

Returns a new instance of ZipExporter.



24
25
26
27
# File 'lib/epubmaker/zip_exporter.rb', line 24

def initialize(tmpdir, config)
  @tmpdir = tmpdir
  @config = config
end

Instance Attribute Details

#tmpdirObject (readonly)

Returns the value of attribute tmpdir.



22
23
24
# File 'lib/epubmaker/zip_exporter.rb', line 22

def tmpdir
  @tmpdir
end

Instance Method Details

#export_zip(epubfile) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/epubmaker/zip_exporter.rb', line 29

def export_zip(epubfile)
  if defined?(Zip)
    export_zip_rubyzip(epubfile)
  else
    export_zip_extcmd(epubfile)
  end
end

#export_zip_extcmd(epubfile) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/epubmaker/zip_exporter.rb', line 37

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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/epubmaker/zip_exporter.rb', line 56

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) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/epubmaker/zip_exporter.rb', line 72

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