Skip to content

Instantly share code, notes, and snippets.

@rylwin
Created August 8, 2020 23:36
Show Gist options
  • Save rylwin/013eaa9768e94b75572d34e1695ac472 to your computer and use it in GitHub Desktop.
Save rylwin/013eaa9768e94b75572d34e1695ac472 to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "caxlsx"
gem "benchmark"
end
require "axlsx"
class SystemZip
attr_reader :xlsx_file
def initialize
@dir = "/tmp/zip"
@xlsx_file = "/tmp/file.xlsx"
end
def <<(string)
@buffer << string
end
def put_next_entry(entry)
write_file
@file = "#{@dir}/#{entry.name}"
`mkdir -p $(dirname #{@file})`
end
def write(content)
@buffer << content
end
def to_zip
write_file
`cd #{@dir} && find -type f | xargs zip #{@xlsx_file}`
end
private
def write_file
if @file
@buffer.rewind
File.open(@file, "wb") { |f| f.write @buffer.read }
end
@file = nil
@buffer = StringIO.new
end
end
def use_system_zip(package)
zip = SystemZip.new
package.send(:write_parts, zip)
zip.to_zip
File.read(zip.xlsx_file)
end
values = ["Cell 1", "Cell 2", "Cell 3"]
package = Axlsx::Package.new
sheet = package.workbook.add_worksheet(name: "Sheet")
10_000.times{ sheet.add_row(values) }
Benchmark.bmbm(10) do |x|
x.report("built-in:") { package.to_stream.read }
x.report("system-zip:") { use_system_zip(package) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment