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 archive 2
SevenZipRuby::Reader.open_file("filename.7z") do |szr|
  szr.extract(:all, "path_to_dir")
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

Constant Summary collapse

COMPRESS_GUARD =

:nodoc:

Mutex.new

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


166
167
168
169
170
171
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 166

def extract(stream, index, dir = ".", param = {})
  password = { password: param.delete(:password) }
  self.open(stream, password) do |szr|
    szr.extract(index, dir)
  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


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

def extract_all(stream, dir = ".", param = {})
  password = { password: param.delete(:password) }
  self.open(stream, password) do |szr|
    szr.extract_all(dir)
  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


101
102
103
104
105
106
107
108
109
110
# 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)
    block.call(szr)
    szr.close
  else
    szr
  end
end

.open_file(filename, param = {}, &block) ⇒ Object

Open 7zip archive to read.

Args

filename

Filename of 7zip archive.

param

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

Examples

# Open archive
SevenZipRuby::SevenZipReader.open_file("filename.7z") do |szr|
  # Read and extract archive.
end

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

# Open without block.
szr = SevenZipRuby::SevenZipReader.open_file("filename.7z")
# Read and extract archive.
szr.close


134
135
136
137
138
139
140
141
142
143
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 134

def open_file(filename, param = {}, &block)  # :yield: szr
  szr = self.new
  szr.open_file(filename, 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


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

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

Instance Method Details

#closeObject



249
250
251
252
253
254
255
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 249

def close
  close_impl
  if (@stream)
    @stream.close
    @stream = nil
  end
end

#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


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 320

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 Enumerable
    index_list = index.map(&:to_i).sort.uniq
    synchronize do
      extract_files_impl(index_list, file_proc(path))
    end
  when nil
    raise ArgumentError.new("Invalid parameter index")
  else
    synchronize do
      extract_impl(index.to_i, file_proc(path))
    end
  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


351
352
353
354
355
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 351

def extract_all(dir = ".")
  synchronize do
    extract_all_impl(file_proc(File.expand_path(dir)))
  end
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


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 398

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

    ret = []
    synchronize do
      extract_all_impl(data_proc(ret, idx_prj))
    end
    return ret

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

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

  when nil
    raise ArgumentError.new("Invalid parameter index")

  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 = []
    synchronize do
      extract_impl(index, data_proc(ret, idx_prj))
    end
    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


371
372
373
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 371

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


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

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

#open_file(filename, param = {}) ⇒ Object

Open 7zip archive file.

Args

filename

Filename of 7zip archive.

param

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

Examples

szr = SevenZipRuby::SevenZipReader.new
szr.open_file("filename.7z")
# ...
szr.close


243
244
245
246
247
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 243

def open_file(filename, param = {})
  @stream = File.open(filename, "rb")
  self.open(@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


269
270
271
272
273
274
275
276
277
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 269

def test
  begin
    synchronize do
      return test_all_impl(nil)
    end
  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


292
293
294
295
296
297
298
299
300
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 292

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