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


174
175
176
177
178
179
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 174

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


192
193
194
195
196
197
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 192

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
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

.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


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 138

def open_file(filename, param = {}, &block)  # :yield: szr
  szr = self.new
  szr.open_file(filename, param)
  if (block)
    begin
      block.call(szr)
      szr.close
    ensure
      szr.close_file
    end
  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


210
211
212
213
214
215
216
217
218
219
220
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 210

def verify(stream, opt = {})
  ret = false
  begin
    self.open(stream, opt) do |szr|
      ret = szr.verify
    end
  rescue
    ret = false
  end
  return ret
end

Instance Method Details

#closeObject



263
264
265
266
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 263

def close
  close_impl
  close_file
end

#close_fileObject

:nodoc:



268
269
270
271
272
273
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 268

def close_file  # :nodoc:
  if (@stream)
    @stream.close rescue nil
    @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


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 338

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


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

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


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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 416

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


389
390
391
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 389

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


238
239
240
241
242
243
244
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 238

def open(stream, param = {})
  param = param.clone
  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


257
258
259
260
261
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 257

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


287
288
289
290
291
292
293
294
295
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 287

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


310
311
312
313
314
315
316
317
318
# File 'lib/seven_zip_ruby/seven_zip_reader.rb', line 310

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