Class: Epzip
- Inherits:
-
Object
- Object
- Epzip
- Defined in:
- lib/epzip.rb,
lib/epzip/version.rb
Constant Summary collapse
- MIMETYPE_FILENAME =
'mimetype'.freeze
- MIMETYPE_CONTENT =
'application/epub+zip'.freeze
- VERSION =
"0.9.0"
Class Method Summary collapse
Class Method Details
.unzip(epubfile, epubdir = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/epzip.rb', line 35 def self.unzip(epubfile, epubdir = nil) epubdir ||= Dir.pwd FileUtils.mkdir_p(epubdir) root = File.(epubdir) Zip::File.open(epubfile) do |zip| zip.each do |entry| next if entry.name_is_directory? filepath = File.(File.join(epubdir, entry.name)) # Guard against Zip Slip: rubyzip skips its own name check when an # explicit destination path is given, so reject entries that would # escape the target directory. unless filepath == root || filepath.start_with?(root + File::SEPARATOR) raise ArgumentError, "Illegal entry name -- #{entry.name}" end FileUtils.mkdir_p(File.dirname(filepath)) entry.extract(filepath) { true } end end epubdir end |
.zip(epubdir, epubfile = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/epzip.rb', line 9 def self.zip(epubdir, epubfile = nil) unless File.exist?(epubdir) raise ArgumentError, "No such directory -- #{epubdir}" end epubfile ||= epubdir + ".epub" Zip::OutputStream.open(epubfile) do |zos| # The mimetype entry must come first and be stored uncompressed. zos.put_next_entry(MIMETYPE_FILENAME, nil, nil, Zip::Entry::STORED) zos << MIMETYPE_CONTENT Dir.chdir(epubdir) do Dir.glob("**/*") do |path| next if path == MIMETYPE_FILENAME next unless File.file?(path) zos.put_next_entry(path) zos << File.binread(path) end end end epubfile end |