Module: Dunlop::FileZip

Defined in:
app/services/dunlop/file_zip.rb

Class Method Summary collapse

Class Method Details

.gzip(unzipped_filename, gzipped_filename) ⇒ Object



36
37
38
39
# File 'app/services/dunlop/file_zip.rb', line 36

def self.gzip(unzipped_filename, gzipped_filename)
  return if mime_type(unzipped_filename) == "application/x-gzip"
  %x[cat #{unzipped_filename} | gzip -c > #{gzipped_filename}]
end

.mime_type(filename) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'app/services/dunlop/file_zip.rb', line 25

def self.mime_type(filename)
  case %x[file -bi #{filename}]
  when /gzip/i
    "application/x-gzip"
  when /zip/i
    "application/zip"
  else
    "text/plain"
  end
end

.unzip(zipped_filename, unzipped_filename) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/services/dunlop/file_zip.rb', line 3

def self.unzip(zipped_filename, unzipped_filename)
  case self.mime_type(zipped_filename)
  when "application/zip"
    %x[ unzip -p #{zipped_filename} > #{unzipped_filename} ]
  when "application/x-gzip"
    %x[ gzip -c -d #{zipped_filename} > #{unzipped_filename} ]
  when "text/plain"
    FileUtils.cp(zipped_filename, unzipped_filename)
  else
    raise "unsupported mime-type"
  end
end

.unzip_to_directory_and_junk_paths(zipped_filename, destination_directory) ⇒ Object



16
17
18
19
20
21
22
23
# File 'app/services/dunlop/file_zip.rb', line 16

def self.unzip_to_directory_and_junk_paths(zipped_filename, destination_directory)
  case self.mime_type(zipped_filename)
  when "application/zip"
    %x[ unzip -j #{zipped_filename} -d #{destination_directory} ]
  else
    raise "unsupported mime-type"
  end
end