Skip to content

Commit

Permalink
For unknown extensions, check mime type for application/zip - there i…
Browse files Browse the repository at this point in the history
…s ton of such cases
  • Loading branch information
taw committed Jul 24, 2018
1 parent 72799df commit 06cbffe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
35 changes: 21 additions & 14 deletions bin/unall
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,33 @@ class Unarchiver
def formats
@formats ||= Formats.map{|fmt, exts| exts.map{|ext| [fmt, ext]}}.flatten(1)
end
def unarchive_run_with_fmt(fn, bn, fmt, ext)
dn = bn[0...-ext.size]
prefixes = send("files_#{fmt}", fn).map{|f| f.sub(/\/.*/, "")}.uniq.select{|f| f != ""}
if prefixes.size == 1 and !File.exist?(prefixes[0]) and !force_separate_dir
send("unpack_#{fmt}", fn)
else
counter = 1
dnx = dn
while File.exist?(dnx)
dnx = "#{dn}-#{counter}"
counter += 1
end
FileUtils.mkdir_p dnx
Dir.chdir(dnx){ send("unpack_#{fmt}", fn) ? "OK" : "FAIL" }
end
end
def unarchive_run(fn)
bn = File.basename(fn)
formats.each do |fmt, ext|
if bn.downcase.reverse[0, ext.size] == ext.reverse
dn = bn[0...-ext.size]
prefixes = send("files_#{fmt}", fn).map{|f| f.sub(/\/.*/, "")}.uniq.select{|f| f != ""}
if prefixes.size == 1 and !File.exist?(prefixes[0]) and !force_separate_dir
return send("unpack_#{fmt}", fn)
else
counter = 1
dnx = dn
while File.exist?(dnx)
dnx = "#{dn}-#{counter}"
counter += 1
end
FileUtils.mkdir_p dnx
return Dir.chdir(dnx){ send("unpack_#{fmt}", fn) ? "OK" : "FAIL" }
end
return unarchive_run_with_fmt(fn, bn, fmt, ext)
end
end
mime_type = `file -b --mime-type #{fn.shellescape}`.chomp
if mime_type == "application/zip"
return unarchive_run_with_fmt(fn, bn, :"7z", File.extname(fn))
end
return "Not supported"
end
def unarchive(fn)
Expand Down
10 changes: 10 additions & 0 deletions spec/unall_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ def unpacks_and_deletes_archive(binary, name)
unpacks_and_deletes_archive binary, "foo.tar.bz2"
end
end

it "unzips archives in .zip format even with nonstandard extension" do
MockUnix.new do |env|
create_archive do
system "7za a foo.zip a.txt b.txt >/dev/null"
system "mv foo.zip foo.wtf"
end
unpacks_and_deletes_archive binary, "foo.wtf"
end
end
end

0 comments on commit 06cbffe

Please sign in to comment.