diff --git a/lib/gepub/book.rb b/lib/gepub/book.rb index 40ff37f..fe62808 100644 --- a/lib/gepub/book.rb +++ b/lib/gepub/book.rb @@ -94,15 +94,15 @@ def self.rootfile_from_container(rootfile) doc.css("#{defaultns}|rootfiles > #{defaultns}|rootfile")[0]['full-path'] end - # Parses existing EPUB2/EPUB3 files from an IO object, and creates new Book object. + # Parses existing EPUB2/EPUB3 files from an IO object or a file path and creates new Book object. # book = self.parse(File.new('some.epub')) - def self.parse(io) + def self.parse(path_or_io) files = {} package = nil package_path = nil book = nil - Zip::File.open_buffer(io) do + Zip::File.open(path_or_io) do |zip_file| package, package_path = parse_container(zip_file, files) check_consistency_of_package(package, package_path) @@ -384,7 +384,7 @@ def self.parse_container(zip_file, files) package = nil zip_file.each do |entry| if !entry.directory? - files[entry.name] = zip_file.read(entry) + files[entry.name] = entry.get_input_stream(&:read) case entry.name when MIMETYPE then if files[MIMETYPE] != MIMETYPE_CONTENTS diff --git a/spec/book_spec.rb b/spec/book_spec.rb index 9f32a28..fb6e3fd 100644 --- a/spec/book_spec.rb +++ b/spec/book_spec.rb @@ -402,6 +402,15 @@ expect(book.items.size).to eq 3 end end + + context 'file path' do + it 'loads book and returns GEPUB::Book object' do + filepath = File.join(File.dirname(__FILE__), 'fixtures', 'testdata', 'wasteland-20120118.epub') + book = GEPUB::Book.parse(filepath) + expect(book).to be_instance_of GEPUB::Book + expect(book.items.size).to eq 6 + end + end end end end diff --git a/spec/gepub_spec.rb b/spec/gepub_spec.rb index f290b5c..2714bc5 100644 --- a/spec/gepub_spec.rb +++ b/spec/gepub_spec.rb @@ -79,8 +79,7 @@ end after do - # workaround; rubyzip opened files could not be deleted with remove_entry_secure on windows. - FileUtils.rm_rf @tempdir + FileUtils.remove_entry_secure @tempdir end it "should have title" do