Module: Unpacker
- Defined in:
- lib/unpacker.rb
Defined Under Namespace
Classes: UnrecognizedArchiveError
Class Method Summary
collapse
Class Method Details
.archive?(file_name) ⇒ Boolean
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# 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 ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# 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) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# 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) ⇒ Boolean
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# 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
|