Class: SmcUtil::FileExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/smcutil/file_extractor.rb

Constant Summary collapse

OUTPUT_FILE_FLAGS =
File::CREAT | File::TRUNC | File::WRONLY

Instance Method Summary collapse

Constructor Details

#initialize(file_reader) ⇒ FileExtractor

Returns a new instance of FileExtractor.



4
5
6
# File 'lib/smcutil/file_extractor.rb', line 4

def initialize(file_reader)
  @file_reader = file_reader
end

Instance Method Details

#extract_to(path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/smcutil/file_extractor.rb', line 9

def extract_to(path)
  File.open(path, OUTPUT_FILE_FLAGS) do |file|
    @file_reader.regions.each do |region|
      range_bytes = region.offset - file.pos
      file.write "\0" * range_bytes if range_bytes > 0
      file.seek region.offset
      file.write region.data
    end
  end
end

#shred_to(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smcutil/file_extractor.rb', line 20

def shred_to(path)
  Dir.mkdir(path) unless Dir.exists? path

  pass = position = 0

  @file_reader.regions.each do |region|
    pass += 1 if region.offset < position
    position = region.offset

    filename = File.join(path, "pass#{pass}_#{region.offset.to_s(16)}.bin")

    File.open(filename, OUTPUT_FILE_FLAGS) do |file|
      file.write region.data
    end
  end
end