Module: ZipUtils

Defined in:
lib/more/facets/ziputils.rb

Overview

Function module for compression methods.

Note: This used to be extensions to FileUtils.

Defined Under Namespace

Modules: DryRun, NoWrite, Verbose

Constant Summary collapse

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

  '.tar.gz'  => 'tar_gzip',
  '.tgz'     => 'tar_gzip',
  '.tar.bz2' => 'tar_bzip',
  '.zip'     => 'zip'
}

Class Method Summary collapse

Class Method Details

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

Compress based on given extension. Supported extensions are:

  • tar.gz

  • tgz

  • tar.bz2

  • zip



64
65
66
67
68
69
70
71
# File 'lib/more/facets/ziputils.rb', line 64

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

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

Tar Bzip2



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/more/facets/ziputils.rb', line 104

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}"
  # display equivalent commandline
  if options[:verbose] or options[:dryrun]
    puts cmd
  end
  # create tar.bzip2 file
  unless options[:noop] or options[:dryrun]
    system cmd
  end
  return File.expand_path(file)
end

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

Tar Gzip



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/more/facets/ziputils.rb', line 75

def tar_gzip(folder, file=nil, options={})
  require 'zlib'
  # name of file to create
  file ||= File.basename(File.expand_path(folder)) + '.tar.gz'
  cmd = "tar --gzip -czf #{file} #{folder}"
  # display equivalent commandline
  if options[:verbose] or options[:dryrun]
    puts cmd
  end
  # create tar.gzip file
  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

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

Untar Bzip2



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/more/facets/ziputils.rb', line 123

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

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

Untar Gzip



95
96
97
98
99
# File 'lib/more/facets/ziputils.rb', line 95

def untar_gzip(file, options={})
  require 'zlib'
  # TODO Write internalized untar_gzip function.

end

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

Unzip



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/more/facets/ziputils.rb', line 157

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

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

Zip

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/more/facets/ziputils.rb', line 139

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