Sha256: 5c3d10f082fb14e59f365cf9d1dbd34171615d45daf5cfe631d2994161d8ed01

Contents?: true

Size: 1.91 KB

Versions: 19

Compression:

Stored size: 1.91 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(10000000000).to_s)
      @unpacker = get_unpacker
    end

    def unpack
      clean
      raise FileNotFoundError unless File.exists?(@archive_path)
      success = @unpacker.call(@path, @archive_path) if @unpacker
      (@unpacker && success && $?.exitstatus == 0) ? success : (clean; raise 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_type = IO.popen("file -z " + esc(@archive_path)).read
      
      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

19 entries across 19 versions & 1 rubygems

Version Path
dwc-archive-0.4.11 lib/dwc-archive/expander.rb
dwc-archive-0.4.10 lib/dwc-archive/expander.rb
dwc-archive-0.4.9 lib/dwc-archive/expander.rb
dwc-archive-0.4.8 lib/dwc-archive/expander.rb
dwc-archive-0.4.7 lib/dwc-archive/expander.rb
dwc-archive-0.4.6 lib/dwc-archive/expander.rb
dwc-archive-0.4.5 lib/dwc-archive/expander.rb
dwc-archive-0.4.4 lib/dwc-archive/expander.rb
dwc-archive-0.4.3 lib/dwc-archive/expander.rb
dwc-archive-0.4.2 lib/dwc-archive/expander.rb
dwc-archive-0.4.1 lib/dwc-archive/expander.rb
dwc-archive-0.4.0 lib/dwc-archive/expander.rb
dwc-archive-0.3.1 lib/dwc-archive/expander.rb
dwc-archive-0.3.0 lib/dwc-archive/expander.rb
dwc-archive-0.2.3 lib/dwc-archive/expander.rb
dwc-archive-0.2.2 lib/dwc-archive/expander.rb
dwc-archive-0.2.1 lib/dwc-archive/expander.rb
dwc-archive-0.2.0 lib/dwc-archive/expander.rb
dwc-archive-0.1.6 lib/dwc-archive/expander.rb