Look for extension first, then fall back to magic number.
This commit is contained in:
parent
a21e7ff742
commit
281ead3096
@ -43,17 +43,17 @@ module UnpackStrategy
|
|||||||
Xar,
|
Xar,
|
||||||
Ttf,
|
Ttf,
|
||||||
Otf,
|
Otf,
|
||||||
Executable,
|
|
||||||
Git,
|
Git,
|
||||||
Mercurial,
|
Mercurial,
|
||||||
Subversion,
|
Subversion,
|
||||||
Cvs,
|
Cvs,
|
||||||
|
SelfExtractingExecutable, # needs to be before Cab
|
||||||
|
Cab,
|
||||||
|
Executable,
|
||||||
Dmg, # needs to be before Bzip2
|
Dmg, # needs to be before Bzip2
|
||||||
Bzip2,
|
Bzip2,
|
||||||
Fossil,
|
Fossil,
|
||||||
Bazaar,
|
Bazaar,
|
||||||
SelfExtractingExecutable, # needs to be before Cab
|
|
||||||
Cab,
|
|
||||||
Compress,
|
Compress,
|
||||||
P7Zip,
|
P7Zip,
|
||||||
Sit,
|
Sit,
|
||||||
@ -77,19 +77,14 @@ module UnpackStrategy
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.from_path(path)
|
def self.from_path(path)
|
||||||
strategy = strategies.detect do |s|
|
strategy = strategies
|
||||||
s.can_extract?(path)
|
.sort_by { |s| s.extensions.map(&:length).max(0) }
|
||||||
end
|
.reverse
|
||||||
|
.detect { |s| s.extensions.include?(path.extname) }
|
||||||
|
|
||||||
# This is so that bad files produce good error messages.
|
strategy ||= strategies.detect { |s| s.can_extract?(path) }
|
||||||
strategy ||= case path.extname
|
|
||||||
when ".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz", ".tar.xz", ".txz"
|
strategy ||= Uncompressed
|
||||||
Tar
|
|
||||||
when ".zip"
|
|
||||||
Zip
|
|
||||||
else
|
|
||||||
Uncompressed
|
|
||||||
end
|
|
||||||
|
|
||||||
strategy
|
strategy
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".air"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
mime_type = "application/vnd.adobe.air-application-installer-package+zip"
|
mime_type = "application/vnd.adobe.air-application-installer-package+zip"
|
||||||
path.magic_number.match?(/.{59}#{Regexp.escape(mime_type)}/)
|
path.magic_number.match?(/.{59}#{Regexp.escape(mime_type)}/)
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".bz2"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\ABZh/n)
|
path.magic_number.match?(/\ABZh/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".cab"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\AMSCF/n)
|
path.magic_number.match?(/\AMSCF/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
class Compress < Tar
|
class Compress < Tar
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".compress"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\A\037\235/n)
|
path.magic_number.match?(/\A\037\235/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.directory?
|
path.directory?
|
||||||
end
|
end
|
||||||
|
@ -92,14 +92,11 @@ module UnpackStrategy
|
|||||||
end
|
end
|
||||||
private_constant :Mount
|
private_constant :Mount
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".dmg"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
bzip2 = Bzip2.can_extract?(path)
|
|
||||||
|
|
||||||
zlib = path.magic_number.match?(/\A(\x78|\x08|\x18|\x28|\x38|\x48|\x58|\x68)/n) &&
|
|
||||||
(path.magic_number[0...2].unpack("S>").first % 31).zero?
|
|
||||||
|
|
||||||
return false unless bzip2 || zlib
|
|
||||||
|
|
||||||
imageinfo = system_command("hdiutil",
|
imageinfo = system_command("hdiutil",
|
||||||
args: ["imageinfo", path],
|
args: ["imageinfo", path],
|
||||||
print_stderr: false).stdout
|
print_stderr: false).stdout
|
||||||
|
@ -4,8 +4,13 @@ module UnpackStrategy
|
|||||||
class Executable < Uncompressed
|
class Executable < Uncompressed
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".sh", ".bash"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\A#!\s*\S+/n)
|
path.magic_number.match?(/\A#!\s*\S+/n) ||
|
||||||
|
path.magic_number.match?(/\AMZ/n)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
return false unless path.magic_number.match?(/\ASQLite format 3\000/n)
|
return false unless path.magic_number.match?(/\ASQLite format 3\000/n)
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(_path)
|
def self.can_extract?(_path)
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".gz"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\A\037\213/n)
|
path.magic_number.match?(/\A\037\213/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
class Jar < Uncompressed
|
class Jar < Uncompressed
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".apk", ".jar"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
return false unless Zip.can_extract?(path)
|
return false unless Zip.can_extract?(path)
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".lha", ".lzh"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\A..-(lh0|lh1|lz4|lz5|lzs|lh\\40|lhd|lh2|lh3|lh4|lh5)-/n)
|
path.magic_number.match?(/\A..-(lh0|lh1|lz4|lz5|lzs|lh\\40|lhd|lh2|lh3|lh4|lh5)-/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
class LuaRock < Uncompressed
|
class LuaRock < Uncompressed
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".rock"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
return false unless Zip.can_extract?(path)
|
return false unless Zip.can_extract?(path)
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".lz"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\ALZIP/n)
|
path.magic_number.match?(/\ALZIP/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".lzma"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\A\]\000\000\200\000/n)
|
path.magic_number.match?(/\A\]\000\000\200\000/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,14 @@ module UnpackStrategy
|
|||||||
class MicrosoftOfficeXml < Uncompressed
|
class MicrosoftOfficeXml < Uncompressed
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[
|
||||||
|
".doc", ".docx",
|
||||||
|
".ppt", ".pptx",
|
||||||
|
".xls", ".xlsx"
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
return false unless Zip.can_extract?(path)
|
return false unless Zip.can_extract?(path)
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
class Otf < Uncompressed
|
class Otf < Uncompressed
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".otf"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\AOTTO/n)
|
path.magic_number.match?(/\AOTTO/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".7z"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\A7z\xBC\xAF\x27\x1C/n)
|
path.magic_number.match?(/\A7z\xBC\xAF\x27\x1C/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
class Pkg < Uncompressed
|
class Pkg < Uncompressed
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".pkg", ".mkpg"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.extname.match?(/\A.m?pkg\Z/) &&
|
path.extname.match?(/\A.m?pkg\Z/) &&
|
||||||
(path.directory? || path.magic_number.match?(/\Axar!/n))
|
(path.directory? || path.magic_number.match?(/\Axar!/n))
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".rar"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\ARar!/n)
|
path.magic_number.match?(/\ARar!/n)
|
||||||
end
|
end
|
||||||
|
@ -4,10 +4,13 @@ module UnpackStrategy
|
|||||||
class SelfExtractingExecutable < GenericUnar
|
class SelfExtractingExecutable < GenericUnar
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.extensions
|
||||||
return false unless path.magic_number.match?(/\AMZ/n)
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
path.file_type.include?("self-extracting archive")
|
def self.can_extract?(path)
|
||||||
|
path.magic_number.match?(/\AMZ/n) &&
|
||||||
|
path.file_type.include?("self-extracting archive")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
class Sit < GenericUnar
|
class Sit < GenericUnar
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".sit"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\AStuffIt/n)
|
path.magic_number.match?(/\AStuffIt/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,16 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[
|
||||||
|
".tar",
|
||||||
|
".tbz", ".tbz2", ".tar.bz2",
|
||||||
|
".tgz", ".tar.gz",
|
||||||
|
".tlz", ".tar.lz",
|
||||||
|
".txz", ".tar.xz"
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
return true if path.magic_number.match?(/\A.{257}ustar/n)
|
return true if path.magic_number.match?(/\A.{257}ustar/n)
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
class Ttf < Uncompressed
|
class Ttf < Uncompressed
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".ttc", ".ttf"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
# TrueType Font
|
# TrueType Font
|
||||||
path.magic_number.match?(/\A\000\001\000\000\000/n) ||
|
path.magic_number.match?(/\A\000\001\000\000\000/n) ||
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".xar"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\Axar!/n)
|
path.magic_number.match?(/\Axar!/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".xz"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\A\xFD7zXZ\x00/n)
|
path.magic_number.match?(/\A\xFD7zXZ\x00/n)
|
||||||
end
|
end
|
||||||
|
@ -4,6 +4,10 @@ module UnpackStrategy
|
|||||||
|
|
||||||
using Magic
|
using Magic
|
||||||
|
|
||||||
|
def self.extensions
|
||||||
|
[".zip"]
|
||||||
|
end
|
||||||
|
|
||||||
def self.can_extract?(path)
|
def self.can_extract?(path)
|
||||||
path.magic_number.match?(/\APK(\003\004|\005\006)/n)
|
path.magic_number.match?(/\APK(\003\004|\005\006)/n)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user