Class: SevenZipRuby::SevenZipReader

Inherits:
Object
  • Object
show all
Defined in:
lib/seven_zip_ruby/seven_zip_reader.rb

Overview

SevenZipReader reads 7zip archive and extract it.

Examples

Get archive information

# Archive property
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file) do |szr|
    info = szr.archive_property  # Return ArchiveInfo instance.
  end
end

# Entry information
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file) do |szr|
    entries = szr.entries
  end
end

Extract 7zip archive.

# Extract archive
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file) do |szr|
    szr.extract(:all, "path_to_dir")
  end
end

# Extract encrypted archive
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file, password: "Password String") do |szr|
    szr.extract(:all, "path_to_dir")
  end
end

# Extract only small files
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::Reader.open(file) do |szr|
    small_files = szr.entries.select{ |i| i.file? && i.size < 1024 }
    szr.extract(small_files, "path_to_dir")
  end
end

# Extract archive on memory
archive_data = "....."
stream = StringIO.new(archive_data)
SevenZipRuby::Reader.open(stream) do |szr|
  entry_data = szr.extract_data(:all)
  # => [ "data", ... ]
end

Verify archive

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::Reader.verify(file)
  # => true/false
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract(stream, index, dir = ".", param = {}) ⇒ Object

Open and extract 7zip archive.

Args

stream

Input stream to read 7zip archive. stream.seek and stream.read are needed, such as File and StringIO.

index

Index of the entry to extract. Integer or Array of Integer can be specified.

dir

Directory to extract the archive to.

param

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

Examples

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.extract(file, 1, "path_to_dir")
end

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.extract(file, [1, 2, 4], "path_to_dir", password: "PasswordOfArchive")
end

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.extract(file, :all, "path_to_dir")
end


126
127
128
129
130
131
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 126

def extract(stream, index, dir = ".", param = {})
  password = { password: param.delete(:password) }
  self.open(stream, password) do |szr|
    szr.extract(index, dir, param)
  end
end

.extract_all(stream, dir = ".", param = {}) ⇒ Object

Open and extract 7zip archive.

Args

stream

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

dir

Directory to extract the archive to.

param

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

Examples

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.extract_all(file, "path_to_dir")
end


144
145
146
147
148
149
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 144

def extract_all(stream, dir = ".", param = {})
  password = { password: param.delete(:password) }
  self.open(stream, password) do |szr|
    szr.extract_all(dir, param)
  end
end

.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


95
96
97
98
99
100
101
102
103
104
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 95

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

.verify(stream, opt = {}) ⇒ Object

Open and verify 7zip archive.

Args

stream

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

opt

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

Examples

File.open("filename.7z", "rb") do |file|
  ret = SevenZipRuby::SevenZipReader.verify(file)
  # => true/false
end


162
163
164
165
166
167
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 162

def verify(stream, opt = {})
  szr = self.open(stream, opt)
  ret = szr.verify
  szr.close
  return ret
end

Instance Method Details

#extract(index, dir = ".") ⇒ Object

Extract some entries of 7zip archive to local directory.

Args

index

Index of the entry to extract. Integer or Array of Integer can be specified.

dir

Directory to extract the archive to.

Examples

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    szr.extract([ 1, 2, 4 ], "path_to_dir")
  end
end

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    szr.extract(:all, "path_to_dir")
  end
end


249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 249

def extract(index, dir = ".")
  path = File.expand_path(dir)
  case(index)
  when Symbol
    raise SevenZipError.new("Argument error") unless (index == :all)
    return extract_all(path)
  when Array
    index_list = index.map(&:to_i).sort.uniq
    extract_files_impl(index_list, file_proc(path))
  else
    extract_impl(index.to_i, file_proc(path))
  end
end

#extract_all(dir = ".") ⇒ Object

Extract all entries of 7zip archive to local directory.

Args

dir

Directory to extract the archive to.

Examples

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    szr.extract_all("path_to_dir")
  end
end


274
275
276
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 274

def extract_all(dir = ".")
  extract_all_impl(file_proc(File.expand_path(dir)))
end

#extract_data(index) ⇒ Object

Extract some entries of 7zip archive and return the extracted data.

Args

index

Index of the entry to extract. :all, Integer or Array of Integer can be specified.

Examples

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    small_entries = szr.entries.select{ |i| i.size < 1024 }

    data_list = szr.extract_data(small_entries)
    # => [ "file contents1", "file contents2", ... ]
  end
end

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    largest_entry = szr.entries.max_by{ |i| i.file? ? i.size : 0 }

    data_list = szr.extract_data(largest_entry)
    # => "file contents..."
  end
end


319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 319

def extract_data(index)
  case(index)
  when :all
    idx_prj = Object.new
    def idx_prj.[](index)
      return index
    end

    ret = []
    extract_all_impl(data_proc(ret, idx_prj))
    return ret

  when Array
    index_list = index.map(&:to_i)
    idx_prj = Hash[*(index_list.each_with_index.map{ |idx, i| [ idx, i ] }.flatten)]

    ret = []
    extract_files_impl(index_list, data_proc(ret, idx_prj))
    return ret

  else
    index = index.to_i
    item = entry(index)
    return nil unless (item.has_data?)

    idx_prj = Object.new
    def idx_prj.[](index)
      return 0
    end

    ret = []
    extract_impl(index, data_proc(ret, idx_prj))
    return ret[0]

  end
end

#extract_if(dir = ".", &block) ⇒ Object

Extract entires of 7zip archive to local directory based on the block return value.

Args

dir

Directory to extract the archive to.

Examples

# Extract files whose size is less than 1024.
File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    szr.extract_if("path_to_dir") do |entry|
      next entry.size < 1024
    end
  end
end


292
293
294
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 292

def extract_if(dir = ".", &block)  # :yield: entry_info
  extract(entries.select(&block).map(&:index), dir)
end

#open(stream, param = {}) ⇒ Object

Open 7zip archive.

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

File.open("filename.7z", "rb") do |file|
  szr = SevenZipRuby::SevenZipReader.new
  szr.open(file)
  # ...
  szr.close
end


183
184
185
186
187
188
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 183

def open(stream, param = {})
  param[:password] = param[:password].to_s if (param[:password])
  stream.set_encoding(Encoding::ASCII_8BIT)
  open_impl(stream, param)
  return self
end

#testObject Also known as: verify

Verify 7zip archive.

Args

none

Examples

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    ret = szr.verify
    # => true/false
  end
end


202
203
204
205
206
207
208
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 202

def test
  begin
    return test_all_impl(nil)
  rescue
    return false
  end
end

#verify_detailObject

Verify 7zip archive and return the result of each entry.

Args

none

Examples

File.open("filename.7z", "rb") do |file|
  SevenZipRuby::SevenZipReader.open(file) do |szr|
    ret = szr.verify_detail
    # => [ true, :DataError, :DataError, ... ]
  end
end


223
224
225
226
227
228
229
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 223

def verify_detail
  begin
    return test_all_impl(true)
  rescue
    return nil
  end
end