module Unpacker
Public Class Methods
archive?(file_name)
click to toggle source
# File lib/unpacker.rb, line 58 def self.archive?(file_name) supported = Unpacker.supported_archives ext = File.extname(file_name).sub('.', '') return true if ext=="" support = supported.include? ext if !support help = case file_name when /(tar|tgz|tar\.gz|tar\.bz|tbz|tar\.bz2)$/ "Please install tar" when /zip$/ "Please install unzip" when /gz$/ "Please install gunzip" when /bz2$/ "Please install bunzip2" else raise UnrecognizedArchiveError end msg = "Archive type not supported: #{ext}\n#{help}" raise UnrecognizedArchiveError.new(msg) end support end
supported_archives()
click to toggle source
# File lib/unpacker.rb, line 103 def self.supported_archives supported = [] if !which('unrar').empty? supported << "rar" end if !which('tar').empty? %w[tar tgz tgz tar.gz tar.bz tar.bz2 tbz].each do |ext| supported << ext end end if !which('unzip').empty? supported << "zip" end if !which('gunzip').empty? supported << "gz" end if !which('bunzip2').empty? supported << "bz2" end supported end
unpack(file, tmpdir = "/tmp", &block)
click to toggle source
# File lib/unpacker.rb, line 34 def self.unpack(file, tmpdir = "/tmp", &block) Dir.mktmpdir 'unpacker' do |dir| cmd = case file when /(tar|tgz|tar\.gz)$/ "tar xzf #{file} --directory #{dir}" when /(tar\.bz|tbz|tar\.bz2)$/ "tar xjf #{file} --directory #{dir}" when /zip$/ "unzip #{file} -d #{dir}" when /gz$/ "gunzip #{file}" when /bz2$/ "bunzip #{file}" else raise UnrecognizedArchiveError end stdout, stderr, status = Open3.capture3 cmd if !status.success? raise RuntimeError.new("There was a problem unpacking #{file}\n#{stderr}") end block.call Dir.new(dir) end end
valid?(file_path, file_name = file_path)
click to toggle source
# File lib/unpacker.rb, line 82 def self.valid?(file_path, file_name = file_path) cmd = case file_name when /(tar|tar\.bz|tbz)$/ "tar tf #{file_path}" when /zip$/ "zip -T #{file_path}" when /gz|tgz$/ "gunzip -t #{file_path}" when /bz2$/ "bunzip2 -t #{file_path}" else raise UnrecognizedArchiveError end stdout, stderr, status = Open3.capture3 cmd if status.success? true else false end end