Module: Unpacker

Defined in:
lib/unpacker.rb

Defined Under Namespace

Classes: UnrecognizedArchiveError

Constant Summary collapse

SUPPORTED_FILEEXTS =
%w[tar rar zip gz bz tgz bgz tar]

Class Method Summary collapse

Class Method Details

.archive?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/unpacker.rb', line 30

def self.archive?(file_name)
  SUPPORTED_FILEEXTS.include? File.extname(file_name).sub('.', '')
end

.unpack(file, tmpdir = "/tmp", &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/unpacker.rb', line 11

def self.unpack(file, tmpdir = "/tmp", &block)
  Dir.mktmpdir 'unpacker' do |dir|
    case file
    when /rar$/
      Shellshot.exec [ "unrar", "x", "-y", file, dir ], :stdout => false
    when /(tar|tgz|tar\.gz|tar\.bz|tbz)$/
      Shellshot.exec [ "tar", "xf", file, "--directory", dir ], :stdout => false
    when /zip$/
      Shellshot.exec [ "unzip", file, "-d", dir ], :stdout => false
    when /gz$/
      Shellshot.exec [ "gunzip", "-c", file ], :stdout => File.join(dir, "gz-contents")
    else
      raise UnrecognizedArchiveError
    end

    block.call(Dir.new(dir))
  end
end

.valid?(file_path, file_name = file_path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/unpacker.rb', line 34

def self.valid?(file_path, file_name = file_path)
  cmd = case file_name
        when /rar$/
          [ 'unrar', 't', file_path ]
        when /(tar|tar\.bz|tbz)$/
          [ 'tar', 'tf', file_path ]
        when /zip$/
          [ 'zip', '-T', file_path ]
        when /gz|tgz$/
          [ 'gunzip', '-t', file_path ]
        else
          raise UnrecognizedArchiveError
        end
  Shellshot.exec cmd, :stdall => false
  true
rescue
  false
end