module BrickAndMortar::Download

Constants

Public Class Methods

deflate_tar(tar_string, destination = Dir.pwd) click to toggle source

Thanks to TomCZ stackoverflow.com/questions/856891/unzip-zip-tar-tag-gz-files-with-ruby for this

# File lib/brick_and_mortar/download.rb, line 41
def self.deflate_tar(tar_string, destination = Dir.pwd)
  first_directory = nil
  Gem::Package::TarReader.new(tar_string) do |tar|
    dest = nil
    tar.each do |entry|
      if entry.full_name == TAR_LONGLINK
        dest = File.join destination, entry.read.strip
        next
      end
      dest ||= File.join destination, entry.full_name
      if entry.directory?
        FileUtils.rm_rf dest unless File.directory? dest
        FileUtils.mkdir_p dest, :mode => entry.header.mode, :verbose => false
        first_directory = dest unless first_directory
      elsif entry.file?
        FileUtils.rm_rf dest unless File.file? dest
        dir = File.dirname dest
        unless File.exist?(dir)
          FileUtils.mkpath dir
          first_directory = dir unless first_directory
        end
        File.open dest, "wb" do |f|
          f.print entry.read
        end
        FileUtils.chmod entry.header.mode, dest, :verbose => false
        unless first_directory
          puts "WARNING: File \"#{dest}\" created before a root directory"
        end
      elsif entry.header.typeflag == '2' #Symlink!
        dir = File.dirname dest
        unless File.exist?(dir)
          FileUtils.mkpath dir
          first_directory = dir unless first_directory
        end
        File.symlink entry.header.linkname, dest
        unless first_directory
          puts "WARNING: Symlink \"#{dest}\" to \"#{entry.header.linkname}\" created before a root directory"
        end
      end
      dest = nil
    end
  end
  first_directory
end
deflate_tar_bz2(archive, destination = Dir.pwd) click to toggle source
# File lib/brick_and_mortar/download.rb, line 88
def self.deflate_tar_bz2(archive, destination = Dir.pwd)
  Bzip2::FFI::Reader.open(archive) do |reader|
    File.open("#{destination}.tar", 'w') do |tar|
      tar.write reader.read
    end
  end
  first_directory = File.open("#{destination}.tar", 'r') do |tar|
    deflate_tar(tar)
  end
  FileUtils.rm "#{destination}.tar"
  first_directory
end
deflate_tar_gz(tar_gz_archive, destination = Dir.pwd) click to toggle source
# File lib/brick_and_mortar/download.rb, line 85
def self.deflate_tar_gz(tar_gz_archive, destination = Dir.pwd)
  deflate_tar(Zlib::GzipReader.open(tar_gz_archive), destination)
end
get_and_unpack_tar_bz2(url, name = nil, options = {}) click to toggle source
# File lib/brick_and_mortar/download.rb, line 114
def self.get_and_unpack_tar_bz2(url, name = nil, options = {})
  unless name
    name = name_from_path url
  end
  uncompressed_dir_name = deflate_tar_bz2(open(url))
  FileUtils.mv uncompressed_dir_name, name
end
get_and_unpack_tar_gz(url, name = nil, options = {}) click to toggle source
# File lib/brick_and_mortar/download.rb, line 107
def self.get_and_unpack_tar_gz(url, name = nil, options = {})
  unless name
    name = name_from_path url
  end
  uncompressed_dir_name = deflate_tar_gz(open(url))
  FileUtils.mv uncompressed_dir_name, name
end
get_and_unpack_zip(url, name = nil, options = {}) click to toggle source
# File lib/brick_and_mortar/download.rb, line 100
def self.get_and_unpack_zip(url, name = nil, options = {})
  unless name
    name = name_from_path url
  end
  unzipped_dir_name = unzip(open(url))
  FileUtils.mv unzipped_dir_name, name
end
name_from_path(url) click to toggle source
# File lib/brick_and_mortar/download.rb, line 17
def self.name_from_path(url)
  File.basename(url, File.extname(url))
end
unzip(zip_file) click to toggle source
# File lib/brick_and_mortar/download.rb, line 20
def self.unzip(zip_file)
  root_dir_name = nil
  Zip::File.open(zip_file) do |z|
    z.each do |entry|
      next unless entry.file?
      fpath = entry.to_s
      unless root_dir_name
        root_dir_name = File.dirname fpath
      end
      unless File.exist?(entry.name)
        FileUtils::mkdir_p(File.dirname(entry.name))
        # puts "Extracting #{entry.name} to #{fpath}"
        entry.extract(entry.name)
      end
    end
  end
  root_dir_name
end