class File
Copyright © 2006-2025 Hal Brodigan (postmodern.mod3 at gmail.com)
ronin-support is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
ronin-support is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with ronin-support. If not, see <www.gnu.org/licenses/>.
Public Class Methods
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 517 def self.aes128_decrypt(path, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.aes128_cipher(direction: :decrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Decrypts the file using AES-128.
@param [String] path
The path to the file.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the AES decrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.aes128_cipher}.
@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)
The desired AES cipher mode.
@option kwargs [Symbol] :hash (:md5)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@return [String]
The encrypted data.
@raise [ArgumentError]
Either the `key:` or `password:` keyword argument must be given.
@example
File.aes256_decrypt('encrypted.bin', password: 's3cr3t') # => "..."
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 464 def self.aes128_encrypt(path, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.aes128_cipher(direction: :encrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Encrypts the file using AES-128.
@param [String] path
The path to the file.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the AES encrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.aes128_cipher}.
@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)
The desired AES cipher mode.
@option kwargs [Symbol] :hash (:md5)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@return [String]
The encrypted data.
@raise [ArgumentError]
Either the `key:` or `password:` keyword argument must be given.
@example
File.aes128_encrypt('file.txt', password: 's3cr3t') # => "..."
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 623 def self.aes256_decrypt(path, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.aes256_cipher(direction: :decrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Decrypts the file using AES-256.
@param [String] path
The path to the file.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the AES decrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.aes256_cipher}.
@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)
The desired AES cipher mode.
@option kwargs [Symbol] :hash (:sha256)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@return [String]
The encrypted data.
@raise [ArgumentError]
Either the `key:` or `password:` keyword argument must be given.
@example
File.aes256_decrypt('encrypted.bin', password: 's3cr3t') # => "..."
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 570 def self.aes256_encrypt(path, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.aes256_cipher(direction: :encrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Encrypts the file using AES-256.
@param [String] path
The path to the file.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the AES encrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.aes256_cipher}.
@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)
The desired AES cipher mode.
@option kwargs [Symbol] :hash (:sha256)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@return [String]
The encrypted data.
@raise [ArgumentError]
Either the `key:` or `password:` keyword argument must be given.
@example
File.aes256_encrypt('file.txt', password: 's3cr3t') # => "..."
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 411 def self.aes_decrypt(path, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.aes_cipher(direction: :decrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Decrypts the file using AES.
@param [String] path
The path to the file.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the AES decrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.aes_cipher}.
@option kwargs [Integer] :key_size
The desired key size in bits.
@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)
The desired AES cipher mode.
@option kwargs [Symbol] :hash (:sha256)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@return [String]
The encrypted data.
@raise [ArgumentError]
Either the `key:` or `password:` keyword argument must be given.
@example
File.aes_decrypt('encrypted.bin', key_size: 256, password: 's3cr3t') # => "..."
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 355 def self.aes_encrypt(path, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.aes_cipher(direction: :encrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Encrypts the file using AES.
@param [String] path
The path to the file.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the AES encrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.aes_cipher}.
@option kwargs [Integer] :key_size
The desired key size in bits.
@option kwargs [:cbc, :cfb, :ofb, :ctr, Symbol] mode (:cbc)
The desired AES cipher mode.
@option kwargs [Symbol] :hash (:sha256)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@return [String]
The encrypted data.
@raise [ArgumentError]
Either the `key:` or `password:` keyword argument must be given.
@example
File.aes_encrypt('file.txt', key_size: 256, password: 's3cr3t') # => "..."
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 298 def self.decrypt(path,cipher, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.cipher(cipher, direction: :decrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Decrypts the file.
@param [String] path
The path to the file.
@param [String] cipher
The cipher to use.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the decrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.cipher}.
@option kwargs [Symbol] :hash (:sha256)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@yield [block]
If a block is given, each encrypted block will be passed to it.
@yieldparam [String] block
An encrypted block from the file.
@return [String]
The decrypted data.
@example
File.decrypt('encrypted.bin', 'aes-256-cbc', password: 's3cr3t') # => "..."
@see rubydoc.info/stdlib/openssl/OpenSSL/Cipher
@since 0.6.0
@api public
Source
# File lib/ronin/support/core_ext/file.rb, line 45 def self.each_line(path) return enum_for(__method__,path) unless block_given? foreach(path) { |line| yield line.chomp } end
Reads each line from the file.
@param [String] path
The path of the file.
@yield [line]
The given block will be passed each line.
@yieldparam [String] line
A line from the file, with the trailing newline characters removed.
@return [Enumerator]
If no block is given, an Enumerator will be returned.
@example
File.each_line('passwords.txt') do |line| # ... end
@since 0.3.0
@api public
Source
# File lib/ronin/support/core_ext/file.rb, line 78 def self.each_row(path,separator=/\s+/) return enum_for(__method__,path,separator) unless block_given? each_line(path) { |line| yield line.split(separator) } end
Reads each row from the file.
@param [String] path
The path of the file.
@param [Regexp, String] separator
The pattern to split the line by.
@yield [row]
The given block will be passed each row.
@yieldparam [Array<String>] row
A row from the file.
@return [Enumerator]
If no block is given, an Enumerator will be returned.
@example
File.each_row('db_dump.txt', '|') do |row| # ... end
@since 0.3.0
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 237 def self.encrypt(path,cipher, block_size: 16384, output: nil, **kwargs,&block) cipher = Ronin::Support::Crypto.cipher(cipher, direction: :encrypt, **kwargs) file = File.open(path,'rb') return cipher.stream(file, block_size: block_size, output: output,&block) end
Encrypts the file.
@param [String] path
The path to the file.
@param [String] cipher
The cipher to use.
@param [Integer] block_size
Reads data from the file in chunks of the given block size.
@param [String, <<, nil] output
The optional output buffer to append the encrypted data to. Defaults to an empty ASCII 8bit encoded String.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.cipher}.
@option kwargs [Symbol] :hash (:sha256)
The algorithm to hash the `:password`.
@option kwargs [String] :key
The secret key to use.
@option kwargs [String] :password
The password for the cipher.
@option kwargs [String] :iv
The optional Initial Vector (IV).
@option kwargs [Integer] :padding
Sets the padding for the cipher.
@yield [block]
If a block is given, each encrypted block will be passed to it.
@yieldparam [String] block
An encrypted block from the file.
@return [String]
The encrypted data.
@example
File.encrypt('file.txt', 'aes-256-cbc', password: 's3cr3t') # => "..."
@see rubydoc.info/stdlib/openssl/OpenSSL/Cipher
@since 0.6.0
@api public
Source
# File lib/ronin/support/core_ext/file.rb, line 95 def self.escape_path(path) path = path.to_s # remove any \0 characters first path.tr!("\0",'') # remove any home-dir expansions path.gsub!('~',"\\~") path = expand_path(File.join('/',path)) # remove the leading slash return path[1..] end
Escapes a path.
@param [String] path
Unescaped path.
@return [String]
The escaped path.
@api public
Source
# File lib/ronin/support/compression/core_ext/file.rb, line 50 def self.gunzip(path,&block) Ronin::Support::Compression::Gzip::Reader.open(path,&block) end
Opens the gzipped file for reading.
@param [String] path
The path to the file to read.
@yield [gz]
If a block is given, it will be passed the gzip reader object.
@yieldparam [Ronin::Support::Compression::Gzip::Reader] gz
The gzip reader object.
@return [Ronin::Support::Compression::Gzip::Reader]
The gzip reader object.
@example
File.gunzip('wordlist.gz') do |gz| gz.each_line do |line| # ... end end
@api public
@since 1.0.0
Source
# File lib/ronin/support/compression/core_ext/file.rb, line 82 def self.gzip(path,&block) Ronin::Support::Compression::Gzip::Writer.open(path,&block) end
Opens the gzip file for writing.
@param [String] path
The path to the file to write to.
@yield [gz]
If a block is given, it will be passed the gzip writer object.
@yieldparam [Ronin::Support::Compression::Gzip::Writer] gz
The gzip writer object.
@return [Ronin::Support::Compression::Gzip::Writer]
The gzip writer object.
@example
File.gunzip('file.gz') do |gz| # gzip header info gz.mtime = Time.now gz.orig_name = 'file.txt' gz.write('Hello World!') end
@api public
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 172 def self.hmac(path, key: , digest: :sha1) hmac = Ronin::Support::Crypto.hmac(key: key, digest: digest) File.open(path,'rb') do |file| until file.eof? hmac.update(file.read(16384)) end end return hmac.hexdigest end
Calculates the HMAC for a file.
@param [String] path
The path to the file.
@param [String] key
The secret key for the HMAC.
@param [Symbol] digest
The digest algorithm for the HMAC.
@return [String]
The hex-encoded HMAC for the String.
@see Ronin::Support::Crypt.hmac
@since 0.6.0
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 40 def self.md5(path) Digest::MD5.file(path).hexdigest end
Calculates the MD5 checksum of a file.
@param [String] path
The path to the file.
@return [String]
The MD5 checksum of the file.
@example
File.md5('data.txt') # => "5d41402abc4b2a76b9719d911017c592"
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 147 def self.rmd160(path) Digest::RMD160.file(path).hexdigest end
Calculates the RMD160 checksum for the File
.
@param [String] path
The path to the file.
@return [String]
The RMD160 checksum of the File.
@example
File.rmd160('data.txt') # => "108f07b8382412612c048d07d13f814118445acd"
@api public
@since 0.6.0
@note JRuby and TruffleRuby do not yet support RMD160.
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 694 def self.rsa_decrypt(path,**kwargs) Ronin::Support::Crypto.rsa_decrypt(File.binread(path),**kwargs) end
Decrypts the file using the given RSA key.
@param [String] path
The path to the file.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.rsa_decrypt}.
@option kwargs [String, nil] :key
The PEM or DER encoded RSA key string.
@option kwargs [String, nil] :key_file
The path to the PEM or DER encoded RSA key file.
@option kwargs [String, nil] :key_password
The optional password to decrypt the encrypted RSA key.
@option kwargs [:pkcs1_oaep, :pkcs1, :sslv23,
nil, false] :padding (:pkcs1) Optional padding mode. `nil` and `false` will disable padding.
@return [String]
The decrypted data.
@raise [ArgumentError]
Either the `key:` or `key_file:` keyword argument must be given.
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 660 def self.rsa_encrypt(path,**kwargs) Ronin::Support::Crypto.rsa_encrypt(File.binread(path),**kwargs) end
Encrypts the file using the given RSA key.
@param [String] path
The path to the file.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Crypto.rsa_encrypt}.
@option kwargs [String, nil] :key
The PEM or DER encoded RSA key string.
@option kwargs [String, nil] :key_file
The path to the PEM or DER encoded RSA key file.
@option kwargs [String, nil] :key_password
The optional password to decrypt the encrypted RSA key.
@option kwargs [:pkcs1_oaep, :pkcs1, :sslv23,
nil, false] :padding (:pkcs1) Optional padding mode. `nil` and `false` will disable padding.
@return [String]
The encrypted data.
@raise [ArgumentError]
Either the `key:` or `key_file:` keyword argument must be given.
@since 1.0.0
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 59 def self.sha1(path) Digest::SHA1.file(path).hexdigest end
Calculates the SHA1 checksum of a file.
@param [String] path
The path to the file.
@return [String]
The SHA1 checksum of the file.
@example
File.sha1('data.txt') # => "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 68 def self.sha128(path) File.sha1(path) end
@see File.sha1
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 96 def self.sha2(path) File.sha256(path) end
@see File.sha256
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 87 def self.sha256(path) Digest::SHA256.file(path).hexdigest end
Calculates the SHA256 checksum of a file.
@param [String] path
The path to the file.
@return [String]
The SHA256 checksum of the file.
@example
File.sha256('data.txt') # => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 124 def self.sha5(path) File.sha512(path) end
@see File.sha512
@api public
Source
# File lib/ronin/support/crypto/core_ext/file.rb, line 115 def self.sha512(path) Digest::SHA512.file(path).hexdigest end
Calculates the SHA512 checksum of a file.
@param [String] path
The path to the file.
@return [String]
The SHA512 checksum of the file.
@example
File.sha512('data.txt') # => "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"
@api public
Source
# File lib/ronin/support/archive/core_ext/file.rb, line 90 def self.tar(path,&block) Ronin::Support::Archive::Tar::Writer.open(path,&block) end
Opens the tar archive file for writing.
@param [String] path
The path to the file to write to.
@yield [tar]
If a block is given, it will be passed the tar writer object.
@yieldparam [Ronin::Support::Archive::Tar::Writer] tar
The tar writer object.
@return [Ronin::Support::Archive::Tar::Writer]
The tar writer object.
@example
File.tar('output.tar') do |tar| tar.mkdir('foo') tar.add_file('foo/bar.txt','Hello World!') end
@api public
@since 1.0.0
Source
# File lib/ronin/support/binary/unhexdump/core_ext/file.rb, line 125 def self.unhexdump(path,**kwargs) parser = Ronin::Support::Binary::Unhexdump::Parser.new(**kwargs) parser.unhexdump(new(path)) end
Converts a hexdump file to it’s original binary data.
@param [Pathname, String] path
The path of the hexdump file.
@param [Hash{Symbol => Object}] kwargs
Additional keyword arguments for {Ronin::Support::Binary::Unhexdump::Parser#initialize}.
@option kwargs [:od, :hexdump] :format (:hexdump)
The expected format of the hexdump. Must be either `:od` or `:hexdump`.
@option kwargs [Symbol] :type (:byte)
Denotes the encoding used for the bytes within the hexdump. Must be one of the following: * `:byte` * `:char` * `:uint8` * `:uint16` * `:uint32` * `:uint64` * `:int8` * `:int16` * `:int32` * `:int64` * `:uchar` * `:ushort` * `:uint` * `:ulong` * `:ulong_long` * `:short` * `:int` * `:long` * `:long_long` * `:float` * `:double` * `:float_le` * `:double_le` * `:float_be` * `:double_be` * `:uint16_le` * `:uint32_le` * `:uint64_le` * `:int16_le` * `:int32_le` * `:int64_le` * `:uint16_be` * `:uint32_be` * `:uint64_be` * `:int16_be` * `:int32_be` * `:int64_be` * `:ushort_le` * `:uint_le` * `:ulong_le` * `:ulong_long_le` * `:short_le` * `:int_le` * `:long_le` * `:long_long_le` * `:ushort_be` * `:uint_be` * `:ulong_be` * `:ulong_long_be` * `:short_be` * `:int_be` * `:long_be` * `:long_long_be`
@option kwargs [2, 8, 10, 16, nil] :address_base
The numerical base that the offset addresses are encoded in.
@option kwargs [2, 8, 10, 16, nil] base
The numerical base that the hexdumped numbers are encoded in.
@option kwargs [Boolean] named_chars
Indicates to parse `od`-style named characters (ex: `nul`, `del`, etc).
@return [String]
The raw-data from the hexdump.
@raise [ArgumentError]
Unsupported `type:` value, or the `format:` was not `:hexdump` or `:od`.
@example Unhexdump a hexdump created by GNU ‘hexdump -C`:
File.unhexdump('hexdump.txt') # => "hello\n"
@example Unhexdump a hexdump created by GNU ‘hexdump`:
File.unhexdump('hexdump.txt', type: :uint16_le) # => "hello\n"
@example Unhexdump a hexdump created by ‘od`:
File.unhexdump('od.txt', format: :od) # => "hello\n"
@api public
Source
# File lib/ronin/support/archive/core_ext/file.rb, line 61 def self.untar(path,&block) Ronin::Support::Archive::Tar::Reader.open(path,&block) end
Opens the tar archive file for reading.
@param [String] path
The path to the file to read.
@yield [tar]
If a block is given, it will be passed the tar reader object.
@yieldparam [Ronin::Support::Archive::Tar::Reader] tar
The tar reader object.
@return [Ronin::Support::Archive::Tar::Reader]
The tar reader object.
@example Enumerating over each entry in the tar archive:
File.untar('file.tar') do |tar| tar.each do |entry| puts entry.full_name puts '-' * 80 puts entry.read puts '-' * 80 end end
@example Reads a specific file from the tar archive:
File.untar('file.tar') do |tar| data = tar.read('foo.txt') # ... end
@api public
@since 1.0.0
Source
# File lib/ronin/support/archive/core_ext/file.rb, line 129 def self.unzip(path,&block) Ronin::Support::Archive::Zip::Reader.open(path,&block) end
Opens the zip archive file for reading.
@param [String] path
The path to the file to read.
@yield [zip]
If a block is given, it will be passed the zip reader object.
@yieldparam [Ronin::Support::Archive::Zip::Reader] zip
The zip reader object.
@return [Ronin::Support::Archive::Zip::Reader]
The zip reader object.
@example Enumerating over each file in a zip archive:
File.unzip('file.zip') do |zip| zip.each do |entry| puts entry.name puts '-' * 80 puts entry.read puts '-' * 80 end end
@example Reads a specific file from a zip archive:
File.unzip('file.zip') do |zip| data = zip.read('foo.txt') # ... end
@api public
@since 1.0.0
Source
# File lib/ronin/support/archive/core_ext/file.rb, line 157 def self.zip(path,&block) Ronin::Support::Archive::Zip::Writer.open(path,&block) end
Opens the zip archive file for writing.
@param [String] path
The path to the file to write to.
@yield [zip]
If a block is given, it will be passed the zip writer object.
@yieldparam [Ronin::Support::Archive::Zip::Writer] zip
The zip writer object.
@return [Ronin::Support::Archive::Zip::Writer]
The zip writer object.
@example
File.zip('output.zip') do |zip| zip.add_file('foo/bar.txt','Hello World!') end
@api public
@since 1.0.0