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.0.127 app/models/renalware/zip_archive.rb
renalware-core-2.0.126 app/models/renalware/zip_archive.rb
renalware-core-2.0.125 app/models/renalware/zip_archive.rb
renalware-core-2.0.124 app/models/renalware/zip_archive.rb
renalware-core-2.0.123 app/models/renalware/zip_archive.rb
renalware-core-2.0.121 app/models/renalware/zip_archive.rb
renalware-core-2.0.120 app/models/renalware/zip_archive.rb
renalware-core-2.0.119 app/models/renalware/zip_archive.rb
renalware-core-2.0.118 app/models/renalware/zip_archive.rb
renalware-core-2.0.117 app/models/renalware/zip_archive.rb
renalware-core-2.0.116 app/models/renalware/zip_archive.rb
renalware-core-2.0.115 app/models/renalware/zip_archive.rb
renalware-core-2.0.113 app/models/renalware/zip_archive.rb
renalware-core-2.0.112 app/models/renalware/zip_archive.rb
renalware-core-2.0.111 app/models/renalware/zip_archive.rb
renalware-core-2.0.110 app/models/renalware/zip_archive.rb
renalware-core-2.0.109 app/models/renalware/zip_archive.rb
renalware-core-2.0.108 app/models/renalware/zip_archive.rb
renalware-core-2.0.106 app/models/renalware/zip_archive.rb
renalware-core-2.0.105 app/models/renalware/zip_archive.rb