Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/zipper.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require 'zip'
21
require 'zip/version' # Not required automatically
22
require 'tempfile'
23
require 'find'
24
require 'base64'
25
26
module Selenium
27
module WebDriver
28
#
29
# @api private
30
#
31
32
module Zipper
33
EXTENSIONS = %w[.zip .xpi].freeze
34
RUBYZIP_V3 = Zip::VERSION >= '3.0.0'
35
36
class << self
37
def unzip(path)
38
destination = Dir.mktmpdir('webdriver-unzip')
39
FileReaper << destination
40
41
Zip::File.open(path) do |zip|
42
zip.each do |entry|
43
to = File.join(destination, entry.name)
44
dirname = File.dirname(to)
45
46
FileUtils.mkdir_p dirname
47
if RUBYZIP_V3
48
zip.extract(entry, entry.name, destination_directory: destination)
49
else
50
zip.extract(entry, to)
51
end
52
end
53
end
54
55
destination
56
end
57
58
def zip(path)
59
with_tmp_zip do |zip|
60
::Find.find(path) do |file|
61
add_zip_entry zip, file, file.sub("#{path}/", '') unless File.directory?(file)
62
end
63
64
zip.commit
65
File.open(zip.name, 'rb') { |io| Base64.strict_encode64 io.read }
66
end
67
end
68
69
def zip_file(path)
70
with_tmp_zip do |zip|
71
add_zip_entry zip, path, File.basename(path)
72
73
zip.commit
74
File.open(zip.name, 'rb') { |io| Base64.strict_encode64 io.read }
75
end
76
end
77
78
private
79
80
def with_tmp_zip(&blk)
81
# Don't use Tempfile since it lacks rb_file_s_rename permission on Windows.
82
Dir.mktmpdir do |tmp_dir|
83
zip_path = File.join(tmp_dir, 'webdriver-zip')
84
if RUBYZIP_V3
85
Zip::File.open(zip_path, create: true, &blk)
86
else
87
Zip::File.open(zip_path, Zip::File::CREATE, &blk)
88
end
89
end
90
end
91
92
def add_zip_entry(zip, file, entry_name)
93
entry = Zip::Entry.new(zip.name, entry_name)
94
entry.follow_symlinks = true
95
96
zip.add entry, file
97
end
98
end
99
end # Zipper
100
end # WebDriver
101
end # Selenium
102
103