The archive header is damaged, preventing format detection.
Data Corruption
The compressed data is damaged, causing decompression failures.
Partial Download
The archive is incomplete due to interrupted download.
Detection
Check File Size
expected_size=1_000_000# From download sourceactual_size=File.size(archive_path)ifactual_size<expected_sizeputs"File may be incomplete"end
Check File Type
file archive.zip
# Expected: Zip archive data# If shows: data - file may be corrupted
Try Extraction
beginExcavate::Archive.new(archive).extractrescueExcavate::UnknownArchiveErrorputs"Archive is corrupted or invalid"rescueExcavate::Error=>eputs"Extraction error: #{e.message}"end
Recovery Strategies
CAB Salvage Mode
CAB extraction uses salvage mode automatically:
# Cabriolet enables salvage mode# Damaged files are extracted with warningsExcavate::Archive.new('damaged.cab').extract
Partial Extraction
Extract what’s possible:
archive=Excavate::Archive.new('damaged.zip')extracted=[]failed=[]archive.filesdo|path|begin# Try to copy fileFileUtils.cp(path,target)extracted<<pathrescue=>efailed<<{path: path,error: e.message}endendputs"Extracted: #{extracted.size}"puts"Failed: #{failed.size}"
# Extract files that are not corruptedarchive.files(filter: '**/*.txt')do|path|begincontent=File.read(path)ifcontent.valid_encoding?# File is validsave_file(path,content)endrescue=>eputs"Skipping corrupted: #{path}"endend
defsafe_extract(path)# Basic checksraise"File not found"unlessFile.exist?(path)raise"File is empty"ifFile.zero?(path)# Try extractionbeginExcavate::Archive.new(path).extractrescueExcavate::UnknownArchiveErrorraise"File is not a valid archive"endend