diff options
| author | Paul Kmiec <[email protected]> | 2023-04-29 16:23:17 -0700 |
|---|---|---|
| committer | Paul Kmiec <[email protected]> | 2023-05-05 09:32:55 -0700 |
| commit | a4a12edab461400da629a42b2847a2242c096d91 (patch) | |
| tree | 7851f5c3d2ed7369241baed70b331998d1a6c0e0 | |
| parent | e5a8faaa70337ffad6ee2a99e00c0ea3b3c69ca0 (diff) | |
| download | caxlsx-a4a12edab461400da629a42b2847a2242c096d91.tar.gz caxlsx-a4a12edab461400da629a42b2847a2242c096d91.zip | |
Write directly to file io instead of buffering the output in memory
| -rw-r--r-- | lib/axlsx/util/buffered_zip_output_stream.rb | 10 | ||||
| -rw-r--r-- | 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) |
