Import accepts a records package (uses the .sam.zip inside it)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-08 06:53:33 -05:00
1 parent 87d25f141a
commit 3f390f3366
2 files changed
+28 -2

No files matched your search

+20 -2
View File
@@ -28,13 +28,31 @@ module AircraftArchive
counts: RECORD_SECTIONS.to_h { [ _1, Array(manifest[_1]).size ] })
end
def self.with_zip(path)
# Opens the archive. A records package (Access & Sharing) carries the
# .sam.zip inside it, so that inner archive is opened instead.
def self.with_zip(path, &)
Zip.validate_entry_sizes = true # a lying size header raises instead of inflating forever
Zip::File.open(path) { |zip| yield zip }
Zip::File.open(path) do |zip|
inner = nested_archive(zip)
next yield(zip) unless inner
raise Error, "The archive inside the records package is too large." if inner.size > ARCHIVE_MAX_BYTES
Tempfile.create([ "sam-inner", ".zip" ], binmode: true) do |tmp|
inner.get_input_stream { |io| IO.copy_stream(io, tmp) }
tmp.flush
Zip::File.open(tmp.path, &)
end
end
rescue Zip::Error => e
raise Error, "Not a valid .sam.zip archive (#{e.message})"
end
# The single *.sam.zip entry of a records package (no manifest of its own).
def self.nested_archive(zip)
return if zip.find_entry("manifest.json") || root_prefix(zip).present?
candidates = zip.entries.reject { _1.name.start_with?("__MACOSX/") }.select { _1.name.match?(%r{\A([^/]+/)?[^/]+\.sam\.zip\z}) }
candidates.first if candidates.one?
end
# "" normally; "folder/" when everything sits in one folder, as when a
# browser unzipped the download and the folder was zipped again (Finder's
# Compress adds that folder, and a __MACOSX folder that's ignored).