1+ module Dearchiver
2+ class Processor
3+
4+ attr_reader :filename
5+ attr_reader :archive_type
6+
7+ # Initialize a new CloudDns::Client object
8+ #
9+ # options - Set of configuration options
10+ #
11+ # options[:filename] - The complete filename (with included path) to work with.
12+ # options[:archive_type] - Optional. See archive_options for recognized file types.
13+ #
14+ def initialize ( options = { } )
15+ @filename = options [ :filename ]
16+ raise ArgumentError , "Processor: :filename required!" unless options . has_key? ( :filename ) and not options [ :filename ] . empty?
17+
18+ @archive_type = File . extname ( @filename ) if valid_file_type?
19+ @archive_type ||= options [ :archive_type ]
20+ raise ArgumentError , "Processor: :archive_type required. :filename does not contain a recognizable extension!" if @archive_type . nil? or @archive_type . empty?
21+ end
22+
23+ def crc_ok?
24+ type = File . extname ( @filename )
25+ command = archive_options [ type ] [ :crc_check ] . gsub ( "<filename>" , filename )
26+ crc_check = %x[#{ command } ]
27+ crc_check . include? ( archive_options [ type ] [ :crc_ok ] ) ? true : false
28+ end
29+
30+ private
31+
32+ def archive_options
33+ {
34+ ".zip" => {
35+ :crc_check => "unzip -t <filename>" ,
36+ :crc_ok => "No errors detected in compressed data"
37+ } ,
38+ ".rar" => {
39+ :crc_check => "unrar t <filename>" ,
40+ :crc_ok => "All OK"
41+ } ,
42+ ".7z" => {
43+ :crc_check => "7z t <filename>" ,
44+ :crc_ok => "Everything is Ok"
45+ } ,
46+ ".tar.gz" => {
47+ :crc_check => "tar -tvzf <filename> > /dev/null" ,
48+ :crc_ok => ""
49+ }
50+ }
51+ end
52+
53+ def valid_file_type?
54+ archive_options . has_key? ( File . extname ( @filename ) )
55+ end
56+
57+ end
58+ end
0 commit comments