can1357/oh-my-pi · error · ArchiveError

Invalid RAR4 archive: ${reason}

Error message

Invalid RAR4 archive: ${reason}

What it means

corrupt() is the RAR4 decoder's central malformed-data handler: any structural violation while parsing or decoding a RAR4 stream — bad Huffman tables, invalid block types, out-of-range offsets, truncated data, bad filter ranges — surfaces as 'Invalid RAR4 archive: <reason>' with a specific reason string. It signals the byte stream does not conform to the RAR4 format at that point.

Source

Thrown at packages/utils/src/ar/rar/rar4-decoder.ts:458

	for (let position = 0; position < size; ) {
		const symbol = preTable.decode(bits);
		if (symbol < 16) {
			lengths[position] = (symbol + previous[position]!) & 15;
			position++;
		} else if (symbol === 16 || symbol === 17) {
			let count = bits.read(symbol === 16 ? 3 : 7) + (symbol === 16 ? 3 : 11);
			const value = position === 0 ? 0 : lengths[position - 1]!;
			while (count-- > 0 && position < size) lengths[position++] = value;
		} else {
			let count = bits.read(symbol === 18 ? 3 : 7) + (symbol === 18 ? 3 : 11);
			while (count-- > 0 && position < size) lengths[position++] = 0;
		}
	}
	return lengths;
}

function corrupt(reason: string): never {
	throw new ArchiveError(`Invalid RAR4 archive: ${reason}`);
}

View on GitHub (pinned to 9690622007)

Solutions

  1. Re-obtain the archive and verify checksums/size — truncation is the most common cause.
  2. For multi-part RARs, ensure every volume (.part1.rar ... .partN.rar) is present and named correctly.
  3. Validate the archive with an independent tool (unrar t archive.rar or 7z t) to confirm corruption before blaming this library.
  4. Read the embedded reason string — it pinpoints which structural invariant failed (tables, ranges, alignment) and guides whether repair is possible (e.g. `rar r` repair).

Example fix

// before (shell)
node extract.js partial.part2.rar
// after (shell)
unrar t archive.part1.rar   # verify integrity first
node extract.js archive.part1.rar  # run from part1 with all volumes present
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify integrity before extraction:
const size = (await Bun.file(p).size);
if (size === 0) throw new Error(`${p} is empty/truncated`);
// and for multi-part sets ensure all volumes exist:
if (await missingRarVolumes(p)) throw new Error(`${p} is missing volumes`);

Try / catch

try {
  await extractArchive(p, dest);
} catch (err) {
  if (err instanceof ArchiveError && err.message.startsWith("Invalid RAR4 archive:")) {
    // read err.message's reason, offer repair (`rar r`) or re-download to the user
  }
  throw err;
}

Prevention

When it happens

Trigger: Any RAR4 decode path calling corrupt(reason): invalid length/Huffman tables, packed-size overflow, ranges where start+blockSize > unpackedSize, misaligned or truncated bit streams, inconsistent header fields.

Common situations: Partially downloaded or truncated .rar files; bit-rotted archives on old media; deliberately fuzzed archives; extracting a multi-part RAR without all volumes present.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/c6a4ac9303bb690c. Report an issue: GitHub.