Class: Epzip

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

Constant Summary collapse

MIMETYPE_FILENAME =
'mimetype'
@@zip_cmd_path =
'zip'
@@unzip_cmd_path =
'unzip'

Class Method Summary collapse

Class Method Details

.unzip(epubfile, epubdir = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/epzip.rb', line 37

def self.unzip(epubfile, epubdir = nil)
  if epubdir
    FileUtils.mkdir_p(epubdir)
  else
    epubdir = Dir.pwd
  end

  system("#{@@unzip_cmd_path} #{epubfile} -d #{epubdir}")
end

.unzip_cmd_pathObject



12
# File 'lib/epzip.rb', line 12

def self.unzip_cmd_path;       @@unzip_cmd_path end

.unzip_cmd_path=(cmd) ⇒ Object



11
# File 'lib/epzip.rb', line 11

def self.unzip_cmd_path=(cmd); @@unzip_cmd_path = cmd end

.zip(epubdir, epubfile = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/epzip.rb', line 14

def self.zip(epubdir, epubfile = nil)
  if !File.exists? epubdir
    raise ArgumentError, "No such directory -- #{epubdir}"
  end

  epubfile ||= epubdir+".epub"
  
  Dir.mktmpdir do |tmpdir|
    tmpfile = "#{tmpdir}/tmp.epub"

    Dir.chdir(epubdir) do
      File.open(MIMETYPE_FILENAME, "w") do |f|
        f.write("application/epub+zip")
      end
      system("#{@@zip_cmd_path} -0X #{tmpfile} #{MIMETYPE_FILENAME}")
      system("#{@@zip_cmd_path} -Xr9D #{tmpfile} * -x #{MIMETYPE_FILENAME}")
    end

    FileUtils.cp(tmpfile, epubfile)
  end
  epubfile
end

.zip_cmd_pathObject



10
# File 'lib/epzip.rb', line 10

def self.zip_cmd_path;       @@zip_cmd_path end

.zip_cmd_path=(cmd) ⇒ Object



9
# File 'lib/epzip.rb', line 9

def self.zip_cmd_path=(cmd); @@zip_cmd_path = cmd end