archives: add code for detecting .rar and .7z files.

This commit is contained in:
evazion
2022-11-14 16:13:22 -06:00
parent 5f92f452fe
commit f128c48485
6 changed files with 60 additions and 3 deletions

View File

@@ -55,8 +55,33 @@ class FileTypeDetector
# https://aomediacodec.github.io/av1-avif/#brands-overview
when /\A....ftyp(?:avif|avis)/
:avif
# https://www.loc.gov/preservation/digital/formats/fdd/fdd000354.shtml#sign
# https://en.wikipedia.org/wiki/ZIP_(file_format)
# XXX Does not detect self-extracting archives
when /\APK\x03\x04/
:zip
# https://docs.fileformat.com/compression/7z/#file-signature
# https://py7zr.readthedocs.io/en/latest/archive_format.html#signature
# https://www.loc.gov/preservation/digital/formats/fdd/fdd000539.shtml#sign
# https://en.wikipedia.org/wiki/7z
# XXX Does not detect self-extracting archives
when /\A7z\xbc\xaf\x27\x1c/n
:"7z"
# Rar 1.5 to 4.0
# https://www.rarlab.com/technote.htm#rarsign
# https://www.loc.gov/preservation/digital/formats/fdd/fdd000450.shtml#sign
# https://en.wikipedia.org/wiki/RAR_(file_format)
# XXX Does not detect self-extracting archives
when /\ARar!\x1a\x07\x00/n
:rar
# Rar 5.0+
when /\ARar!\x1a\x07\x01\x00/n
:rar
else
:bin
end

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -75,6 +75,27 @@ class DanbooruArchiveTest < ActiveSupport::TestCase
filenames.each { |filename| assert_equal(true, File.exist?(filename)) }
end
end
should "work with a .zip file" do
Danbooru::Archive.extract!("test/files/archive/ugoira.zip") do |dir, filenames|
assert_equal(5, filenames.size)
filenames.each { |filename| assert_equal(true, File.exist?(filename)) }
end
end
should "work with a .rar file" do
Danbooru::Archive.extract!("test/files/archive/ugoira.rar") do |dir, filenames|
assert_equal(5, filenames.size)
filenames.each { |filename| assert_equal(true, File.exist?(filename)) }
end
end
should "work with a .7z file" do
Danbooru::Archive.extract!("test/files/archive/ugoira.7z") do |dir, filenames|
assert_equal(5, filenames.size)
filenames.each { |filename| assert_equal(true, File.exist?(filename)) }
end
end
end
context "#uncompressed_size method" do
@@ -92,9 +113,20 @@ class DanbooruArchiveTest < ActiveSupport::TestCase
end
context "#format method" do
should "work" do
archive = Danbooru::Archive.open!("test/files/ugoira.zip")
assert_equal("ZIP 2.0 (uncompressed)", archive.format)
should "detect the file type" do
assert_equal("ZIP 2.0 (uncompressed)", Danbooru::Archive.open("test/files/archive/ugoira.zip").format)
assert_equal("RAR5", Danbooru::Archive.open("test/files/archive/ugoira.rar").format)
assert_equal("7-Zip", Danbooru::Archive.open("test/files/archive/ugoira.7z").format)
assert_equal("7-Zip", Danbooru::Archive.open("test/files/archive/ugoira.tar.7z").format)
end
end
context "#file_ext method" do
should "detect the file extension" do
assert_equal(:zip, Danbooru::Archive.open("test/files/archive/ugoira.zip").file_ext)
assert_equal(:rar, Danbooru::Archive.open("test/files/archive/ugoira.rar").file_ext)
assert_equal(:"7z", Danbooru::Archive.open("test/files/archive/ugoira.7z").file_ext)
assert_equal(:"7z", Danbooru::Archive.open("test/files/archive/ugoira.tar.7z").file_ext)
end
end