Sha256: 49fd0d4340e83740cd30a5a23405639cae5b23e2a39b4406033e0817d15339b3

Contents?: true

Size: 1.27 KB

Versions: 78

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require_dependency "renalware"

module Renalware
  class ZipArchive
    def initialize(file)
      @file = Pathname.new(file)
    end

    def unzip
      # Create a tmp dir and ensure PG has access to it.
      Dir.mktmpdir do |dir|
        `chmod a+rX #{dir}`
        files = unzip_to_tmp_dir_and_return_pathames_array(dir)
        yield(files)
      end
    end

    def rar_archive?
      `file #{file}`.match? /RAR/
    end

    private

    attr_reader :file

    # Zip arguments
    # -o = overwrite if files already there (to avoid unattended)
    # -j = junk (throw away) the path structure in the zip file - this has the risk that
    #      files in separate folders but with the same name could overwrite each other..
    # Unrar aguments
    #  - e extract files
    #  - o+ overwrite existing
    def unzip_to_tmp_dir_and_return_pathames_array(dir)
      zip_realpath = file.realpath
      Dir.chdir(dir) do
        if rar_archive?
          execute("unrar e -o+ #{zip_realpath}")
        else
          execute("unzip -o -j #{zip_realpath}")
        end
      end
      Pathname.new(dir).children
    end

    def execute(cmd)
      success = system(cmd)
      raise("Command '#{cmd}' returned #{$CHILD_STATUS.exitstatus}") unless success
    end
  end
end

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/models/renalware/zip_archive.rb
renalware-core-2.1.0 app/models/renalware/zip_archive.rb
renalware-core-2.0.167 app/models/renalware/zip_archive.rb
renalware-core-2.0.166 app/models/renalware/zip_archive.rb
renalware-core-2.0.165 app/models/renalware/zip_archive.rb
renalware-core-2.0.164 app/models/renalware/zip_archive.rb
renalware-core-2.0.163 app/models/renalware/zip_archive.rb
renalware-core-2.0.162 app/models/renalware/zip_archive.rb
renalware-core-2.0.161 app/models/renalware/zip_archive.rb
renalware-core-2.0.160 app/models/renalware/zip_archive.rb
renalware-core-2.0.159 app/models/renalware/zip_archive.rb
renalware-core-2.0.158 app/models/renalware/zip_archive.rb
renalware-core-2.0.157 app/models/renalware/zip_archive.rb
renalware-core-2.0.156 app/models/renalware/zip_archive.rb
renalware-core-2.0.155 app/models/renalware/zip_archive.rb
renalware-core-2.0.153 app/models/renalware/zip_archive.rb
renalware-core-2.0.152 app/models/renalware/zip_archive.rb
renalware-core-2.0.151 app/models/renalware/zip_archive.rb
renalware-core-2.0.149 app/models/renalware/zip_archive.rb
renalware-core-2.0.148 app/models/renalware/zip_archive.rb