Module ZipUtils
In: lib/more/facets/ziputils.rb

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::DryRun
Module ZipUtils::NoWrite
Module ZipUtils::Verbose

Constants

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

Public Instance methods

bzip(file, option={})

Alias for bzip2

[Source]

# 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

TODO: support gzip and bzip2 as well.

[Source]

# 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

[Source]

# 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

[Source]

# 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_bzip(folder, file=nil, options={})

Alias for tar_bzip2

Tar Bzip2

[Source]

# 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

[Source]

# 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
tar_j(folder, file=nil, options={})

Alias for tar_bzip2

tar_z(folder, file=nil, options={})

Alias for tar_gzip

unbzip(file, options={})

Alias for unbzip2

[Source]

# 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

[Source]

# 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

[Source]

# 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_bzip(file, options={})

Alias for untar_bzip2

Untar Bzip2

[Source]

# 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.

[Source]

# File lib/more/facets/ziputils.rb, line 143
  def untar_gzip(file, options={})
    untar(ungzip(file, options), options)
  end
untar_j(file, options={})

Alias for untar_bzip2

untar_z(file, options={})

Alias for untar_gzip

Unzip

[Source]

# 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

[Source]

# 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

[Validate]