Method: SevenZipRuby::SevenZipReader.open

Defined in:
lib/seven_zip_ruby/seven_zip_reader.rb

.open(stream, param = {}, &block) ⇒ Object

Open 7zip archive to read.

Args

stream

Input stream to read 7zip archive. stream.seek and stream.read are needed.

param

Optional hash parameter. :password key represents password of this archive.

Examples

# Open archive
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    # Read and extract archive.
  end
end

# Open encrypted archive
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file, password: "PasswordOfArchive") do |szr|
    # Read and extract archive.
  end
end

# Open without block.
File.open("filename.7z", "rb") do |file|
  szr = SevenZipRuby::SevenZipReader.open(file)
  # Read and extract archive.
  szr.close
end

# Open archive on memory.
archive_data = "....."
stream = StringIO.new(archive_data)
SevenZipRuby::Reader.open(stream) do |szr|
  szr.extract(:all, "path_to_dir")
end

101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 101

def open(stream, param = {}, &block)  # :yield: szr
  szr = self.new
  szr.open(stream, param)
  if (block)
    begin
      block.call(szr)
      szr.close
    ensure
      szr.close_file
    end
  else
    szr
  end
end