Skip to content

Commit

Permalink
Merge pull request #222 from amitree/correctly_handle_xlsx_streams
Browse files Browse the repository at this point in the history
Allow Roo::Excelx to open streams, correctly this time
  • Loading branch information
simonoff committed Jun 3, 2015
2 parents ae94acc + 2382b3c commit 16dcb43
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
16 changes: 5 additions & 11 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,13 @@ def process_zipfile(zipfilename_or_stream)
@sheet_files = []

unless is_stream?(zipfilename_or_stream)
process_zipfile_entries Zip::File.open(zipfilename_or_stream).to_a.sort_by(&:name)
zip_file = Zip::File.open(zipfilename_or_stream)
else
stream = Zip::InputStream.open zipfilename_or_stream
begin
entries = []
while (entry = stream.get_next_entry)
entries << entry
end
process_zipfile_entries entries
ensure
stream.close
end
zip_file = Zip::CentralDirectory.new
zip_file.read_from_stream zipfilename_or_stream
end

process_zipfile_entries zip_file.to_a.sort_by(&:name)
end

def process_zipfile_entries(entries)
Expand Down
2 changes: 1 addition & 1 deletion test/test_roo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,7 @@ def test_noexpand_merged_range

def test_open_stream
return unless EXCELX
file_contents = File.read File.join(TESTDIR, fixture_filename(:numbers1, :excelx))
file_contents = File.read File.join(TESTDIR, fixture_filename(:numbers1, :excelx)), encoding: 'BINARY'
stream = StringIO.new(file_contents)
xlsx = Roo::Excelx.new(stream)
assert_equal ["Tabelle1","Name of Sheet 2","Sheet3","Sheet4","Sheet5"], xlsx.sheets
Expand Down

0 comments on commit 16dcb43

Please sign in to comment.