2020-10-20 00:56:50 +02:00
|
|
|
# typed: strict
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-23 20:59:21 +02:00
|
|
|
module UnpackStrategy
|
2020-08-09 06:09:05 +02:00
|
|
|
# Strategy for unpacking ZIP archives.
|
2018-07-23 20:59:21 +02:00
|
|
|
class Zip
|
2020-10-20 00:56:50 +02:00
|
|
|
extend T::Sig
|
|
|
|
|
2018-07-23 20:59:21 +02:00
|
|
|
include UnpackStrategy
|
|
|
|
|
2018-07-29 10:04:51 +02:00
|
|
|
using Magic
|
|
|
|
|
2020-10-20 00:56:50 +02:00
|
|
|
sig { returns(T::Array[String]) }
|
2018-07-30 09:49:59 +02:00
|
|
|
def self.extensions
|
|
|
|
[".zip"]
|
|
|
|
end
|
|
|
|
|
2020-10-20 00:56:50 +02:00
|
|
|
sig { params(path: Pathname).returns(T::Boolean) }
|
2018-07-29 10:04:51 +02:00
|
|
|
def self.can_extract?(path)
|
|
|
|
path.magic_number.match?(/\APK(\003\004|\005\006)/n)
|
2018-07-23 20:59:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-17 22:45:55 -08:00
|
|
|
sig {
|
2020-10-20 00:56:50 +02:00
|
|
|
override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean)
|
|
|
|
.returns(SystemCommand::Result)
|
2021-01-17 22:45:55 -08:00
|
|
|
}
|
2018-07-23 20:59:21 +02:00
|
|
|
def extract_to_dir(unpack_dir, basename:, verbose:)
|
|
|
|
quiet_flags = verbose ? [] : ["-qq"]
|
2018-09-13 23:17:06 +02:00
|
|
|
result = system_command! "unzip",
|
2019-10-15 12:33:42 +00:00
|
|
|
args: [*quiet_flags, "-o", path, "-d", unpack_dir],
|
2018-11-02 17:18:07 +00:00
|
|
|
verbose: verbose,
|
2018-09-13 23:17:06 +02:00
|
|
|
print_stderr: false
|
|
|
|
|
|
|
|
FileUtils.rm_rf unpack_dir/"__MACOSX"
|
|
|
|
|
|
|
|
result
|
2018-07-23 20:59:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-08-01 07:41:52 +02:00
|
|
|
|
|
|
|
require "extend/os/mac/unpack_strategy/zip" if OS.mac?
|