ZipUtils
Function module for compression methods.
TODO: Much of this shells out. It would be best to internalize.
Methods
bzip
bzip2
compress
gzip
tar
tar_bzip
tar_bzip2
tar_gzip
tar_j
tar_z
unbzip
unbzip2
ungzip
untar
untar_bzip
untar_bzip2
untar_gzip
untar_j
untar_z
unzip
zip
Classes and Modules
Module ZipUtils::DryRunModule ZipUtils::NoWrite
Module ZipUtils::Verbose
Constants
COMPRESS_FORMAT | = | { '.tar.gz' => 'tar_gzip', '.tgz' => 'tar_gzip', '.tar.bz2' => 'tar_bzip2', '.zip' => 'zip' |
Public Instance methods
Alias for bzip2
This method is also aliased as
bzip
[ + ]
# File lib/more/facets/ziputils.rb, line 70 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 or file based on given extension. Supported extensions are:
- .tar.gz
- .tgz
- .tar.bz2
- .zip
[ + ]
# File lib/more/facets/ziputils.rb, line 31 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
[ + ]
# File lib/more/facets/ziputils.rb, line 42 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
[ + ]
# File lib/more/facets/ziputils.rb, line 92 def tar(folder, file=nil, options={}) require 'facets/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
Alias for tar_bzip2
Tar Bzip2
[ + ]
# File lib/more/facets/ziputils.rb, line 151 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
This method is also aliased as
tar_z
[ + ]
# File lib/more/facets/ziputils.rb, line 120 def tar_gzip(folder, file=nil, options={}) require 'zlib' require 'facets/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
Alias for tar_bzip2
Alias for tar_gzip
Alias for unbzip2
This method is also aliased as
unbzip
[ + ]
# File lib/more/facets/ziputils.rb, line 81 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
[ + ]
# File lib/more/facets/ziputils.rb, line 56 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
[ + ]
# File lib/more/facets/ziputils.rb, line 106 def untar(file, options={}) require 'facets/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
Alias for untar_bzip2
Untar Bzip2
This method is also aliased as
untar_bzip
untar_j
[ + ]
# File lib/more/facets/ziputils.rb, line 166 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
TODO: Write unified untar_gzip function.
This method is also aliased as
untar_z
[ + ]
# File lib/more/facets/ziputils.rb, line 143 def untar_gzip(file, options={}) untar(ungzip(file, options), options) end
Alias for untar_bzip2
Alias for untar_gzip
Unzip
[ + ]
# File lib/more/facets/ziputils.rb, line 189 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
[ + ]
# File lib/more/facets/ziputils.rb, line 178 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