Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close entries streams after read on parse #142

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/gepub/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions spec/gepub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down