Module | ZipUtils |
In: |
lib/more/facets/ziputils.rb
|
COMPRESS_FORMAT | = | { '.tar.gz' => 'tar_gzip', '.tgz' => 'tar_gzip', '.tar.bz2' => 'tar_bzip2', '.zip' => 'zip' |
# 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:
TODO: support gzip and bzip2 as well.
# 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
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
# 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
# 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
Untar Bzip2
# 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.
# File lib/more/facets/ziputils.rb, line 143 def untar_gzip(file, options={}) untar(ungzip(file, options), options) end
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