Skip to content

Commit

Permalink
Merge pull request #142 from artygus/fix-parser-file-handler-leak-on-…
Browse files Browse the repository at this point in the history
…windows

Close entries streams after read on parse
  • Loading branch information
skoji authored May 15, 2024
2 parents c29a1be + 63c6647 commit 00bd03e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
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

0 comments on commit 00bd03e

Please sign in to comment.