class Axlsx::ZipCommand
The ZipCommand
class supports zipping the Excel file contents using a binary zip program instead of RubyZip's `Zip::OutputStream`.
The methods provided here mimic `Zip::OutputStream` so that `ZipCommand` can be used as a drop-in replacement. Note that method signatures are not identical to `Zip::OutputStream`, they are only sufficiently close so that `ZipCommand` and `Zip::OutputStream` can be interchangeably used within `caxlsx`.
Public Class Methods
new(zip_command)
click to toggle source
# File lib/axlsx/util/zip_command.rb, line 19 def initialize(zip_command) @current_file = nil @files = [] @zip_command = zip_command end
Public Instance Methods
open(output, &block)
click to toggle source
Create a temporary directory for writing files to.
The directory and its contents are removed at the end of the block.
# File lib/axlsx/util/zip_command.rb, line 28 def open(output, &block) Dir.mktmpdir do |dir| @dir = dir block.call(self) write_file zip_parts(output) end end
put_next_entry(entry)
click to toggle source
Closes the current entry and opens a new for writing.
# File lib/axlsx/util/zip_command.rb, line 38 def put_next_entry(entry) write_file @current_file = "#{@dir}/#{entry.name}" @files << entry.name FileUtils.mkdir_p(File.dirname(@current_file)) end
write(content)
click to toggle source
Write to a buffer that will be written to the current entry
# File lib/axlsx/util/zip_command.rb, line 46 def write(content) @buffer << content end
Also aliased as: <<
Private Instance Methods
write_file()
click to toggle source
# File lib/axlsx/util/zip_command.rb, line 53 def write_file if @current_file @buffer.rewind File.open(@current_file, "wb") { |f| f.write @buffer.read } end @current_file = nil @buffer = StringIO.new end
zip_parts(output)
click to toggle source
# File lib/axlsx/util/zip_command.rb, line 62 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)) end end