Format Detection
Concepts
Format detection is the process of determining which extractor to use for a given file. Excavate uses file extension as the primary detection method.
Extension-Based Detection
Excavate maps file extensions to extractors:
TYPES = {
"cab" => CabExtractor,
"cpio" => CpioExtractor,
"exe" => SevenZipExtractor,
"gz" => GzipExtractor,
"msi" => OleExtractor,
"pkg" => XarExtractor,
"rpm" => RpmExtractor,
"tar" => TarExtractor,
"xz" => XzExtractor,
"zip" => ZipExtractor
}Compound Extensions
Some formats use compound extensions:
-
.tar.gz- TAR + GZIP -
.tar.xz- TAR + XZ -
.tgz- Short for.tar.gz -
.txz- Short for.tar.xz
Excavate handles these automatically by extracting layers in sequence.
Special Cases
Magic Byte Detection
Limited magic byte detection is available via FileMagic:
format = Excavate::FileMagic.detect('archive.cab')
# => :cab
format = Excavate::FileMagic.detect('file.gz')
# => :gzipSupported magic byte detection:
| Format | Magic Bytes | Return |
|---|---|---|
CAB |
|
|
GZIP |
|
|
XZ |
|
|
Limitations
Extension-based detection has limitations:
-
Files must have correct extensions
-
Renamed files may not extract properly
-
Content-based detection is limited
Workarounds:
# Rename file to correct extension
File.rename('unknown.dat', 'archive.zip')
Excavate::Archive.new('archive.zip').extractBest Practices
-
Ensure files have correct extensions
-
Use
.tar.gznot.tgzfor clarity -
Verify format if extraction fails