Sha256: 7fcffc93c46c425bd8b71d0d3bae9495ccda62662257efc2d7857d1a1f80b5ae

Contents?: true

Size: 1.98 KB

Versions: 29

Compression:

Stored size: 1.98 KB

Contents

require 'zip/zip'
require 'tempfile'
require 'find'

module Selenium
  module WebDriver
    #
    # @api private
    #

    module Zipper

      EXTENSIONS = %w[.zip .xpi]

      class << self

        def unzip(path)
          destination = Dir.mktmpdir("unzip")
          FileReaper << destination

          Zip::ZipFile.open(path) do |zip|
            zip.each do |entry|
              to      = File.join(destination, entry.name)
              dirname = File.dirname(to)

              FileUtils.mkdir_p dirname unless File.exist? dirname
              zip.extract(entry, to)
            end
          end

          destination
        end

        def zip(path)
          with_tmp_zip do |zip|
            ::Find.find(path) do |file|
              unless File.directory?(file)
                add_zip_entry zip, file, file.sub("#{path}/", '')
              end
            end

            zip.commit
            File.open(zip.name, "rb") { |io| Base64.strict_encode64 io.read }
          end
        end

        def zip_file(path)
          with_tmp_zip do |zip|
            add_zip_entry zip, path, File.basename(path)

            zip.commit
            File.open(zip.name, "rb") { |io| Base64.strict_encode64 io.read }
          end
        end

        private

        def with_tmp_zip(&blk)
          # can't use Tempfile here since it doesn't support File::BINARY mode on 1.8
          # can't use Dir.mktmpdir(&blk) because of http://jira.codehaus.org/browse/JRUBY-4082
          tmp_dir = Dir.mktmpdir
          zip_path = File.join(tmp_dir, "webdriver-zip")

          begin
            Zip::ZipFile.open(zip_path, Zip::ZipFile::CREATE, &blk)
          ensure
            FileUtils.rm_rf tmp_dir
            FileUtils.rm_rf zip_path
          end
        end

        def add_zip_entry(zip, file, entry_name)
          entry = Zip::ZipEntry.new(zip.name, entry_name)
          entry.follow_symlinks = true

          zip.add entry, file
        end

      end
    end # Zipper
  end # WebDriver
end # Selenium

Version data entries

29 entries across 29 versions & 4 rubygems

Version Path
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/common/zipper.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/common/zipper.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/common/zipper.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/common/zipper.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.22.0.rc1 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.21.2 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.21.2.rc1 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.21.1 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.21.0 lib/selenium/webdriver/common/zipper.rb
frameworks-capybara-0.2.0.rc6 vendor/bundle/ruby/1.8/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/common/zipper.rb
frameworks-capybara-0.2.0.rc5 vendor/bundle/ruby/1.8/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/common/zipper.rb
frameworks-capybara-0.2.0.rc4 vendor/bundle/ruby/1.8/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/common/zipper.rb
frameworks-capybara-0.2.0.rc3 vendor/bundle/ruby/1.8/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/common/zipper.rb
frameworks-capybara-0.2.0.rc2 vendor/bundle/ruby/1.8/gems/selenium-webdriver-2.20.0/lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.20.0 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.20.0.rc1 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.19.0 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.19.0.rc2 lib/selenium/webdriver/common/zipper.rb
selenium-webdriver-2.19.0.rc1 lib/selenium/webdriver/common/zipper.rb