Sha256: 70cdf294f5ee49b64bf0a72b87c478db87171c59fcea7b2c751710ed791fdf7c

Contents?: true

Size: 1.22 KB

Versions: 71

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require_dependency "renalware"

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

    def unzip
      Dir.mktmpdir(nil, Rails.root.join("tmp").to_s) do |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

71 entries across 71 versions & 1 rubygems

Version Path
renalware-core-2.0.86 app/models/renalware/zip_archive.rb
renalware-core-2.0.85 app/models/renalware/zip_archive.rb
renalware-core-2.0.84 app/models/renalware/zip_archive.rb
renalware-core-2.0.83 app/models/renalware/zip_archive.rb
renalware-core-2.0.82 app/models/renalware/zip_archive.rb
renalware-core-2.0.81 app/models/renalware/zip_archive.rb
renalware-core-2.0.80 app/models/renalware/zip_archive.rb
renalware-core-2.0.79 app/models/renalware/zip_archive.rb
renalware-core-2.0.78 app/models/renalware/zip_archive.rb
renalware-core-2.0.77 app/models/renalware/zip_archive.rb
renalware-core-2.0.76 app/models/renalware/zip_archive.rb
renalware-core-2.0.75 app/models/renalware/zip_archive.rb
renalware-core-2.0.74 app/models/renalware/zip_archive.rb
renalware-core-2.0.73 app/models/renalware/zip_archive.rb
renalware-core-2.0.72 app/models/renalware/zip_archive.rb
renalware-core-2.0.71 app/models/renalware/zip_archive.rb
renalware-core-2.0.70 app/models/renalware/zip_archive.rb
renalware-core-2.0.69 app/models/renalware/zip_archive.rb
renalware-core-2.0.68 app/models/renalware/zip_archive.rb
renalware-core-2.0.67 app/models/renalware/zip_archive.rb