Skip to content

Instantly share code, notes, and snippets.

@polyvision
Created April 26, 2012 08:11
Show Gist options
  • Save polyvision/2497427 to your computer and use it in GitHub Desktop.
Save polyvision/2497427 to your computer and use it in GitHub Desktop.
example on generating an epub-zip file with ruby and rubyzip
entries = Hash.new
# fill the entries with files like this
# entries['OEBPS/chapter1.xhtml'] = "complete_file_path_on_the filesystem"
FileUtils.remove(zip_path) if File.exists?(zip_path) # deleting the old zip file
# creating the output zip stream
zip = Zip::ZipOutputStream.new(zip_path)
# this is the real sucker of epub, the MUST contain a file called
# mimetype and it's first entry in the zip AND WITHOUT compression with the specified mimetype below
zip.put_next_entry('mimetype', nil, nil, Zip::ZipEntry::STORED, Zlib::NO_COMPRESSION)
zip.write "application/epub+zip"
entries.keys.each do |key|
# now let's add all the stuff to the epub file
zip.put_next_entry key, nil, nil, Zip::ZipEntry::DEFLATED, Zlib::BEST_COMPRESSION
zip.write IO.read(entries[key])
end
zip.close # we're finished
puts "EPub.zip: wrote #{self.zip_output_path}"
@polyvision
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment