class Epubber::Services::ZipFileGenerator
Helper class to create the actual zip file
Public Class Methods
new(inputDir, outputFile)
click to toggle source
Initialize with the directory to zip and the location of the output archive.
# File lib/epubber/services/compressor.rb, line 15 def initialize(inputDir, outputFile) @inputDir = inputDir @outputFile = outputFile end
Public Instance Methods
write()
click to toggle source
Zip the input directory.
# File lib/epubber/services/compressor.rb, line 21 def write() entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..") io = Zip::File.open(@outputFile, Zip::File::CREATE); writeEntries(entries, "", io) io.close(); end
Private Instance Methods
writeEntries(entries, path, io)
click to toggle source
# File lib/epubber/services/compressor.rb, line 31 def writeEntries(entries, path, io) entries.each { |e| zipFilePath = path == "" ? e : File.join(path, e) diskFilePath = File.join(@inputDir, zipFilePath) if File.directory?(diskFilePath) io.mkdir(zipFilePath) subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..") writeEntries(subdir, zipFilePath, io) else io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())} end } end