Created
April 26, 2012 08:11
-
-
Save polyvision/2497427 to your computer and use it in GitHub Desktop.
example on generating an epub-zip file with ruby and rubyzip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this gist is based on the work of https://github.com/avdgaag/rpub/blob/master/lib/rpub/compressor.rb