Class: Ronin::Support::Archive::Zip::Writer
- Inherits:
-
Object
- Object
- Ronin::Support::Archive::Zip::Writer
- Defined in:
- lib/ronin/support/archive/zip/writer.rb
Overview
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.
Handles creating zip archives.
Instance Attribute Summary collapse
-
#password ⇒ String?
readonly
The optional password for the zip archive.
-
#path ⇒ String
readonly
The path to the zip archive.
-
#tempdir ⇒ String
readonly
private
The temp directory where the contents of the zip archive will be written into before zipping.
Class Method Summary collapse
-
.open(path, **kwargs) {|zip| ... } ⇒ Writer
Alias for new.
Instance Method Summary collapse
-
#add_file(name, contents = nil) {|file| ... } ⇒ Object
Adds a file to the zip archive.
-
#cleanup ⇒ Object
private
Cleanup the zip archive's #tempdir.
-
#close ⇒ Object
Closes the zip archive.
-
#initialize(path, password: nil) {|self| ... } ⇒ Writer
constructor
Initializes the zip writer.
-
#save ⇒ Object
private
Saves the contents of the zip archive to #path.
Constructor Details
#initialize(path, password: nil) {|self| ... } ⇒ Writer
Initializes the zip writer.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ronin/support/archive/zip/writer.rb', line 72 def initialize(path, password: nil) @path = File.(path) @password = password @tempdir = Dir.mktmpdir('ronin-support') if block_given? yield self close end end |
Instance Attribute Details
#password ⇒ String? (readonly)
The optional password for the zip archive.
50 51 52 |
# File 'lib/ronin/support/archive/zip/writer.rb', line 50 def password @password end |
#path ⇒ String (readonly)
The path to the zip archive.
45 46 47 |
# File 'lib/ronin/support/archive/zip/writer.rb', line 45 def path @path end |
#tempdir ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The temp directory where the contents of the zip archive will be written into before zipping.
58 59 60 |
# File 'lib/ronin/support/archive/zip/writer.rb', line 58 def tempdir @tempdir end |
Class Method Details
.open(path, **kwargs) {|zip| ... } ⇒ Writer
Alias for Ronin::Support::Archive::Zip.new.
107 108 109 |
# File 'lib/ronin/support/archive/zip/writer.rb', line 107 def self.open(path,**kwargs,&block) new(path,**kwargs,&block) end |
Instance Method Details
#add_file(name, contents = nil) {|file| ... } ⇒ Object
Adds a file to the zip archive.
127 128 129 130 131 132 133 134 135 |
# 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 ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Cleanup the zip archive's #tempdir.
159 160 161 |
# File 'lib/ronin/support/archive/zip/writer.rb', line 159 def cleanup FileUtils.rm_r(@tempdir) end |
#close ⇒ Object
Closes the zip archive.
166 167 168 169 |
# File 'lib/ronin/support/archive/zip/writer.rb', line 166 def close save cleanup end |
#save ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Saves the contents of the zip archive to #path.
142 143 144 145 146 147 148 149 150 151 152 |
# 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 |