From a4a12edab461400da629a42b2847a2242c096d91 Mon Sep 17 00:00:00 2001 From: Paul Kmiec Date: Sat, 29 Apr 2023 16:23:17 -0700 Subject: Write directly to file io instead of buffering the output in memory --- lib/axlsx/util/buffered_zip_output_stream.rb | 10 +++++----- lib/axlsx/util/zip_command.rb | 10 ++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/axlsx/util/buffered_zip_output_stream.rb b/lib/axlsx/util/buffered_zip_output_stream.rb index 0025e9e2..b348dc55 100644 --- a/lib/axlsx/util/buffered_zip_output_stream.rb +++ b/lib/axlsx/util/buffered_zip_output_stream.rb @@ -18,19 +18,19 @@ module Axlsx # Create a temporary directory for writing files to. # # The directory and its contents are removed at the end of the block. - def self.open(file_name, encrypter = nil, &block) + def self.open(file_name, encrypter = nil) Zip::OutputStream.open(file_name, encrypter) do |zos| bzos = new(zos) - block.call(bzos) + yield(bzos) ensure bzos.flush if bzos end end - def self.write_buffer(io = ::StringIO.new, encrypter = nil, &block) + def self.write_buffer(io = ::StringIO.new, encrypter = nil) Zip::OutputStream.write_buffer(io, encrypter) do |zos| bzos = new(zos) - block.call(bzos) + yield(bzos) ensure bzos.flush if bzos end @@ -51,7 +51,7 @@ module Axlsx alias << write def flush - return if @buffer.size == 0 + return if @buffer.empty? @zos << @buffer @buffer.clear diff --git a/lib/axlsx/util/zip_command.rb b/lib/axlsx/util/zip_command.rb index 00faa942..e43ba03d 100644 --- a/lib/axlsx/util/zip_command.rb +++ b/lib/axlsx/util/zip_command.rb @@ -40,23 +40,21 @@ module Axlsx @current_file = "#{@dir}/#{entry.name}" @files << entry.name FileUtils.mkdir_p(File.dirname(@current_file)) + @io = File.open(@current_file, "wb") end # Write to a buffer that will be written to the current entry def write(content) - @buffer << content + @io << content end alias << write private def write_file - if @current_file - @buffer.rewind - File.open(@current_file, "wb") { |f| f.write @buffer.read } - end + @io.close if @current_file @current_file = nil - @buffer = StringIO.new + @io = nil end def zip_parts(output) -- cgit v1.2.3