Class: Ronin::Support::Archive::Zip::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/archive/zip/writer.rb

Overview

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.

Handles creating zip archives.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, password: nil) {|self| ... } ⇒ Writer

Initializes the zip writer.

Parameters:

  • path (String)

    The path to the zip archive.

  • password (String, nil) (defaults to: nil)

    The optional password for the zip archive.

Yields:

  • (self)

    If a block is given it will be passed the zip archive writer.

Since:

  • 1.0.0



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.expand_path(path)
  @password = password

  @tempdir  = Dir.mktmpdir('ronin-support')

  if block_given?
    yield self
    close
  end
end

Instance Attribute Details

#passwordString? (readonly)

The optional password for the zip archive.

Returns:

Since:

  • 1.0.0



50
51
52
# File 'lib/ronin/support/archive/zip/writer.rb', line 50

def password
  @password
end

#pathString (readonly)

The path to the zip archive.

Returns:

Since:

  • 1.0.0



45
46
47
# File 'lib/ronin/support/archive/zip/writer.rb', line 45

def path
  @path
end

#tempdirString (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.

Returns:

Since:

  • 1.0.0



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

Parameters:

Options Hash (**kwargs):

  • :password (String, nil)

    The optional password for the zip archive.

Yields:

  • (zip)

    If a block is given it will be passed the zip archive writer.

Yield Parameters:

  • zip (Writer)

    The newly created zip archive writer.

Returns:

  • (Writer)

    The newly created zip archive writer.

See Also:

Since:

  • 1.0.0



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.

Parameters:

  • name (String)

    The relative path to the file.

  • contents (String, nil) (defaults to: nil)

    The optional contents for the file.

Yields:

  • (file)

    If a block is given, it will be passed the opened file for writing.

Yield Parameters:

  • file (File)

    The opened file for writing.

Since:

  • 1.0.0



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

#cleanupObject

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.

Since:

  • 1.0.0



159
160
161
# File 'lib/ronin/support/archive/zip/writer.rb', line 159

def cleanup
  FileUtils.rm_r(@tempdir)
end

#closeObject

Closes the zip archive.

Since:

  • 1.0.0



166
167
168
169
# File 'lib/ronin/support/archive/zip/writer.rb', line 166

def close
  save
  cleanup
end

#saveObject

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.

Since:

  • 1.0.0



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