Sha256: 40dd37848b5abcbcf24798c3d3926b64dd874550b387694d92e65bd25d347e0c

Contents?: true

Size: 1.19 KB

Versions: 13

Compression:

Stored size: 1.19 KB

Contents

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

13 entries across 13 versions & 1 rubygems

Version Path
renalware-core-2.0.8 app/models/renalware/zip_archive.rb
renalware-core-2.0.7 app/models/renalware/zip_archive.rb
renalware-core-2.0.5 app/models/renalware/zip_archive.rb
renalware-core-2.0.4 app/models/renalware/zip_archive.rb
renalware-core-2.0.3 app/models/renalware/zip_archive.rb
renalware-core-2.0.2 app/models/renalware/zip_archive.rb
renalware-core-2.0.1 app/models/renalware/zip_archive.rb
renalware-core-2.0.0 app/models/renalware/zip_archive.rb
renalware-core-2.0.0.pre.rc13 app/models/renalware/zip_archive.rb
renalware-core-2.0.0.pre.rc11 app/models/renalware/zip_archive.rb
renalware-core-2.0.0.pre.rc10 app/models/renalware/zip_archive.rb
renalware-core-2.0.0.pre.rc9 app/models/renalware/zip_archive.rb
renalware-core-2.0.0.pre.rc8 app/models/renalware/zip_archive.rb