lib/axlsx/util/zip_command.rb in caxlsx-3.4.1 vs lib/axlsx/util/zip_command.rb in caxlsx-4.0.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'open3'
require 'shellwords'
module Axlsx
# The ZipCommand class supports zipping the Excel file contents using
@@ -21,14 +23,14 @@
end
# Create a temporary directory for writing files to.
#
# The directory and its contents are removed at the end of the block.
- def open(output, &block)
+ def open(output)
Dir.mktmpdir do |dir|
@dir = dir
- block.call(self)
+ yield(self)
write_file
zip_parts(output)
end
end
@@ -36,36 +38,34 @@
def put_next_entry(entry)
write_file
@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)
output = Shellwords.shellescape(File.absolute_path(output))
inputs = Shellwords.shelljoin(@files)
escaped_dir = Shellwords.shellescape(@dir)
command = "cd #{escaped_dir} && #{@zip_command} #{output} #{inputs}"
stdout_and_stderr, status = Open3.capture2e(command)
- if !status.success?
- raise(ZipError.new(stdout_and_stderr))
+ unless status.success?
+ raise ZipError, stdout_and_stderr
end
end
end
end