Sha256: aa4c759930264067b1c7b791dc616b726a89e0c4c059aab3e62742622842cd6a

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

class DarwinCore
  class Expander
    def initialize(archive_path, tmp_dir)
      @archive_path = archive_path
      @tmp_dir = tmp_dir
      @path = File.join(tmp_dir, 'dwc_' + rand(10_000_000_000).to_s)
      @unpacker = get_unpacker
    end

    def unpack
      clean
      raise DarwinCore::FileNotFoundError unless File.exists?(@archive_path)
      success = @unpacker.call(@path, @archive_path) if @unpacker
      (@unpacker && success && $?.exitstatus == 0) ? 
        success : 
        (clean; raise DarwinCore::UnpackingError)
    end

    def path
      @files_path ||= files_path
    end

    def clean
      FileUtils.rm_rf(@path) if FileTest.exists?(@path)
    end

    def files
      return nil unless path && FileTest.exists?(path)
      Dir.entries(path).select {|e| e !~ /[\.]{1,2}$/}.sort
    end

    private

    def esc(a_str)
      "'" + a_str.gsub(92.chr, '\\\\\\').gsub("'", "\\\\'") + "'"
    end

    def get_unpacker
      file_command = IO.popen("file -z " + esc(@archive_path))
      file_type    = file_command.read
      file_command.close

      if file_type.match(/tar.*gzip/i)
        return proc do |tmp_path, archive_path|
          FileUtils.mkdir tmp_path
          system("tar -zxf #{esc(archive_path)} -C #{tmp_path} > /dev/null 2>&1")
        end
      end

      if file_type.match(/Zip/)
        return proc { |tmp_path, archive_path| system("unzip -qq -d #{tmp_path} #{esc(archive_path)} > /dev/null 2>&1") }
      end

      return nil
    end

    def path_entries(dir)
      Dir.entries(dir).select {|e| e !~ /[\.]{1,2}$/}.sort
    end

    def files_path
      res = nil
      entries = path_entries(@path)
      if entries.include?('meta.xml')
        res = @path
      else
        entries.each do |e|
          check_path = File.join(@path, e)
          if FileTest.directory?(check_path)
            if path_entries(check_path).include?('meta.xml')
              res = check_path
              break
            end
          end
        end
      end
      res
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dwc-archive-0.9.6 lib/dwc-archive/expander.rb
dwc-archive-0.9.5 lib/dwc-archive/expander.rb
dwc-archive-0.9.4 lib/dwc-archive/expander.rb