Class: Excavate::FileMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/excavate/file_magic.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileMagic

Returns a new instance of FileMagic.



7
8
9
# File 'lib/excavate/file_magic.rb', line 7

def initialize(path)
  @path = path
end

Class Method Details

.detect(path) ⇒ Object



3
4
5
# File 'lib/excavate/file_magic.rb', line 3

def self.detect(path)
  new(path).detect
end

Instance Method Details

#detectObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/excavate/file_magic.rb', line 11

def detect
  beginning = File.read(@path, 8, mode: "rb")
  case beginning
  when "MSCF\x00\x00\x00\x00".force_encoding("BINARY")
    :cab
  else
    case beginning.byteslice(0, 2)
    when "\x1F\x8B".force_encoding("BINARY")
      :gzip
    end
  end
end