Module: ZipUtils

Defined in:
lib/folio/ziputils.rb

Overview

ZipUtils

Function module for compression methods.

TODO: Much of this shells out. It would be best to internalize.

Defined Under Namespace

Modules: DryRun, NoWrite, Verbose

Constant Summary collapse

COMPRESS_FORMAT =
{
  '.tar.gz'  => 'tar_gzip',
  '.tgz'     => 'tar_gzip',
  '.tar.bz2' => 'tar_bzip2',
  '.zip'     => 'zip'
}

Class Method Summary collapse

Class Method Details

.bzip2(file, option = {}) ⇒ Object Also known as: bzip



69
70
71
72
73
74
# File 'lib/folio/ziputils.rb', line 69

def bzip2(file, option={})
  cmd = "bzip2 #{file}"
  puts   cmd if     options[:dryrun] or options[:verbose]
  system cmd unless options[:dryrun] or options[:noop]
  return File.expand_path(file + '.bz2')
end

.compress(folder, file, options = {}) ⇒ Object

Compress folder or file based on given extension. Supported extensions are:

  • .tar.gz

  • .tgz

  • .tar.bz2

  • .zip

TODO: support gzip and bzip2 as well.



30
31
32
33
34
35
36
37
# File 'lib/folio/ziputils.rb', line 30

def compress(folder, file, options={})
  format = COMPRESS_FORMAT[File.extname(file)]
  if format
    send(format, folder, file, options)
  else
    raise ArgumentError, "unknown compression format -- #{format_extension}"
  end
end

.gzip(file, option = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/folio/ziputils.rb', line 41

def gzip(file, option={})
  require 'zlib'
  fname = File.basename(file) + '.gz'
  if options[:dryrun] or options[:verbose]
    puts "gzip #{file}"
  end
  Zlib::GzipWriter.open(fname) do |gz|
    gz.write(File.read(file))
  end unless options[:dryrun] or options[:noop]
  return File.expand_path(fname)
end

.tar(folder, file = nil, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/folio/ziputils.rb', line 91

def tar(folder, file=nil, options={})
  require 'folio/minitar'
  file ||= File.basename(File.expand_path(folder)) + '.tar'
  cmd = "tar -cf #{file} #{folder}"
  puts cmd if options[:verbose] or options[:dryrun]
  unless options[:noop] or options[:dryrun]
    gzIO = File.open(file, 'wb')
    Archive::Tar::Minitar.pack(folder, gzIO)
  end
  return File.expand_path(file)
end

.tar_bzip2(folder, file = nil, options = {}) ⇒ Object Also known as: tar_bzip, tar_j

Tar Bzip2



150
151
152
153
154
155
156
157
# File 'lib/folio/ziputils.rb', line 150

def tar_bzip2(folder, file=nil, options={})
  # name of file to create
  file ||= File.basename(File.expand_path(folder)) + '.tar.bz2'
  cmd = "tar --bzip2 -cf #{file} #{folder}"
  puts   cmd if     options[:dryrun] or options[:verbose]
  system cmd unless options[:dryrun] or options[:noop]
  return File.expand_path(file)
end

.tar_gzip(folder, file = nil, options = {}) ⇒ Object Also known as: tar_z

Tar Gzip



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/folio/ziputils.rb', line 119

def tar_gzip(folder, file=nil, options={})
  require 'zlib'
  require 'folio/minitar'
  file ||= File.basename(File.expand_path(folder)) + '.tar.gz' # '.tgz' which ?
  cmd = "tar --gzip -czf #{file} #{folder}"
  puts cmd if options[:verbose] or options[:dryrun]
  unless options[:noop] or options[:dryrun]
    gzIO = Zlib::GzipWriter.new(File.open(file, 'wb'))
    Archive::Tar::Minitar.pack(folder, gzIO)
  end
  return File.expand_path(file)
end

.unbzip2(file, options = {}) ⇒ Object Also known as: unbzip



80
81
82
83
84
85
# File 'lib/folio/ziputils.rb', line 80

def unbzip2(file, options={})
  cmd = "unbzip2 #{file}"
  puts   cmd if     options[:dryrun] or options[:verbose]
  system cmd unless options[:dryrun] or options[:noop]
  return File.expand_path(file.chomp(File.extname(file)))
end

.ungzip(file, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/folio/ziputils.rb', line 55

def ungzip(file, options={})
  require 'zlib'
  fname = File.basename(file).chomp(File.extname(file))
  if options[:dryrun] or options[:verbose]
    puts "ungzip #{file}"
  end
  Zlib::GzipReader.open(file) do |gz|
    File.open(fname, 'wb'){ |f| f << gz.read }
  end unless options[:dryrun] or options[:noop]
  return File.expand_path(fname)
end

.untar(file, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/folio/ziputils.rb', line 105

def untar(file, options={})
  require 'folio/minitar'
  #file ||= File.basename(File.expand_path(folder)) + '.tar'
  cmd = "untar #{file}"
  puts cmd if options[:verbose] or options[:dryrun]
  unless options[:noop] or options[:dryrun]
    gzIO = File.open(file, 'wb')
    Archive::Tar::Minitar.unpack(gzIO)
  end
  return File.expand_path(file)
end

.untar_bzip2(file, options = {}) ⇒ Object Also known as: untar_bzip, untar_j

Untar Bzip2



165
166
167
168
169
# File 'lib/folio/ziputils.rb', line 165

def untar_bzip2(file, options={})
  cmd = "tar --bzip2 -xf #{file}"
  puts   cmd if     options[:dryrun] or options[:verbose]
  system cmd unless options[:dryrun] or options[:noop]
end

.untar_gzip(file, options = {}) ⇒ Object Also known as: untar_z

Untar Gzip

TODO: Write unified untar_gzip function.



142
143
144
# File 'lib/folio/ziputils.rb', line 142

def untar_gzip(file, options={})
  untar(ungzip(file, options), options)
end

.unzip(file, options = {}) ⇒ Object

Unzip



188
189
190
191
192
# File 'lib/folio/ziputils.rb', line 188

def unzip(file, options={})
  cmd = "unzip #{file}"
  puts   cmd if     options[:dryrun] or options[:verbose]
  system cmd unless options[:dryrun] or options[:noop]
end

.zip(folder, file = nil, options = {}) ⇒ Object

Zip

Raises:

  • (ArgumentError)


177
178
179
180
181
182
183
184
# File 'lib/folio/ziputils.rb', line 177

def zip(folder, file=nil, options={})
  raise ArgumentError if folder == '.*'
  file ||= File.basename(File.expand_path(folder)) + '.zip'
  cmd = "zip -rqu #{file} #{folder}"
  puts   cmd if     options[:dryrun] or options[:verbose]
  system cmd unless options[:dryrun] or options[:noop]
  return File.expand_path(file)
end