refactor(zip): share Zip64 end record probe offsets (#17084)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-05 21:30:46 +08:00
1 parent 56a03e349b
commit 6ea57e503c
3 files changed
+10 -16

No files matched your search

+2 -7
View File
@@ -8,6 +8,7 @@ import {
ZIP_EOCD_BYTES,
readZip64DirectoryRecord,
readZip64Locator,
zip64EndRecordOffsets,
zip64EndUsesSingleVolume,
zip64LocatorUsesSingleVolume,
zipEndOfCentralDirectoryCandidates,
@@ -55,13 +56,7 @@ const directoryEnd = (reader: BinaryReader) => {
// Like Python's zipfile, follow a locator whether or not the classic record defers to it.
const locator = readZip64Locator(reader, locatorOffset);
if (locator === undefined) return classic;
const declared = locator.recordOffset;
const fallback = locatorOffset - ZIP64_END.fixedBytes;
// Keep the fixed-record fallback for archives with an unadjusted prepended offset.
for (const at of [
declared <= BigInt(fallback) ? Number(declared) : -1,
fallback,
]) {
for (const at of zip64EndRecordOffsets(locator.recordOffset, locatorOffset)) {
const record = readZip64DirectoryRecord(reader, at, locatorOffset);
if (!record.ok || record.value.end !== locatorOffset) continue;
if (
+6
View File
@@ -34,6 +34,12 @@ export const ZIP64_LOCATOR = {
totalDisksOffset: 16,
} as const;
/** Probe the declared record first, then the fixed record before the locator, even when the two offsets match. */
export const zip64EndRecordOffsets = (declared: bigint, locator: number) => {
const fallback = locator - ZIP64_END.fixedBytes;
return [declared <= BigInt(fallback) ? Number(declared) : -1, fallback];
};
type Zip64Locator = {
readonly disk: number;
readonly totalDisks: number;
+2 -9
View File
@@ -15,6 +15,7 @@ import {
ZIP64_LOCATOR,
readZip64DirectoryRecord,
readZip64Locator,
zip64EndRecordOffsets,
zip64EndUsesSingleVolume,
zip64LocatorUsesSingleVolume,
zipEndOfCentralDirectoryCandidates,
@@ -98,15 +99,7 @@ const trailingDirectory = async (
0,
);
if (locator !== undefined) {
const fallback = locatorAt - ZIP64_END.fixedBytes;
// A declared pointer covers adjusted offsets and variable-size records;
// the fixed-record fallback covers an unchanged archive after a prefix.
for (const at of [
locator.recordOffset <= BigInt(fallback)
? Number(locator.recordOffset)
: -1,
fallback,
]) {
for (const at of zip64EndRecordOffsets(locator.recordOffset, locatorAt)) {
if (at < 0) continue;
const record = readZip64DirectoryRecord(
new BinaryReader(