class Ronin::Support::Archive::Zip::Writer
Handles creating zip archives.
@note
This provides a simple interface for creating zip archives using the `zip` command. If you need something more powerful, use the [archive-zip] gem instead.
[archive-zip]: github.com/javanthropus/archive-zip
@api public
@since 1.0.0
Attributes
The optional password for the zip archive.
@return [String, nil]
The path to the zip archive.
@return [String]
The temp directory where the contents of the zip archive will be written into before zipping.
@return [String]
@api private
Public Class Methods
Initializes the zip writer.
@param [String] path
The path to the zip archive.
@param [String, nil] password
The optional password for the zip archive.
@yield [self]
If a block is given it will be passed the zip archive writer.
# File lib/ronin/support/archive/zip/writer.rb, line 72 def initialize(path, password: nil) @path = File.expand_path(path) @password = password @tempdir = Dir.mktmpdir('ronin-support') if block_given? yield self close end end
Alias for {new}.
@param [String] path
The path to the zip archive.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {new}.
@option kwargs [String, nil] :password
The optional password for the zip archive.
@yield [zip]
If a block is given it will be passed the zip archive writer.
@yieldparam [Writer] zip
The newly created zip archive writer.
@return [Writer]
The newly created zip archive writer.
@see new
# File lib/ronin/support/archive/zip/writer.rb, line 107 def self.open(path,**kwargs,&block) new(path,**kwargs,&block) end
Public Instance Methods
Adds a file to the zip archive.
@param [String] name
The relative path to the file.
@param [String, nil] contents
The optional contents for the file.
@yield [file]
If a block is given, it will be passed the opened file for writing.
@yieldparam [File] file
The opened file for writing.
# File lib/ronin/support/archive/zip/writer.rb, line 127 def add_file(name,contents=nil,&block) file_path = File.join(@tempdir,name) if contents File.write(file_path,contents) else File.open(file_path,'w',&block) end end
Cleanup the zip archive’s {#tempdir}.
@api private
# File lib/ronin/support/archive/zip/writer.rb, line 159 def cleanup FileUtils.rm_r(@tempdir) end
Closes the zip archive.
# File lib/ronin/support/archive/zip/writer.rb, line 166 def close save cleanup end
Saves the contents of the zip archive to {#path}.
@api private
# File lib/ronin/support/archive/zip/writer.rb, line 142 def save Dir.chdir(@tempdir) do args = ['-q'] if @password args << '-P' << @password end system('zip',*args,'-r',@path,'.') end end